1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use syntax::ast::*;
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use utils::{in_macro, snippet, span_lint_and_then};
declare_clippy_lint! {
pub CONST_STATIC_LIFETIME,
style,
"Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
}
pub struct StaticConst;
impl LintPass for StaticConst {
fn get_lints(&self) -> LintArray {
lint_array!(CONST_STATIC_LIFETIME)
}
}
impl StaticConst {
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext) {
match ty.node {
TyKind::Array(ref ty, _) => {
self.visit_type(&*ty, cx);
},
TyKind::Tup(ref tup) => for tup_ty in tup {
self.visit_type(&*tup_ty, cx);
},
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
if let Some(lifetime) = *optional_lifetime {
match borrow_type.ty.node {
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) |
TyKind::Tup(..) => {
if lifetime.ident.name == "'static" {
let snip = snippet(cx, borrow_type.ty.span, "<type>");
let sugg = format!("&{}", snip);
span_lint_and_then(
cx,
CONST_STATIC_LIFETIME,
lifetime.ident.span,
"Constants have by default a `'static` lifetime",
|db| {
db.span_suggestion(ty.span, "consider removing `'static`", sugg);
},
);
}
}
_ => {}
}
}
self.visit_type(&*borrow_type.ty, cx);
},
TyKind::Slice(ref ty) => {
self.visit_type(ty, cx);
},
_ => {},
}
}
}
impl EarlyLintPass for StaticConst {
fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
if !in_macro(item.span) {
if let ItemKind::Const(ref var_type, _) = item.node {
self.visit_type(var_type, cx);
}
}
}
}