@@ -5,7 +5,7 @@ use rustc_errors::Applicability;
55use syntax:: ast:: * ;
66
77declare_clippy_lint ! {
8- /// **What it does:** Checks for constants with an explicit `'static` lifetime.
8+ /// **What it does:** Checks for constants and statics with an explicit `'static` lifetime.
99 ///
1010 /// **Why is this bad?** Adding `'static` to every reference can create very
1111 /// complicated types.
@@ -16,29 +16,32 @@ declare_clippy_lint! {
1616 /// ```ignore
1717 /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
1818 /// &[...]
19+ /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
20+ /// &[...]
1921 /// ```
2022 /// This code can be rewritten as
2123 /// ```ignore
2224 /// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
25+ /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
2326 /// ```
24- pub CONST_STATIC_LIFETIME ,
27+ pub REDUNDANT_STATIC_LIFETIMES ,
2528 style,
26- "Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
29+ "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them."
2730}
2831
29- declare_lint_pass ! ( StaticConst => [ CONST_STATIC_LIFETIME ] ) ;
32+ declare_lint_pass ! ( RedundantStaticLifetimes => [ REDUNDANT_STATIC_LIFETIMES ] ) ;
3033
31- impl StaticConst {
34+ impl RedundantStaticLifetimes {
3235 // Recursively visit types
33- fn visit_type ( & mut self , ty : & Ty , cx : & EarlyContext < ' _ > ) {
36+ fn visit_type ( & mut self , ty : & Ty , cx : & EarlyContext < ' _ > , reason : & str ) {
3437 match ty. node {
3538 // Be careful of nested structures (arrays and tuples)
3639 TyKind :: Array ( ref ty, _) => {
37- self . visit_type ( & * ty, cx) ;
40+ self . visit_type ( & * ty, cx, reason ) ;
3841 } ,
3942 TyKind :: Tup ( ref tup) => {
4043 for tup_ty in tup {
41- self . visit_type ( & * tup_ty, cx) ;
44+ self . visit_type ( & * tup_ty, cx, reason ) ;
4245 }
4346 } ,
4447 // This is what we are looking for !
@@ -50,44 +53,40 @@ impl StaticConst {
5053 if lifetime. ident . name == syntax:: symbol:: kw:: StaticLifetime {
5154 let snip = snippet ( cx, borrow_type. ty . span , "<type>" ) ;
5255 let sugg = format ! ( "&{}" , snip) ;
53- span_lint_and_then (
54- cx,
55- CONST_STATIC_LIFETIME ,
56- lifetime. ident . span ,
57- "Constants have by default a `'static` lifetime" ,
58- |db| {
59- db. span_suggestion (
60- ty. span ,
61- "consider removing `'static`" ,
62- sugg,
63- Applicability :: MachineApplicable , //snippet
64- ) ;
65- } ,
66- ) ;
56+ span_lint_and_then ( cx, REDUNDANT_STATIC_LIFETIMES , lifetime. ident . span , reason, |db| {
57+ db. span_suggestion (
58+ ty. span ,
59+ "consider removing `'static`" ,
60+ sugg,
61+ Applicability :: MachineApplicable , //snippet
62+ ) ;
63+ } ) ;
6764 }
6865 } ,
6966 _ => { } ,
7067 }
7168 }
72- self . visit_type ( & * borrow_type. ty , cx) ;
69+ self . visit_type ( & * borrow_type. ty , cx, reason ) ;
7370 } ,
7471 TyKind :: Slice ( ref ty) => {
75- self . visit_type ( ty, cx) ;
72+ self . visit_type ( ty, cx, reason ) ;
7673 } ,
7774 _ => { } ,
7875 }
7976 }
8077}
8178
82- impl EarlyLintPass for StaticConst {
79+ impl EarlyLintPass for RedundantStaticLifetimes {
8380 fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
8481 if !in_macro_or_desugar ( item. span ) {
85- // Match only constants...
8682 if let ItemKind :: Const ( ref var_type, _) = item. node {
87- self . visit_type ( var_type, cx) ;
83+ self . visit_type ( var_type, cx, "Constants have by default a `'static` lifetime" ) ;
84+ // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
85+ }
86+
87+ if let ItemKind :: Static ( ref var_type, _, _) = item. node {
88+ self . visit_type ( var_type, cx, "Statics have by default a `'static` lifetime" ) ;
8889 }
8990 }
9091 }
91-
92- // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
9392}
0 commit comments