Skip to content

Commit ff1b533

Browse files
krkflip1995
authored andcommitted
Move type-checking logic in StaticConst to RedundantStaticLifetime.
1 parent 16bd479 commit ff1b533

File tree

3 files changed

+63
-47
lines changed

3 files changed

+63
-47
lines changed

clippy_lints/src/const_static_lifetime.rs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
1+
use crate::redundant_static_lifetime::RedundantStaticLifetime;
2+
use crate::utils::in_macro_or_desugar;
23
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
34
use rustc::{declare_lint_pass, declare_tool_lint};
4-
use rustc_errors::Applicability;
55
use syntax::ast::*;
66

77
declare_clippy_lint! {
@@ -31,51 +31,9 @@ declare_lint_pass!(StaticConst => [CONST_STATIC_LIFETIME]);
3131
impl StaticConst {
3232
// Recursively visit types
3333
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
34-
match ty.node {
35-
// Be careful of nested structures (arrays and tuples)
36-
TyKind::Array(ref ty, _) => {
37-
self.visit_type(&*ty, cx);
38-
},
39-
TyKind::Tup(ref tup) => {
40-
for tup_ty in tup {
41-
self.visit_type(&*tup_ty, cx);
42-
}
43-
},
44-
// This is what we are looking for !
45-
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
46-
// Match the 'static lifetime
47-
if let Some(lifetime) = *optional_lifetime {
48-
match borrow_type.ty.node {
49-
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
50-
if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
51-
let snip = snippet(cx, borrow_type.ty.span, "<type>");
52-
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-
);
67-
}
68-
},
69-
_ => {},
70-
}
71-
}
72-
self.visit_type(&*borrow_type.ty, cx);
73-
},
74-
TyKind::Slice(ref ty) => {
75-
self.visit_type(ty, cx);
76-
},
77-
_ => {},
78-
}
34+
let mut rsl =
35+
RedundantStaticLifetime::new(CONST_STATIC_LIFETIME, "Constants have by default a `'static` lifetime");
36+
rsl.visit_type(ty, cx)
7937
}
8038
}
8139

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ macro_rules! declare_clippy_lint {
141141
mod consts;
142142
#[macro_use]
143143
mod utils;
144+
mod redundant_static_lifetime;
144145

145146
// begin lints modules, do not remove this comment, it’s used in `update_lints`
146147
pub mod approx_const;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::utils::{snippet, span_lint_and_then};
2+
use rustc::lint::{EarlyContext, Lint};
3+
use rustc_errors::Applicability;
4+
use syntax::ast::*;
5+
6+
pub struct RedundantStaticLifetime {
7+
lint: &'static Lint,
8+
reason: &'static str,
9+
}
10+
11+
impl RedundantStaticLifetime {
12+
pub fn new(lint: &'static Lint, reason: &'static str) -> Self {
13+
Self { lint, reason }
14+
}
15+
// Recursively visit types
16+
pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
17+
match ty.node {
18+
// Be careful of nested structures (arrays and tuples)
19+
TyKind::Array(ref ty, _) => {
20+
self.visit_type(&*ty, cx);
21+
},
22+
TyKind::Tup(ref tup) => {
23+
for tup_ty in tup {
24+
self.visit_type(&*tup_ty, cx);
25+
}
26+
},
27+
// This is what we are looking for !
28+
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
29+
// Match the 'static lifetime
30+
if let Some(lifetime) = *optional_lifetime {
31+
match borrow_type.ty.node {
32+
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
33+
if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
34+
let snip = snippet(cx, borrow_type.ty.span, "<type>");
35+
let sugg = format!("&{}", snip);
36+
span_lint_and_then(cx, self.lint, lifetime.ident.span, self.reason, |db| {
37+
db.span_suggestion(
38+
ty.span,
39+
"consider removing `'static`",
40+
sugg,
41+
Applicability::MachineApplicable, //snippet
42+
);
43+
});
44+
}
45+
},
46+
_ => {},
47+
}
48+
}
49+
self.visit_type(&*borrow_type.ty, cx);
50+
},
51+
TyKind::Slice(ref ty) => {
52+
self.visit_type(ty, cx);
53+
},
54+
_ => {},
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)