Skip to content

Commit b38ce08

Browse files
krkflip1995
authored andcommitted
Merge StaticConst and StaticStatic lints into StaticConst.
1 parent 87e9dee commit b38ce08

8 files changed

+154
-255
lines changed
Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::redundant_static_lifetime::RedundantStaticLifetime;
2-
use crate::utils::in_macro_or_desugar;
1+
use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
32
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
43
use rustc::{declare_lint_pass, declare_tool_lint};
4+
use rustc_errors::Applicability;
55
use syntax::ast::*;
66

77
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.
99
///
1010
/// **Why is this bad?** Adding `'static` to every reference can create very
1111
/// complicated types.
@@ -16,36 +16,77 @@ 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
/// ```
2427
pub CONST_STATIC_LIFETIME,
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

2932
declare_lint_pass!(StaticConst => [CONST_STATIC_LIFETIME]);
3033

3134
impl StaticConst {
3235
// Recursively visit types
33-
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
34-
let mut rsl =
35-
RedundantStaticLifetime::new(CONST_STATIC_LIFETIME, "Constants have by default a `'static` lifetime");
36-
rsl.visit_type(ty, cx)
36+
pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) {
37+
match ty.node {
38+
// Be careful of nested structures (arrays and tuples)
39+
TyKind::Array(ref ty, _) => {
40+
self.visit_type(&*ty, cx, reason);
41+
},
42+
TyKind::Tup(ref tup) => {
43+
for tup_ty in tup {
44+
self.visit_type(&*tup_ty, cx, reason);
45+
}
46+
},
47+
// This is what we are looking for !
48+
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
49+
// Match the 'static lifetime
50+
if let Some(lifetime) = *optional_lifetime {
51+
match borrow_type.ty.node {
52+
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
53+
if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
54+
let snip = snippet(cx, borrow_type.ty.span, "<type>");
55+
let sugg = format!("&{}", snip);
56+
span_lint_and_then(cx, CONST_STATIC_LIFETIME, lifetime.ident.span, reason, |db| {
57+
db.span_suggestion(
58+
ty.span,
59+
"consider removing `'static`",
60+
sugg,
61+
Applicability::MachineApplicable, //snippet
62+
);
63+
});
64+
}
65+
},
66+
_ => {},
67+
}
68+
}
69+
self.visit_type(&*borrow_type.ty, cx, reason);
70+
},
71+
TyKind::Slice(ref ty) => {
72+
self.visit_type(ty, cx, reason);
73+
},
74+
_ => {},
75+
}
3776
}
3877
}
3978

4079
impl EarlyLintPass for StaticConst {
4180
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
4281
if !in_macro_or_desugar(item.span) {
43-
// Match only constants...
4482
if let ItemKind::Const(ref var_type, _) = item.node {
45-
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");
4689
}
4790
}
4891
}
49-
50-
// Don't check associated consts because `'static` cannot be elided on those (issue #2438)
5192
}

clippy_lints/src/lib.rs

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

146145
// begin lints modules, do not remove this comment, it’s used in `update_lints`
147146
pub mod approx_const;
@@ -257,7 +256,6 @@ pub mod returns;
257256
pub mod serde_api;
258257
pub mod shadow;
259258
pub mod slow_vector_initialization;
260-
pub mod static_static_lifetime;
261259
pub mod strings;
262260
pub mod suspicious_trait_impl;
263261
pub mod swap;
@@ -556,7 +554,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
556554
reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default());
557555
reg.register_late_lint_pass(box types::ImplicitHasher);
558556
reg.register_early_lint_pass(box const_static_lifetime::StaticConst);
559-
reg.register_early_lint_pass(box static_static_lifetime::StaticStatic);
560557
reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom);
561558
reg.register_late_lint_pass(box replace_consts::ReplaceConsts);
562559
reg.register_late_lint_pass(box types::UnitArg);
@@ -847,7 +844,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
847844
returns::UNUSED_UNIT,
848845
serde_api::SERDE_API_MISUSE,
849846
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
850-
static_static_lifetime::STATIC_STATIC_LIFETIME,
851847
strings::STRING_LIT_AS_BYTES,
852848
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
853849
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -966,7 +962,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
966962
returns::LET_AND_RETURN,
967963
returns::NEEDLESS_RETURN,
968964
returns::UNUSED_UNIT,
969-
static_static_lifetime::STATIC_STATIC_LIFETIME,
970965
strings::STRING_LIT_AS_BYTES,
971966
types::FN_TO_NUMERIC_CAST,
972967
types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION,

