@@ -5,7 +5,7 @@ use rustc_errors::Applicability;
5
5
use syntax:: ast:: * ;
6
6
7
7
declare_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.
9
9
///
10
10
/// **Why is this bad?** Adding `'static` to every reference can create very
11
11
/// complicated types.
@@ -16,29 +16,32 @@ declare_clippy_lint! {
16
16
/// ```ignore
17
17
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
18
18
/// &[...]
19
+ /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
20
+ /// &[...]
19
21
/// ```
20
22
/// This code can be rewritten as
21
23
/// ```ignore
22
24
/// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
25
+ /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
23
26
/// ```
24
- pub CONST_STATIC_LIFETIME ,
27
+ pub REDUNDANT_STATIC_LIFETIMES ,
25
28
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."
27
30
}
28
31
29
- declare_lint_pass ! ( StaticConst => [ CONST_STATIC_LIFETIME ] ) ;
32
+ declare_lint_pass ! ( RedundantStaticLifetimes => [ REDUNDANT_STATIC_LIFETIMES ] ) ;
30
33
31
- impl StaticConst {
34
+ impl RedundantStaticLifetimes {
32
35
// 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 ) {
34
37
match ty. node {
35
38
// Be careful of nested structures (arrays and tuples)
36
39
TyKind :: Array ( ref ty, _) => {
37
- self . visit_type ( & * ty, cx) ;
40
+ self . visit_type ( & * ty, cx, reason ) ;
38
41
} ,
39
42
TyKind :: Tup ( ref tup) => {
40
43
for tup_ty in tup {
41
- self . visit_type ( & * tup_ty, cx) ;
44
+ self . visit_type ( & * tup_ty, cx, reason ) ;
42
45
}
43
46
} ,
44
47
// This is what we are looking for !
@@ -50,44 +53,40 @@ impl StaticConst {
50
53
if lifetime. ident . name == syntax:: symbol:: kw:: StaticLifetime {
51
54
let snip = snippet ( cx, borrow_type. ty . span , "<type>" ) ;
52
55
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
+ } ) ;
67
64
}
68
65
} ,
69
66
_ => { } ,
70
67
}
71
68
}
72
- self . visit_type ( & * borrow_type. ty , cx) ;
69
+ self . visit_type ( & * borrow_type. ty , cx, reason ) ;
73
70
} ,
74
71
TyKind :: Slice ( ref ty) => {
75
- self . visit_type ( ty, cx) ;
72
+ self . visit_type ( ty, cx, reason ) ;
76
73
} ,
77
74
_ => { } ,
78
75
}
79
76
}
80
77
}
81
78
82
- impl EarlyLintPass for StaticConst {
79
+ impl EarlyLintPass for RedundantStaticLifetimes {
83
80
fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
84
81
if !in_macro_or_desugar ( item. span ) {
85
- // Match only constants...
86
82
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" ) ;
88
89
}
89
90
}
90
91
}
91
-
92
- // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
93
92
}
0 commit comments