Add lint for statics with explicit static lifetime.
This commit is contained in:
parent
bd39cea01c
commit
16bd4796e9
5 changed files with 229 additions and 0 deletions
|
|
@ -256,6 +256,7 @@ pub mod returns;
|
|||
pub mod serde_api;
|
||||
pub mod shadow;
|
||||
pub mod slow_vector_initialization;
|
||||
pub mod static_static_lifetime;
|
||||
pub mod strings;
|
||||
pub mod suspicious_trait_impl;
|
||||
pub mod swap;
|
||||
|
|
@ -554,6 +555,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
|
|||
reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default());
|
||||
reg.register_late_lint_pass(box types::ImplicitHasher);
|
||||
reg.register_early_lint_pass(box const_static_lifetime::StaticConst);
|
||||
reg.register_early_lint_pass(box static_static_lifetime::StaticStatic);
|
||||
reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom);
|
||||
reg.register_late_lint_pass(box replace_consts::ReplaceConsts);
|
||||
reg.register_late_lint_pass(box types::UnitArg);
|
||||
|
|
@ -844,6 +846,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
|
|||
returns::UNUSED_UNIT,
|
||||
serde_api::SERDE_API_MISUSE,
|
||||
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
|
||||
static_static_lifetime::STATIC_STATIC_LIFETIME,
|
||||
strings::STRING_LIT_AS_BYTES,
|
||||
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
|
||||
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
|
||||
|
|
@ -962,6 +965,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
|
|||
returns::LET_AND_RETURN,
|
||||
returns::NEEDLESS_RETURN,
|
||||
returns::UNUSED_UNIT,
|
||||
static_static_lifetime::STATIC_STATIC_LIFETIME,
|
||||
strings::STRING_LIT_AS_BYTES,
|
||||
types::FN_TO_NUMERIC_CAST,
|
||||
types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION,
|
||||
|
|
|
|||
93
clippy_lints/src/static_static_lifetime.rs
Normal file
93
clippy_lints/src/static_static_lifetime.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
|
||||
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
||||
use rustc::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_errors::Applicability;
|
||||
use syntax::ast::*;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for statics with an explicit `'static` lifetime.
|
||||
///
|
||||
/// **Why is this bad?** Adding `'static` to every reference can create very
|
||||
/// complicated types.
|
||||
///
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
/// ```ignore
|
||||
/// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
|
||||
/// &[...]
|
||||
/// ```
|
||||
/// This code can be rewritten as
|
||||
/// ```ignore
|
||||
/// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
|
||||
/// ```
|
||||
pub STATIC_STATIC_LIFETIME,
|
||||
style,
|
||||
"Using explicit `'static` lifetime for statics when elision rules would allow omitting them."
|
||||
}
|
||||
|
||||
declare_lint_pass!(StaticStatic => [STATIC_STATIC_LIFETIME]);
|
||||
|
||||
impl StaticStatic {
|
||||
// Recursively visit types
|
||||
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
|
||||
match ty.node {
|
||||
// Be careful of nested structures (arrays and tuples)
|
||||
TyKind::Array(ref ty, _) => {
|
||||
self.visit_type(&*ty, cx);
|
||||
},
|
||||
TyKind::Tup(ref tup) => {
|
||||
for tup_ty in tup {
|
||||
self.visit_type(&*tup_ty, cx);
|
||||
}
|
||||
},
|
||||
// This is what we are looking for !
|
||||
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
|
||||
// Match the 'static lifetime
|
||||
if let Some(lifetime) = *optional_lifetime {
|
||||
match borrow_type.ty.node {
|
||||
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
|
||||
if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
|
||||
let snip = snippet(cx, borrow_type.ty.span, "<type>");
|
||||
let sugg = format!("&{}", snip);
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
STATIC_STATIC_LIFETIME,
|
||||
lifetime.ident.span,
|
||||
"Statics have by default a `'static` lifetime",
|
||||
|db| {
|
||||
db.span_suggestion(
|
||||
ty.span,
|
||||
"consider removing `'static`",
|
||||
sugg,
|
||||
Applicability::MachineApplicable, //snippet
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
self.visit_type(&*borrow_type.ty, cx);
|
||||
},
|
||||
TyKind::Slice(ref ty) => {
|
||||
self.visit_type(ty, cx);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EarlyLintPass for StaticStatic {
|
||||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
||||
if !in_macro_or_desugar(item.span) {
|
||||
// Match only statics...
|
||||
if let ItemKind::Static(ref var_type, _, _) = item.node {
|
||||
self.visit_type(var_type, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't check associated consts because `'static` cannot be elided on those (issue #2438)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue