Skip to content

Commit 7a95c20

Browse files
committed
Auto merge of rust-lang#4162 - krk:static-static, r=flip1995
Add lint for statics with explicit static lifetime. changelog: Add lint for statics with explicit static lifetime, fixes rust-lang#4138.
2 parents bd39cea + 7e07d1b commit 7a95c20

8 files changed

+236
-134
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,6 @@ All notable changes to this project will be documented in this file.
881881
[`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned
882882
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
883883
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
884-
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
885884
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
886885
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
887886
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
@@ -1068,6 +1067,7 @@ All notable changes to this project will be documented in this file.
10681067
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
10691068
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
10701069
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
1070+
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
10711071
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
10721072
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
10731073
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts

clippy_lints/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ pub mod cargo_common_metadata;
157157
pub mod checked_conversions;
158158
pub mod cognitive_complexity;
159159
pub mod collapsible_if;
160-
pub mod const_static_lifetime;
161160
pub mod copies;
162161
pub mod copy_iterator;
163162
pub mod dbg_macro;
@@ -249,6 +248,7 @@ pub mod ranges;
249248
pub mod redundant_clone;
250249
pub mod redundant_field_names;
251250
pub mod redundant_pattern_matching;
251+
pub mod redundant_static_lifetimes;
252252
pub mod reference;
253253
pub mod regex;
254254
pub mod replace_consts;
@@ -553,7 +553,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
553553
reg.register_late_lint_pass(box invalid_ref::InvalidRef);
554554
reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default());
555555
reg.register_late_lint_pass(box types::ImplicitHasher);
556-
reg.register_early_lint_pass(box const_static_lifetime::StaticConst);
556+
reg.register_early_lint_pass(box redundant_static_lifetimes::RedundantStaticLifetimes);
557557
reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom);
558558
reg.register_late_lint_pass(box replace_consts::ReplaceConsts);
559559
reg.register_late_lint_pass(box types::UnitArg);
@@ -686,7 +686,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
686686
bytecount::NAIVE_BYTECOUNT,
687687
cognitive_complexity::COGNITIVE_COMPLEXITY,
688688
collapsible_if::COLLAPSIBLE_IF,
689-
const_static_lifetime::CONST_STATIC_LIFETIME,
690689
copies::IFS_SAME_COND,
691690
copies::IF_SAME_THEN_ELSE,
692691
derive::DERIVE_HASH_XOR_EQ,
@@ -834,6 +833,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
834833
ranges::RANGE_ZIP_WITH_LEN,
835834
redundant_field_names::REDUNDANT_FIELD_NAMES,
836835
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
836+
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
837837
reference::DEREF_ADDROF,
838838
reference::REF_IN_DEREF,
839839
regex::INVALID_REGEX,
@@ -901,7 +901,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
901901
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
902902
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
903903
collapsible_if::COLLAPSIBLE_IF,
904-
const_static_lifetime::CONST_STATIC_LIFETIME,
905904
enum_variants::ENUM_VARIANT_NAMES,
906905
enum_variants::MODULE_INCEPTION,
907906
eq_op::OP_REF,
@@ -957,6 +956,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
957956
question_mark::QUESTION_MARK,
958957
redundant_field_names::REDUNDANT_FIELD_NAMES,
959958
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
959+
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
960960
regex::REGEX_MACRO,
961961
regex::TRIVIAL_REGEX,
962962
returns::LET_AND_RETURN,
@@ -1153,6 +1153,7 @@ pub fn register_renamed(ls: &mut rustc::lint::LintStore) {
11531153
ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions");
11541154
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
11551155
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
1156+
ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes");
11561157
}
11571158

11581159
// only exists to let the dogfood integration test works.

clippy_lints/src/const_static_lifetime.rs renamed to clippy_lints/src/redundant_static_lifetimes.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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,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
}

tests/ui/const_static_lifetime.stderr

-82
This file was deleted.

tests/ui/const_static_lifetime.rs renamed to tests/ui/redundant_static_lifetimes.rs

+22
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);

0 commit comments

Comments
 (0)