clippy_lints/src/redundant_static_lifetime.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

clippy_lints/src/static_static_lifetime.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/ui/const_static_lifetime.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static
2323

2424
const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
2525

26+
static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static.
27+
28+
static STATIC_VAR_TWO: &str = "Test static #2"; // This line should not raise a warning.
29+
30+
static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
31+
32+
static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
33+
34+
static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
35+
36+
static STATIC_VAR_SIX: &'static u8 = &5;
37+
38+
static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
39+
40+
static STATIC_VAR_HEIGHT: &'static Foo = &Foo {};
41+
42+
static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static.
43+
44+
static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static.
45+
46+
static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
47+
2648
fn main() {
2749
let false_positive: &'static str = "test";
2850
println!("{}", VAR_ONE);

tests/ui/const_static_lifetime.stderr

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,83 @@ error: Constants have by default a `'static` lifetime
7878
LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
7979
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
8080

81-
error: aborting due to 13 previous errors
81+
error: Statics have by default a `'static` lifetime
82+
--> $DIR/const_static_lifetime.rs:26:25
83+
|
84+
LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static.
85+
| -^^^^^^^---- help: consider removing `'static`: `&str`
86+
87+
error: Statics have by default a `'static` lifetime
88+
--> $DIR/const_static_lifetime.rs:30:29
89+
|
90+
LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
91+
| -^^^^^^^---- help: consider removing `'static`: `&str`
92+
93+
error: Statics have by default a `'static` lifetime
94+
--> $DIR/const_static_lifetime.rs:32:40
95+
|
96+
LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
97+
| -^^^^^^^---- help: consider removing `'static`: `&str`
98+
99+
error: Statics have by default a `'static` lifetime
100+
--> $DIR/const_static_lifetime.rs:32:55
101+
|
102+
LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
103+
| -^^^^^^^---- help: consider removing `'static`: `&str`
104+
105+
error: Statics have by default a `'static` lifetime
106+
--> $DIR/const_static_lifetime.rs:34:26
107+
|
108+
LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
109+
| -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]`
110+
111+
error: Statics have by default a `'static` lifetime
112+
--> $DIR/const_static_lifetime.rs:34:38
113+
|
114+
LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
115+
| -^^^^^^^---- help: consider removing `'static`: `&str`
116+
117+
error: Statics have by default a `'static` lifetime
118+
--> $DIR/const_static_lifetime.rs:36:25
119+
|
120+
LL | static STATIC_VAR_SIX: &'static u8 = &5;
121+
| -^^^^^^^--- help: consider removing `'static`: `&u8`
122+
123+
error: Statics have by default a `'static` lifetime
124+
--> $DIR/const_static_lifetime.rs:38:37
125+
|
126+
LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
127+
| -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]`
128+
129+
error: Statics have by default a `'static` lifetime
130+
--> $DIR/const_static_lifetime.rs:38:47
131+
|
132+
LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
133+
| -^^^^^^^---- help: consider removing `'static`: `&str`
134+
135+
error: Statics have by default a `'static` lifetime
136+
--> $DIR/const_static_lifetime.rs:40:28
137+
|
138+
LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {};
139+
| -^^^^^^^---- help: consider removing `'static`: `&Foo`
140+
141+
error: Statics have by default a `'static` lifetime
142+
--> $DIR/const_static_lifetime.rs:42:27
143+
|
144+
LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static.
145+
| -^^^^^^^----- help: consider removing `'static`: `&[u8]`
146+
147+
error: Statics have by default a `'static` lifetime
148+
--> $DIR/const_static_lifetime.rs:44:27
149+
|
150+
LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static.
151+
| -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)`
152+
153+
error: Statics have by default a `'static` lifetime
154+
--> $DIR/const_static_lifetime.rs:46:27
155+
|
156+
LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
157+
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
158+
159+
error: aborting due to 26 previous errors
82160

0 commit comments

Comments
 (0)