From 16bd4796e998f7ef3c7eaecc60bf03349fdbb859 Mon Sep 17 00:00:00 2001 From: krk Date: Sat, 1 Jun 2019 12:10:15 +0200 Subject: [PATCH 1/9] Add lint for statics with explicit static lifetime. --- CHANGELOG.md | 1 + clippy_lints/src/lib.rs | 4 + clippy_lints/src/static_static_lifetime.rs | 93 ++++++++++++++++++++++ tests/ui/static_static_lifetime.rs | 49 ++++++++++++ tests/ui/static_static_lifetime.stderr | 82 +++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 clippy_lints/src/static_static_lifetime.rs create mode 100644 tests/ui/static_static_lifetime.rs create mode 100644 tests/ui/static_static_lifetime.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f34d24cd470..03c8be124c33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1088,6 +1088,7 @@ All notable changes to this project will be documented in this file. [`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match [`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization +[`static_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#static_static_lifetime [`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string [`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add [`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 2bec5cb2d015..0b1c76859690 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -256,6 +256,7 @@ pub mod returns; pub mod serde_api; pub mod shadow; pub mod slow_vector_initialization; +pub mod static_static_lifetime; pub mod strings; pub mod suspicious_trait_impl; pub mod swap; @@ -554,6 +555,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default()); reg.register_late_lint_pass(box types::ImplicitHasher); reg.register_early_lint_pass(box const_static_lifetime::StaticConst); + reg.register_early_lint_pass(box static_static_lifetime::StaticStatic); reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom); reg.register_late_lint_pass(box replace_consts::ReplaceConsts); reg.register_late_lint_pass(box types::UnitArg); @@ -844,6 +846,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { returns::UNUSED_UNIT, serde_api::SERDE_API_MISUSE, slow_vector_initialization::SLOW_VECTOR_INITIALIZATION, + static_static_lifetime::STATIC_STATIC_LIFETIME, strings::STRING_LIT_AS_BYTES, suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL, suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL, @@ -962,6 +965,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { returns::LET_AND_RETURN, returns::NEEDLESS_RETURN, returns::UNUSED_UNIT, + static_static_lifetime::STATIC_STATIC_LIFETIME, strings::STRING_LIT_AS_BYTES, types::FN_TO_NUMERIC_CAST, types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION, diff --git a/clippy_lints/src/static_static_lifetime.rs b/clippy_lints/src/static_static_lifetime.rs new file mode 100644 index 000000000000..338d9e766023 --- /dev/null +++ b/clippy_lints/src/static_static_lifetime.rs @@ -0,0 +1,93 @@ +use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; +use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; +use rustc::{declare_lint_pass, declare_tool_lint}; +use rustc_errors::Applicability; +use syntax::ast::*; + +declare_clippy_lint! { + /// **What it does:** Checks for statics with an explicit `'static` lifetime. + /// + /// **Why is this bad?** Adding `'static` to every reference can create very + /// complicated types. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// ```ignore + /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = + /// &[...] + /// ``` + /// This code can be rewritten as + /// ```ignore + /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] + /// ``` + pub STATIC_STATIC_LIFETIME, + style, + "Using explicit `'static` lifetime for statics when elision rules would allow omitting them." +} + +declare_lint_pass!(StaticStatic => [STATIC_STATIC_LIFETIME]); + +impl StaticStatic { + // Recursively visit types + fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { + match ty.node { + // Be careful of nested structures (arrays and tuples) + TyKind::Array(ref ty, _) => { + self.visit_type(&*ty, cx); + }, + TyKind::Tup(ref tup) => { + for tup_ty in tup { + self.visit_type(&*tup_ty, cx); + } + }, + // This is what we are looking for ! + TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { + // Match the 'static lifetime + if let Some(lifetime) = *optional_lifetime { + match borrow_type.ty.node { + TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { + if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { + let snip = snippet(cx, borrow_type.ty.span, ""); + let sugg = format!("&{}", snip); + span_lint_and_then( + cx, + STATIC_STATIC_LIFETIME, + lifetime.ident.span, + "Statics have by default a `'static` lifetime", + |db| { + db.span_suggestion( + ty.span, + "consider removing `'static`", + sugg, + Applicability::MachineApplicable, //snippet + ); + }, + ); + } + }, + _ => {}, + } + } + self.visit_type(&*borrow_type.ty, cx); + }, + TyKind::Slice(ref ty) => { + self.visit_type(ty, cx); + }, + _ => {}, + } + } +} + +impl EarlyLintPass for StaticStatic { + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + if !in_macro_or_desugar(item.span) { + // Match only statics... + if let ItemKind::Static(ref var_type, _, _) = item.node { + self.visit_type(var_type, cx); + } + } + } + + // Don't check associated consts because `'static` cannot be elided on those (issue #2438) +} diff --git a/tests/ui/static_static_lifetime.rs b/tests/ui/static_static_lifetime.rs new file mode 100644 index 000000000000..04fecbfaeb27 --- /dev/null +++ b/tests/ui/static_static_lifetime.rs @@ -0,0 +1,49 @@ +#[derive(Debug)] +struct Foo {} + +static VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. + +static VAR_TWO: &str = "Test static #2"; // This line should not raise a warning. + +static VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static + +static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + +static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + +static VAR_SIX: &'static u8 = &5; + +static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + +static VAR_HEIGHT: &'static Foo = &Foo {}; + +static VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. + +static VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. + +static VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. + +fn main() { + let false_positive: &'static str = "test"; + println!("{}", VAR_ONE); + println!("{}", VAR_TWO); + println!("{:?}", VAR_THREE); + println!("{:?}", VAR_FOUR); + println!("{:?}", VAR_FIVE); + println!("{:?}", VAR_SIX); + println!("{:?}", VAR_SEVEN); + println!("{:?}", VAR_HEIGHT); + println!("{}", false_positive); +} + +// trait Bar { +// static TRAIT_VAR: &'static str; +// } + +// impl Foo { +// static IMPL_VAR: &'static str = "var"; +// } + +// impl Bar for Foo { +// static TRAIT_VAR: &'static str = "foo"; +// } diff --git a/tests/ui/static_static_lifetime.stderr b/tests/ui/static_static_lifetime.stderr new file mode 100644 index 000000000000..c4111b1f13c7 --- /dev/null +++ b/tests/ui/static_static_lifetime.stderr @@ -0,0 +1,82 @@ +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:4:18 + | +LL | static VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. + | -^^^^^^^---- help: consider removing `'static`: `&str` + | + = note: `-D clippy::static-static-lifetime` implied by `-D warnings` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:8:22 + | +LL | static VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:10:33 + | +LL | static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:10:48 + | +LL | static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:12:19 + | +LL | static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:12:31 + | +LL | static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:14:18 + | +LL | static VAR_SIX: &'static u8 = &5; + | -^^^^^^^--- help: consider removing `'static`: `&u8` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:16:30 + | +LL | static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:16:40 + | +LL | static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:18:21 + | +LL | static VAR_HEIGHT: &'static Foo = &Foo {}; + | -^^^^^^^---- help: consider removing `'static`: `&Foo` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:20:20 + | +LL | static VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. + | -^^^^^^^----- help: consider removing `'static`: `&[u8]` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:22:20 + | +LL | static VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. + | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` + +error: Statics have by default a `'static` lifetime + --> $DIR/static_static_lifetime.rs:24:20 + | +LL | static VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. + | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` + +error: aborting due to 13 previous errors + From ff1b533c1383a6dbde3f1d3d5fe467b627281d09 Mon Sep 17 00:00:00 2001 From: krk Date: Mon, 10 Jun 2019 19:01:05 +0200 Subject: [PATCH 2/9] Move type-checking logic in StaticConst to RedundantStaticLifetime. --- clippy_lints/src/const_static_lifetime.rs | 52 ++--------------- clippy_lints/src/lib.rs | 1 + clippy_lints/src/redundant_static_lifetime.rs | 57 +++++++++++++++++++ 3 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 clippy_lints/src/redundant_static_lifetime.rs diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index fefd180953f2..a3f88114c393 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -1,7 +1,7 @@ -use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; +use crate::redundant_static_lifetime::RedundantStaticLifetime; +use crate::utils::in_macro_or_desugar; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; -use rustc_errors::Applicability; use syntax::ast::*; declare_clippy_lint! { @@ -31,51 +31,9 @@ declare_lint_pass!(StaticConst => [CONST_STATIC_LIFETIME]); impl StaticConst { // Recursively visit types fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { - match ty.node { - // Be careful of nested structures (arrays and tuples) - TyKind::Array(ref ty, _) => { - self.visit_type(&*ty, cx); - }, - TyKind::Tup(ref tup) => { - for tup_ty in tup { - self.visit_type(&*tup_ty, cx); - } - }, - // This is what we are looking for ! - TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { - // Match the 'static lifetime - if let Some(lifetime) = *optional_lifetime { - match borrow_type.ty.node { - TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { - if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { - let snip = snippet(cx, borrow_type.ty.span, ""); - 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, - Applicability::MachineApplicable, //snippet - ); - }, - ); - } - }, - _ => {}, - } - } - self.visit_type(&*borrow_type.ty, cx); - }, - TyKind::Slice(ref ty) => { - self.visit_type(ty, cx); - }, - _ => {}, - } + let mut rsl = + RedundantStaticLifetime::new(CONST_STATIC_LIFETIME, "Constants have by default a `'static` lifetime"); + rsl.visit_type(ty, cx) } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 0b1c76859690..563a8c196d56 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -141,6 +141,7 @@ macro_rules! declare_clippy_lint { mod consts; #[macro_use] mod utils; +mod redundant_static_lifetime; // begin lints modules, do not remove this comment, it’s used in `update_lints` pub mod approx_const; diff --git a/clippy_lints/src/redundant_static_lifetime.rs b/clippy_lints/src/redundant_static_lifetime.rs new file mode 100644 index 000000000000..efac8bfcc6ff --- /dev/null +++ b/clippy_lints/src/redundant_static_lifetime.rs @@ -0,0 +1,57 @@ +use crate::utils::{snippet, span_lint_and_then}; +use rustc::lint::{EarlyContext, Lint}; +use rustc_errors::Applicability; +use syntax::ast::*; + +pub struct RedundantStaticLifetime { + lint: &'static Lint, + reason: &'static str, +} + +impl RedundantStaticLifetime { + pub fn new(lint: &'static Lint, reason: &'static str) -> Self { + Self { lint, reason } + } + // Recursively visit types + pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { + match ty.node { + // Be careful of nested structures (arrays and tuples) + TyKind::Array(ref ty, _) => { + self.visit_type(&*ty, cx); + }, + TyKind::Tup(ref tup) => { + for tup_ty in tup { + self.visit_type(&*tup_ty, cx); + } + }, + // This is what we are looking for ! + TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { + // Match the 'static lifetime + if let Some(lifetime) = *optional_lifetime { + match borrow_type.ty.node { + TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { + if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { + let snip = snippet(cx, borrow_type.ty.span, ""); + let sugg = format!("&{}", snip); + span_lint_and_then(cx, self.lint, lifetime.ident.span, self.reason, |db| { + db.span_suggestion( + ty.span, + "consider removing `'static`", + sugg, + Applicability::MachineApplicable, //snippet + ); + }); + } + }, + _ => {}, + } + } + self.visit_type(&*borrow_type.ty, cx); + }, + TyKind::Slice(ref ty) => { + self.visit_type(ty, cx); + }, + _ => {}, + } + } +} From 87e9dee884988aa4385723ec08d69c548f7f50a2 Mon Sep 17 00:00:00 2001 From: krk Date: Mon, 10 Jun 2019 19:25:00 +0200 Subject: [PATCH 3/9] Use RedundantStaticLifetime in StaticStatic. --- clippy_lints/src/static_static_lifetime.rs | 54 ++-------------------- 1 file changed, 5 insertions(+), 49 deletions(-) diff --git a/clippy_lints/src/static_static_lifetime.rs b/clippy_lints/src/static_static_lifetime.rs index 338d9e766023..518cb5e7a7ac 100644 --- a/clippy_lints/src/static_static_lifetime.rs +++ b/clippy_lints/src/static_static_lifetime.rs @@ -1,7 +1,7 @@ -use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; +use crate::redundant_static_lifetime::RedundantStaticLifetime; +use crate::utils::in_macro_or_desugar; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; -use rustc_errors::Applicability; use syntax::ast::*; declare_clippy_lint! { @@ -31,51 +31,9 @@ declare_lint_pass!(StaticStatic => [STATIC_STATIC_LIFETIME]); impl StaticStatic { // Recursively visit types fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { - match ty.node { - // Be careful of nested structures (arrays and tuples) - TyKind::Array(ref ty, _) => { - self.visit_type(&*ty, cx); - }, - TyKind::Tup(ref tup) => { - for tup_ty in tup { - self.visit_type(&*tup_ty, cx); - } - }, - // This is what we are looking for ! - TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { - // Match the 'static lifetime - if let Some(lifetime) = *optional_lifetime { - match borrow_type.ty.node { - TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { - if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { - let snip = snippet(cx, borrow_type.ty.span, ""); - let sugg = format!("&{}", snip); - span_lint_and_then( - cx, - STATIC_STATIC_LIFETIME, - lifetime.ident.span, - "Statics have by default a `'static` lifetime", - |db| { - db.span_suggestion( - ty.span, - "consider removing `'static`", - sugg, - Applicability::MachineApplicable, //snippet - ); - }, - ); - } - }, - _ => {}, - } - } - self.visit_type(&*borrow_type.ty, cx); - }, - TyKind::Slice(ref ty) => { - self.visit_type(ty, cx); - }, - _ => {}, - } + let mut rsl = + RedundantStaticLifetime::new(STATIC_STATIC_LIFETIME, "Statics have by default a `'static` lifetime"); + rsl.visit_type(ty, cx) } } @@ -88,6 +46,4 @@ impl EarlyLintPass for StaticStatic { } } } - - // Don't check associated consts because `'static` cannot be elided on those (issue #2438) } From b38ce08e76a10ad0b7755ef6b149ddfd0c1cf811 Mon Sep 17 00:00:00 2001 From: krk Date: Tue, 11 Jun 2019 21:32:38 +0200 Subject: [PATCH 4/9] Merge StaticConst and StaticStatic lints into StaticConst. --- clippy_lints/src/const_static_lifetime.rs | 65 ++++++++++++--- clippy_lints/src/lib.rs | 5 -- clippy_lints/src/redundant_static_lifetime.rs | 57 ------------- clippy_lints/src/static_static_lifetime.rs | 49 ----------- tests/ui/const_static_lifetime.rs | 22 +++++ tests/ui/const_static_lifetime.stderr | 80 +++++++++++++++++- tests/ui/static_static_lifetime.rs | 49 ----------- tests/ui/static_static_lifetime.stderr | 82 ------------------- 8 files changed, 154 insertions(+), 255 deletions(-) delete mode 100644 clippy_lints/src/redundant_static_lifetime.rs delete mode 100644 clippy_lints/src/static_static_lifetime.rs delete mode 100644 tests/ui/static_static_lifetime.rs delete mode 100644 tests/ui/static_static_lifetime.stderr diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index a3f88114c393..b3eb2b96fc0d 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -1,11 +1,11 @@ -use crate::redundant_static_lifetime::RedundantStaticLifetime; -use crate::utils::in_macro_or_desugar; +use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; +use rustc_errors::Applicability; use syntax::ast::*; declare_clippy_lint! { - /// **What it does:** Checks for constants with an explicit `'static` lifetime. + /// **What it does:** Checks for constants and statics with an explicit `'static` lifetime. /// /// **Why is this bad?** Adding `'static` to every reference can create very /// complicated types. @@ -16,36 +16,77 @@ declare_clippy_lint! { /// ```ignore /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = /// &[...] + /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = + /// &[...] /// ``` /// This code can be rewritten as /// ```ignore /// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] + /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] /// ``` pub CONST_STATIC_LIFETIME, style, - "Using explicit `'static` lifetime for constants when elision rules would allow omitting them." + "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them." } declare_lint_pass!(StaticConst => [CONST_STATIC_LIFETIME]); impl StaticConst { // Recursively visit types - fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { - let mut rsl = - RedundantStaticLifetime::new(CONST_STATIC_LIFETIME, "Constants have by default a `'static` lifetime"); - rsl.visit_type(ty, cx) + pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) { + match ty.node { + // Be careful of nested structures (arrays and tuples) + TyKind::Array(ref ty, _) => { + self.visit_type(&*ty, cx, reason); + }, + TyKind::Tup(ref tup) => { + for tup_ty in tup { + self.visit_type(&*tup_ty, cx, reason); + } + }, + // This is what we are looking for ! + TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { + // Match the 'static lifetime + if let Some(lifetime) = *optional_lifetime { + match borrow_type.ty.node { + TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { + if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { + let snip = snippet(cx, borrow_type.ty.span, ""); + let sugg = format!("&{}", snip); + span_lint_and_then(cx, CONST_STATIC_LIFETIME, lifetime.ident.span, reason, |db| { + db.span_suggestion( + ty.span, + "consider removing `'static`", + sugg, + Applicability::MachineApplicable, //snippet + ); + }); + } + }, + _ => {}, + } + } + self.visit_type(&*borrow_type.ty, cx, reason); + }, + TyKind::Slice(ref ty) => { + self.visit_type(ty, cx, reason); + }, + _ => {}, + } } } impl EarlyLintPass for StaticConst { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { if !in_macro_or_desugar(item.span) { - // Match only constants... if let ItemKind::Const(ref var_type, _) = item.node { - self.visit_type(var_type, cx); + self.visit_type(var_type, cx, "Constants have by default a `'static` lifetime"); + // Don't check associated consts because `'static` cannot be elided on those (issue #2438) + } + + if let ItemKind::Static(ref var_type, _, _) = item.node { + self.visit_type(var_type, cx, "Statics have by default a `'static` lifetime"); } } } - - // Don't check associated consts because `'static` cannot be elided on those (issue #2438) } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 563a8c196d56..2bec5cb2d015 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -141,7 +141,6 @@ macro_rules! declare_clippy_lint { mod consts; #[macro_use] mod utils; -mod redundant_static_lifetime; // begin lints modules, do not remove this comment, it’s used in `update_lints` pub mod approx_const; @@ -257,7 +256,6 @@ pub mod returns; pub mod serde_api; pub mod shadow; pub mod slow_vector_initialization; -pub mod static_static_lifetime; pub mod strings; pub mod suspicious_trait_impl; pub mod swap; @@ -556,7 +554,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default()); reg.register_late_lint_pass(box types::ImplicitHasher); reg.register_early_lint_pass(box const_static_lifetime::StaticConst); - reg.register_early_lint_pass(box static_static_lifetime::StaticStatic); reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom); reg.register_late_lint_pass(box replace_consts::ReplaceConsts); reg.register_late_lint_pass(box types::UnitArg); @@ -847,7 +844,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { returns::UNUSED_UNIT, serde_api::SERDE_API_MISUSE, slow_vector_initialization::SLOW_VECTOR_INITIALIZATION, - static_static_lifetime::STATIC_STATIC_LIFETIME, strings::STRING_LIT_AS_BYTES, suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL, suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL, @@ -966,7 +962,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { returns::LET_AND_RETURN, returns::NEEDLESS_RETURN, returns::UNUSED_UNIT, - static_static_lifetime::STATIC_STATIC_LIFETIME, strings::STRING_LIT_AS_BYTES, types::FN_TO_NUMERIC_CAST, types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION, diff --git a/clippy_lints/src/redundant_static_lifetime.rs b/clippy_lints/src/redundant_static_lifetime.rs deleted file mode 100644 index efac8bfcc6ff..000000000000 --- a/clippy_lints/src/redundant_static_lifetime.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::utils::{snippet, span_lint_and_then}; -use rustc::lint::{EarlyContext, Lint}; -use rustc_errors::Applicability; -use syntax::ast::*; - -pub struct RedundantStaticLifetime { - lint: &'static Lint, - reason: &'static str, -} - -impl RedundantStaticLifetime { - pub fn new(lint: &'static Lint, reason: &'static str) -> Self { - Self { lint, reason } - } - // Recursively visit types - pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { - match ty.node { - // Be careful of nested structures (arrays and tuples) - TyKind::Array(ref ty, _) => { - self.visit_type(&*ty, cx); - }, - TyKind::Tup(ref tup) => { - for tup_ty in tup { - self.visit_type(&*tup_ty, cx); - } - }, - // This is what we are looking for ! - TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { - // Match the 'static lifetime - if let Some(lifetime) = *optional_lifetime { - match borrow_type.ty.node { - TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { - if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { - let snip = snippet(cx, borrow_type.ty.span, ""); - let sugg = format!("&{}", snip); - span_lint_and_then(cx, self.lint, lifetime.ident.span, self.reason, |db| { - db.span_suggestion( - ty.span, - "consider removing `'static`", - sugg, - Applicability::MachineApplicable, //snippet - ); - }); - } - }, - _ => {}, - } - } - self.visit_type(&*borrow_type.ty, cx); - }, - TyKind::Slice(ref ty) => { - self.visit_type(ty, cx); - }, - _ => {}, - } - } -} diff --git a/clippy_lints/src/static_static_lifetime.rs b/clippy_lints/src/static_static_lifetime.rs deleted file mode 100644 index 518cb5e7a7ac..000000000000 --- a/clippy_lints/src/static_static_lifetime.rs +++ /dev/null @@ -1,49 +0,0 @@ -use crate::redundant_static_lifetime::RedundantStaticLifetime; -use crate::utils::in_macro_or_desugar; -use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; -use rustc::{declare_lint_pass, declare_tool_lint}; -use syntax::ast::*; - -declare_clippy_lint! { - /// **What it does:** Checks for statics with an explicit `'static` lifetime. - /// - /// **Why is this bad?** Adding `'static` to every reference can create very - /// complicated types. - /// - /// **Known problems:** None. - /// - /// **Example:** - /// ```ignore - /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = - /// &[...] - /// ``` - /// This code can be rewritten as - /// ```ignore - /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] - /// ``` - pub STATIC_STATIC_LIFETIME, - style, - "Using explicit `'static` lifetime for statics when elision rules would allow omitting them." -} - -declare_lint_pass!(StaticStatic => [STATIC_STATIC_LIFETIME]); - -impl StaticStatic { - // Recursively visit types - fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) { - let mut rsl = - RedundantStaticLifetime::new(STATIC_STATIC_LIFETIME, "Statics have by default a `'static` lifetime"); - rsl.visit_type(ty, cx) - } -} - -impl EarlyLintPass for StaticStatic { - fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if !in_macro_or_desugar(item.span) { - // Match only statics... - if let ItemKind::Static(ref var_type, _, _) = item.node { - self.visit_type(var_type, cx); - } - } - } -} diff --git a/tests/ui/const_static_lifetime.rs b/tests/ui/const_static_lifetime.rs index 745821a1503c..fc9f0e066d43 100644 --- a/tests/ui/const_static_lifetime.rs +++ b/tests/ui/const_static_lifetime.rs @@ -23,6 +23,28 @@ const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. +static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. + +static STATIC_VAR_TWO: &str = "Test static #2"; // This line should not raise a warning. + +static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static + +static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + +static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + +static STATIC_VAR_SIX: &'static u8 = &5; + +static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + +static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; + +static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. + +static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. + +static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. + fn main() { let false_positive: &'static str = "test"; println!("{}", VAR_ONE); diff --git a/tests/ui/const_static_lifetime.stderr b/tests/ui/const_static_lifetime.stderr index c329e860558e..eb4d3200e8be 100644 --- a/tests/ui/const_static_lifetime.stderr +++ b/tests/ui/const_static_lifetime.stderr @@ -78,5 +78,83 @@ error: Constants have by default a `'static` lifetime LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` -error: aborting due to 13 previous errors +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:26:25 + | +LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:30:29 + | +LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:32:40 + | +LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:32:55 + | +LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:34:26 + | +LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:34:38 + | +LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:36:25 + | +LL | static STATIC_VAR_SIX: &'static u8 = &5; + | -^^^^^^^--- help: consider removing `'static`: `&u8` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:38:37 + | +LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:38:47 + | +LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; + | -^^^^^^^---- help: consider removing `'static`: `&str` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:40:28 + | +LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; + | -^^^^^^^---- help: consider removing `'static`: `&Foo` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:42:27 + | +LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. + | -^^^^^^^----- help: consider removing `'static`: `&[u8]` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:44:27 + | +LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. + | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` + +error: Statics have by default a `'static` lifetime + --> $DIR/const_static_lifetime.rs:46:27 + | +LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. + | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` + +error: aborting due to 26 previous errors diff --git a/tests/ui/static_static_lifetime.rs b/tests/ui/static_static_lifetime.rs deleted file mode 100644 index 04fecbfaeb27..000000000000 --- a/tests/ui/static_static_lifetime.rs +++ /dev/null @@ -1,49 +0,0 @@ -#[derive(Debug)] -struct Foo {} - -static VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. - -static VAR_TWO: &str = "Test static #2"; // This line should not raise a warning. - -static VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static - -static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static - -static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static - -static VAR_SIX: &'static u8 = &5; - -static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; - -static VAR_HEIGHT: &'static Foo = &Foo {}; - -static VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. - -static VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. - -static VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. - -fn main() { - let false_positive: &'static str = "test"; - println!("{}", VAR_ONE); - println!("{}", VAR_TWO); - println!("{:?}", VAR_THREE); - println!("{:?}", VAR_FOUR); - println!("{:?}", VAR_FIVE); - println!("{:?}", VAR_SIX); - println!("{:?}", VAR_SEVEN); - println!("{:?}", VAR_HEIGHT); - println!("{}", false_positive); -} - -// trait Bar { -// static TRAIT_VAR: &'static str; -// } - -// impl Foo { -// static IMPL_VAR: &'static str = "var"; -// } - -// impl Bar for Foo { -// static TRAIT_VAR: &'static str = "foo"; -// } diff --git a/tests/ui/static_static_lifetime.stderr b/tests/ui/static_static_lifetime.stderr deleted file mode 100644 index c4111b1f13c7..000000000000 --- a/tests/ui/static_static_lifetime.stderr +++ /dev/null @@ -1,82 +0,0 @@ -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:4:18 - | -LL | static VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. - | -^^^^^^^---- help: consider removing `'static`: `&str` - | - = note: `-D clippy::static-static-lifetime` implied by `-D warnings` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:8:22 - | -LL | static VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static - | -^^^^^^^---- help: consider removing `'static`: `&str` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:10:33 - | -LL | static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static - | -^^^^^^^---- help: consider removing `'static`: `&str` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:10:48 - | -LL | static VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static - | -^^^^^^^---- help: consider removing `'static`: `&str` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:12:19 - | -LL | static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static - | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:12:31 - | -LL | static VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static - | -^^^^^^^---- help: consider removing `'static`: `&str` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:14:18 - | -LL | static VAR_SIX: &'static u8 = &5; - | -^^^^^^^--- help: consider removing `'static`: `&u8` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:16:30 - | -LL | static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; - | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:16:40 - | -LL | static VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; - | -^^^^^^^---- help: consider removing `'static`: `&str` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:18:21 - | -LL | static VAR_HEIGHT: &'static Foo = &Foo {}; - | -^^^^^^^---- help: consider removing `'static`: `&Foo` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:20:20 - | -LL | static VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. - | -^^^^^^^----- help: consider removing `'static`: `&[u8]` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:22:20 - | -LL | static VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. - | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` - -error: Statics have by default a `'static` lifetime - --> $DIR/static_static_lifetime.rs:24:20 - | -LL | static VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. - | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` - -error: aborting due to 13 previous errors - From 637e92d44eab06ab1e33774d858f0d9938a54833 Mon Sep 17 00:00:00 2001 From: krk Date: Tue, 11 Jun 2019 21:52:18 +0200 Subject: [PATCH 5/9] Rename const_static_lifetime to redundant_static_lifetime. --- CHANGELOG.md | 3 +- clippy_lints/src/lib.rs | 8 +-- ...fetime.rs => redundant_static_lifetime.rs} | 10 ++-- ...fetime.rs => redundant_static_lifetime.rs} | 0 ...tderr => redundant_static_lifetime.stderr} | 54 +++++++++---------- 5 files changed, 37 insertions(+), 38 deletions(-) rename clippy_lints/src/{const_static_lifetime.rs => redundant_static_lifetime.rs} (91%) rename tests/ui/{const_static_lifetime.rs => redundant_static_lifetime.rs} (100%) rename tests/ui/{const_static_lifetime.stderr => redundant_static_lifetime.stderr} (83%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c8be124c33..4f8c562c826b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -881,7 +881,6 @@ All notable changes to this project will be documented in this file. [`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned [`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity [`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if -[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime [`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator [`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute [`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. [`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names [`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern [`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching +[`redundant_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetime [`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref [`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro [`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts @@ -1088,7 +1088,6 @@ All notable changes to this project will be documented in this file. [`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match [`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization -[`static_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#static_static_lifetime [`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string [`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add [`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 2bec5cb2d015..8a56e04973c9 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -157,7 +157,6 @@ pub mod cargo_common_metadata; pub mod checked_conversions; pub mod cognitive_complexity; pub mod collapsible_if; -pub mod const_static_lifetime; pub mod copies; pub mod copy_iterator; pub mod dbg_macro; @@ -249,6 +248,7 @@ pub mod ranges; pub mod redundant_clone; pub mod redundant_field_names; pub mod redundant_pattern_matching; +pub mod redundant_static_lifetime; pub mod reference; pub mod regex; pub mod replace_consts; @@ -553,7 +553,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box invalid_ref::InvalidRef); reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default()); reg.register_late_lint_pass(box types::ImplicitHasher); - reg.register_early_lint_pass(box const_static_lifetime::StaticConst); + reg.register_early_lint_pass(box redundant_static_lifetime::RedundantStaticLifetime); reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom); reg.register_late_lint_pass(box replace_consts::ReplaceConsts); reg.register_late_lint_pass(box types::UnitArg); @@ -686,7 +686,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { bytecount::NAIVE_BYTECOUNT, cognitive_complexity::COGNITIVE_COMPLEXITY, collapsible_if::COLLAPSIBLE_IF, - const_static_lifetime::CONST_STATIC_LIFETIME, copies::IFS_SAME_COND, copies::IF_SAME_THEN_ELSE, derive::DERIVE_HASH_XOR_EQ, @@ -834,6 +833,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { ranges::RANGE_ZIP_WITH_LEN, redundant_field_names::REDUNDANT_FIELD_NAMES, redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING, + redundant_static_lifetime::REDUNDANT_STATIC_LIFETIME, reference::DEREF_ADDROF, reference::REF_IN_DEREF, regex::INVALID_REGEX, @@ -901,7 +901,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR, block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT, collapsible_if::COLLAPSIBLE_IF, - const_static_lifetime::CONST_STATIC_LIFETIME, enum_variants::ENUM_VARIANT_NAMES, enum_variants::MODULE_INCEPTION, eq_op::OP_REF, @@ -957,6 +956,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { question_mark::QUESTION_MARK, redundant_field_names::REDUNDANT_FIELD_NAMES, redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING, + redundant_static_lifetime::REDUNDANT_STATIC_LIFETIME, regex::REGEX_MACRO, regex::TRIVIAL_REGEX, returns::LET_AND_RETURN, diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/redundant_static_lifetime.rs similarity index 91% rename from clippy_lints/src/const_static_lifetime.rs rename to clippy_lints/src/redundant_static_lifetime.rs index b3eb2b96fc0d..2e49fdef5012 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/redundant_static_lifetime.rs @@ -24,14 +24,14 @@ declare_clippy_lint! { /// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] /// ``` - pub CONST_STATIC_LIFETIME, + pub REDUNDANT_STATIC_LIFETIME, style, "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them." } -declare_lint_pass!(StaticConst => [CONST_STATIC_LIFETIME]); +declare_lint_pass!(RedundantStaticLifetime => [REDUNDANT_STATIC_LIFETIME]); -impl StaticConst { +impl RedundantStaticLifetime { // Recursively visit types pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) { match ty.node { @@ -53,7 +53,7 @@ impl StaticConst { if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { let snip = snippet(cx, borrow_type.ty.span, ""); let sugg = format!("&{}", snip); - span_lint_and_then(cx, CONST_STATIC_LIFETIME, lifetime.ident.span, reason, |db| { + span_lint_and_then(cx, REDUNDANT_STATIC_LIFETIME, lifetime.ident.span, reason, |db| { db.span_suggestion( ty.span, "consider removing `'static`", @@ -76,7 +76,7 @@ impl StaticConst { } } -impl EarlyLintPass for StaticConst { +impl EarlyLintPass for RedundantStaticLifetime { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { if !in_macro_or_desugar(item.span) { if let ItemKind::Const(ref var_type, _) = item.node { diff --git a/tests/ui/const_static_lifetime.rs b/tests/ui/redundant_static_lifetime.rs similarity index 100% rename from tests/ui/const_static_lifetime.rs rename to tests/ui/redundant_static_lifetime.rs diff --git a/tests/ui/const_static_lifetime.stderr b/tests/ui/redundant_static_lifetime.stderr similarity index 83% rename from tests/ui/const_static_lifetime.stderr rename to tests/ui/redundant_static_lifetime.stderr index eb4d3200e8be..10baf306500d 100644 --- a/tests/ui/const_static_lifetime.stderr +++ b/tests/ui/redundant_static_lifetime.stderr @@ -1,157 +1,157 @@ error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:4:17 + --> $DIR/redundant_static_lifetime.rs:4:17 | LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` | - = note: `-D clippy::const-static-lifetime` implied by `-D warnings` + = note: `-D clippy::redundant-static-lifetime` implied by `-D warnings` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:8:21 + --> $DIR/redundant_static_lifetime.rs:8:21 | LL | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:10:32 + --> $DIR/redundant_static_lifetime.rs:10:32 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:10:47 + --> $DIR/redundant_static_lifetime.rs:10:47 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:12:18 + --> $DIR/redundant_static_lifetime.rs:12:18 | LL | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:12:30 + --> $DIR/redundant_static_lifetime.rs:12:30 | LL | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:14:17 + --> $DIR/redundant_static_lifetime.rs:14:17 | LL | const VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:16:29 + --> $DIR/redundant_static_lifetime.rs:16:29 | LL | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:16:39 + --> $DIR/redundant_static_lifetime.rs:16:39 | LL | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:18:20 + --> $DIR/redundant_static_lifetime.rs:18:20 | LL | const VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:20:19 + --> $DIR/redundant_static_lifetime.rs:20:19 | LL | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:22:19 + --> $DIR/redundant_static_lifetime.rs:22:19 | LL | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: Constants have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:24:19 + --> $DIR/redundant_static_lifetime.rs:24:19 | LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:26:25 + --> $DIR/redundant_static_lifetime.rs:26:25 | LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:30:29 + --> $DIR/redundant_static_lifetime.rs:30:29 | LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:32:40 + --> $DIR/redundant_static_lifetime.rs:32:40 | LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:32:55 + --> $DIR/redundant_static_lifetime.rs:32:55 | LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:34:26 + --> $DIR/redundant_static_lifetime.rs:34:26 | LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:34:38 + --> $DIR/redundant_static_lifetime.rs:34:38 | LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:36:25 + --> $DIR/redundant_static_lifetime.rs:36:25 | LL | static STATIC_VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:38:37 + --> $DIR/redundant_static_lifetime.rs:38:37 | LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:38:47 + --> $DIR/redundant_static_lifetime.rs:38:47 | LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:40:28 + --> $DIR/redundant_static_lifetime.rs:40:28 | LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:42:27 + --> $DIR/redundant_static_lifetime.rs:42:27 | LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:44:27 + --> $DIR/redundant_static_lifetime.rs:44:27 | LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: Statics have by default a `'static` lifetime - --> $DIR/const_static_lifetime.rs:46:27 + --> $DIR/redundant_static_lifetime.rs:46:27 | LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` From 3b1080542b3c9e94f5bf0c6ca19d84d79123327f Mon Sep 17 00:00:00 2001 From: Kerem Date: Wed, 12 Jun 2019 19:57:49 +0200 Subject: [PATCH 6/9] Remove pub from RedundantStaticLifetime.visit_type function. Co-Authored-By: Philipp Krones --- clippy_lints/src/redundant_static_lifetime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/redundant_static_lifetime.rs b/clippy_lints/src/redundant_static_lifetime.rs index 2e49fdef5012..3aa95e7978cf 100644 --- a/clippy_lints/src/redundant_static_lifetime.rs +++ b/clippy_lints/src/redundant_static_lifetime.rs @@ -33,7 +33,7 @@ declare_lint_pass!(RedundantStaticLifetime => [REDUNDANT_STATIC_LIFETIME]); impl RedundantStaticLifetime { // Recursively visit types - pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) { + fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) { match ty.node { // Be careful of nested structures (arrays and tuples) TyKind::Array(ref ty, _) => { From 55740219b0203fadefc1513ffe393a69a04b66b2 Mon Sep 17 00:00:00 2001 From: krk Date: Thu, 13 Jun 2019 23:47:06 +0200 Subject: [PATCH 7/9] Rename REDUNDANT_STATIC_LIFETIME to REDUNDANT_STATIC_LIFETIMES. --- CHANGELOG.md | 2 +- clippy_lints/src/lib.rs | 8 +-- ...etime.rs => redundant_static_lifetimes.rs} | 10 ++-- ...etime.rs => redundant_static_lifetimes.rs} | 0 ...derr => redundant_static_lifetimes.stderr} | 54 +++++++++---------- 5 files changed, 37 insertions(+), 37 deletions(-) rename clippy_lints/src/{redundant_static_lifetime.rs => redundant_static_lifetimes.rs} (93%) rename tests/ui/{redundant_static_lifetime.rs => redundant_static_lifetimes.rs} (100%) rename tests/ui/{redundant_static_lifetime.stderr => redundant_static_lifetimes.stderr} (83%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f8c562c826b..d40eb9513f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1067,7 +1067,7 @@ All notable changes to this project will be documented in this file. [`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names [`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern [`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching -[`redundant_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetime +[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes [`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref [`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro [`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 8a56e04973c9..5ffa71722104 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -248,7 +248,7 @@ pub mod ranges; pub mod redundant_clone; pub mod redundant_field_names; pub mod redundant_pattern_matching; -pub mod redundant_static_lifetime; +pub mod redundant_static_lifetimes; pub mod reference; pub mod regex; pub mod replace_consts; @@ -553,7 +553,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box invalid_ref::InvalidRef); reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default()); reg.register_late_lint_pass(box types::ImplicitHasher); - reg.register_early_lint_pass(box redundant_static_lifetime::RedundantStaticLifetime); + reg.register_early_lint_pass(box redundant_static_lifetimes::RedundantStaticLifetimes); reg.register_late_lint_pass(box fallible_impl_from::FallibleImplFrom); reg.register_late_lint_pass(box replace_consts::ReplaceConsts); reg.register_late_lint_pass(box types::UnitArg); @@ -833,7 +833,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { ranges::RANGE_ZIP_WITH_LEN, redundant_field_names::REDUNDANT_FIELD_NAMES, redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING, - redundant_static_lifetime::REDUNDANT_STATIC_LIFETIME, + redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES, reference::DEREF_ADDROF, reference::REF_IN_DEREF, regex::INVALID_REGEX, @@ -956,7 +956,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { question_mark::QUESTION_MARK, redundant_field_names::REDUNDANT_FIELD_NAMES, redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING, - redundant_static_lifetime::REDUNDANT_STATIC_LIFETIME, + redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES, regex::REGEX_MACRO, regex::TRIVIAL_REGEX, returns::LET_AND_RETURN, diff --git a/clippy_lints/src/redundant_static_lifetime.rs b/clippy_lints/src/redundant_static_lifetimes.rs similarity index 93% rename from clippy_lints/src/redundant_static_lifetime.rs rename to clippy_lints/src/redundant_static_lifetimes.rs index 3aa95e7978cf..bdd48fe86461 100644 --- a/clippy_lints/src/redundant_static_lifetime.rs +++ b/clippy_lints/src/redundant_static_lifetimes.rs @@ -24,14 +24,14 @@ declare_clippy_lint! { /// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] /// static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] /// ``` - pub REDUNDANT_STATIC_LIFETIME, + pub REDUNDANT_STATIC_LIFETIMES, style, "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them." } -declare_lint_pass!(RedundantStaticLifetime => [REDUNDANT_STATIC_LIFETIME]); +declare_lint_pass!(RedundantStaticLifetimes => [REDUNDANT_STATIC_LIFETIMES]); -impl RedundantStaticLifetime { +impl RedundantStaticLifetimes { // Recursively visit types fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) { match ty.node { @@ -53,7 +53,7 @@ impl RedundantStaticLifetime { if lifetime.ident.name == syntax::symbol::kw::StaticLifetime { let snip = snippet(cx, borrow_type.ty.span, ""); let sugg = format!("&{}", snip); - span_lint_and_then(cx, REDUNDANT_STATIC_LIFETIME, lifetime.ident.span, reason, |db| { + span_lint_and_then(cx, REDUNDANT_STATIC_LIFETIMES, lifetime.ident.span, reason, |db| { db.span_suggestion( ty.span, "consider removing `'static`", @@ -76,7 +76,7 @@ impl RedundantStaticLifetime { } } -impl EarlyLintPass for RedundantStaticLifetime { +impl EarlyLintPass for RedundantStaticLifetimes { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { if !in_macro_or_desugar(item.span) { if let ItemKind::Const(ref var_type, _) = item.node { diff --git a/tests/ui/redundant_static_lifetime.rs b/tests/ui/redundant_static_lifetimes.rs similarity index 100% rename from tests/ui/redundant_static_lifetime.rs rename to tests/ui/redundant_static_lifetimes.rs diff --git a/tests/ui/redundant_static_lifetime.stderr b/tests/ui/redundant_static_lifetimes.stderr similarity index 83% rename from tests/ui/redundant_static_lifetime.stderr rename to tests/ui/redundant_static_lifetimes.stderr index 10baf306500d..da6175d1debc 100644 --- a/tests/ui/redundant_static_lifetime.stderr +++ b/tests/ui/redundant_static_lifetimes.stderr @@ -1,157 +1,157 @@ error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:4:17 + --> $DIR/redundant_static_lifetimes.rs:4:17 | LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` | - = note: `-D clippy::redundant-static-lifetime` implied by `-D warnings` + = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:8:21 + --> $DIR/redundant_static_lifetimes.rs:8:21 | LL | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:10:32 + --> $DIR/redundant_static_lifetimes.rs:10:32 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:10:47 + --> $DIR/redundant_static_lifetimes.rs:10:47 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:12:18 + --> $DIR/redundant_static_lifetimes.rs:12:18 | LL | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:12:30 + --> $DIR/redundant_static_lifetimes.rs:12:30 | LL | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:14:17 + --> $DIR/redundant_static_lifetimes.rs:14:17 | LL | const VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:16:29 + --> $DIR/redundant_static_lifetimes.rs:16:29 | LL | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:16:39 + --> $DIR/redundant_static_lifetimes.rs:16:39 | LL | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:18:20 + --> $DIR/redundant_static_lifetimes.rs:18:20 | LL | const VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:20:19 + --> $DIR/redundant_static_lifetimes.rs:20:19 | LL | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:22:19 + --> $DIR/redundant_static_lifetimes.rs:22:19 | LL | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: Constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:24:19 + --> $DIR/redundant_static_lifetimes.rs:24:19 | LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:26:25 + --> $DIR/redundant_static_lifetimes.rs:26:25 | LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:30:29 + --> $DIR/redundant_static_lifetimes.rs:30:29 | LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:32:40 + --> $DIR/redundant_static_lifetimes.rs:32:40 | LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:32:55 + --> $DIR/redundant_static_lifetimes.rs:32:55 | LL | static STATIC_VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:34:26 + --> $DIR/redundant_static_lifetimes.rs:34:26 | LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:34:38 + --> $DIR/redundant_static_lifetimes.rs:34:38 | LL | static STATIC_VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:36:25 + --> $DIR/redundant_static_lifetimes.rs:36:25 | LL | static STATIC_VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:38:37 + --> $DIR/redundant_static_lifetimes.rs:38:37 | LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:38:47 + --> $DIR/redundant_static_lifetimes.rs:38:47 | LL | static STATIC_VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; | -^^^^^^^---- help: consider removing `'static`: `&str` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:40:28 + --> $DIR/redundant_static_lifetimes.rs:40:28 | LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:42:27 + --> $DIR/redundant_static_lifetimes.rs:42:27 | LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:44:27 + --> $DIR/redundant_static_lifetimes.rs:44:27 | LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: Statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetime.rs:46:27 + --> $DIR/redundant_static_lifetimes.rs:46:27 | LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` From fefa7e7f6717fa761b7033b29b14ca30e5da393d Mon Sep 17 00:00:00 2001 From: krk Date: Thu, 13 Jun 2019 23:54:05 +0200 Subject: [PATCH 8/9] Register rename to the LintStore. --- clippy_lints/src/lib.rs | 1 + tests/ui/rename.rs | 3 +++ tests/ui/rename.stderr | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 5ffa71722104..cfd6070356d1 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1153,6 +1153,7 @@ pub fn register_renamed(ls: &mut rustc::lint::LintStore) { ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions"); ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default"); ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity"); + ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes"); } // only exists to let the dogfood integration test works. diff --git a/tests/ui/rename.rs b/tests/ui/rename.rs index b1846a1096cb..1e25d1812c6e 100644 --- a/tests/ui/rename.rs +++ b/tests/ui/rename.rs @@ -7,6 +7,9 @@ fn main() {} #[warn(clippy::new_without_default_derive)] struct Foo; +#[warn(clippy::const_static_lifetime)] +static Bar: &'static str = "baz"; + impl Foo { fn new() -> Self { Foo diff --git a/tests/ui/rename.stderr b/tests/ui/rename.stderr index ac850b60d972..6fcf0e299243 100644 --- a/tests/ui/rename.stderr +++ b/tests/ui/rename.stderr @@ -26,6 +26,12 @@ error: lint `clippy::new_without_default_derive` has been renamed to `clippy::ne LL | #[warn(clippy::new_without_default_derive)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default` +error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes` + --> $DIR/rename.rs:10:8 + | +LL | #[warn(clippy::const_static_lifetime)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes` + error: unknown lint: `stutter` --> $DIR/rename.rs:1:10 | @@ -38,5 +44,13 @@ error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cogniti LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` -error: aborting due to 6 previous errors +error: Statics have by default a `'static` lifetime + --> $DIR/rename.rs:11:14 + | +LL | static Bar: &'static str = "baz"; + | -^^^^^^^---- help: consider removing `'static`: `&str` + | + = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings` + +error: aborting due to 8 previous errors From 7e07d1bed84e509fc39cf2bf8aa6892a22b1a5fc Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 14 Jun 2019 09:54:34 +0200 Subject: [PATCH 9/9] Restructure rename tests --- tests/ui/rename.rs | 9 ++++++++- tests/ui/rename.stderr | 34 ++++++---------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/tests/ui/rename.rs b/tests/ui/rename.rs index 1e25d1812c6e..f3758174125a 100644 --- a/tests/ui/rename.rs +++ b/tests/ui/rename.rs @@ -1,4 +1,11 @@ -#![allow(stutter)] +//! Test for Clippy lint renames. + +// allow the new lint name here, to test if the new name works +#![allow(clippy::module_name_repetitions)] +#![allow(clippy::new_without_default)] +#![allow(clippy::cognitive_complexity)] +#![allow(clippy::redundant_static_lifetimes)] +// warn for the old lint name here, to test if the renaming worked #![warn(clippy::cyclomatic_complexity)] #[warn(clippy::stutter)] diff --git a/tests/ui/rename.stderr b/tests/ui/rename.stderr index 6fcf0e299243..0765e6e954a1 100644 --- a/tests/ui/rename.stderr +++ b/tests/ui/rename.stderr @@ -1,13 +1,5 @@ -error: unknown lint: `stutter` - --> $DIR/rename.rs:1:10 - | -LL | #![allow(stutter)] - | ^^^^^^^ - | - = note: `-D unknown-lints` implied by `-D warnings` - error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity` - --> $DIR/rename.rs:2:9 + --> $DIR/rename.rs:9:9 | LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` @@ -15,42 +7,28 @@ LL | #![warn(clippy::cyclomatic_complexity)] = note: `-D renamed-and-removed-lints` implied by `-D warnings` error: lint `clippy::stutter` has been renamed to `clippy::module_name_repetitions` - --> $DIR/rename.rs:4:8 + --> $DIR/rename.rs:11:8 | LL | #[warn(clippy::stutter)] | ^^^^^^^^^^^^^^^ help: use the new name: `clippy::module_name_repetitions` error: lint `clippy::new_without_default_derive` has been renamed to `clippy::new_without_default` - --> $DIR/rename.rs:7:8 + --> $DIR/rename.rs:14:8 | LL | #[warn(clippy::new_without_default_derive)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default` error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes` - --> $DIR/rename.rs:10:8 + --> $DIR/rename.rs:17:8 | LL | #[warn(clippy::const_static_lifetime)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes` -error: unknown lint: `stutter` - --> $DIR/rename.rs:1:10 - | -LL | #![allow(stutter)] - | ^^^^^^^ - error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity` - --> $DIR/rename.rs:2:9 + --> $DIR/rename.rs:9:9 | LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` -error: Statics have by default a `'static` lifetime - --> $DIR/rename.rs:11:14 - | -LL | static Bar: &'static str = "baz"; - | -^^^^^^^---- help: consider removing `'static`: `&str` - | - = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings` - -error: aborting due to 8 previous errors +error: aborting due to 5 previous errors