Merge StaticConst and StaticStatic lints into StaticConst.

This commit is contained in:
krk 2019-06-11 21:32:38 +02:00 committed by flip1995
parent 87e9dee884
commit b38ce08e76
No known key found for this signature in database
GPG Key ID: 693086869D506637
8 changed files with 154 additions and 255 deletions

View File

@ -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, "<type>");
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)
}

View File

@ -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, its 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,

View File

@ -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, "<type>");
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);
},
_ => {},
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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);

View File

@ -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

View File

@ -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";
// }

View File

@ -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