Better linting : use of span_lint_and_then.

This commit is contained in:
Paul Florence 2017-10-20 10:00:44 -04:00
parent fbce504664
commit 4bbda68d56
2 changed files with 34 additions and 44 deletions

View file

@ -1,16 +1,18 @@
use syntax::ast::{Item, ItemKind, TyKind, Ty};
use rustc::lint::{LintPass, EarlyLintPass, LintArray, EarlyContext};
use utils::{span_help_and_lint, in_macro};
use utils::{span_lint_and_then, in_macro};
/// **What it does:** Checks for constants with an explicit `'static` lifetime.
///
/// **Why is this bad?** Adding `'static` to every reference can create very complicated types.
/// **Why is this bad?** Adding `'static` to every reference can create very
/// complicated types.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = &[..]
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
/// &[...]
/// ```
/// This code can be rewritten as
/// ```rust
@ -52,11 +54,12 @@ impl StaticConst {
if let TyKind::Path(_, _) = borrow_type.ty.node {
// Verify that the path is a str
if lifetime.ident.name == "'static" {
span_help_and_lint(cx,
let mut sug: String = String::new();
span_lint_and_then(cx,
CONST_STATIC_LIFETIME,
lifetime.span,
"Constants have by default a `'static` lifetime",
"consider removing `'static`");
|db| {db.span_suggestion(lifetime.span,"consider removing `'static`",sug);});
}
}
}

View file

@ -1,65 +1,52 @@
warning: running cargo clippy on a crate that also imports the clippy plugin
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:6:17
--> $DIR/const_static_lifetime.rs:7:17
|
6 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static.
| ^^^^^^^
7 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static.
| ^^^^^^^ help: consider removing `'static`: `&str`
|
= note: `-D const-static-lifetime` implied by `-D warnings`
= help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:10:21
--> $DIR/const_static_lifetime.rs:11:21
|
10 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
| ^^^^^^^
|
= help: consider removing `'static`
11 | 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:12:32
--> $DIR/const_static_lifetime.rs:13:32
|
12 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
| ^^^^^^^
|
= help: consider removing `'static`
13 | 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:47
--> $DIR/const_static_lifetime.rs:13:47
|
12 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
| ^^^^^^^
|
= help: consider removing `'static`
13 | 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:14:30
--> $DIR/const_static_lifetime.rs:15:30
|
14 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
| ^^^^^^^
|
= help: consider removing `'static`
15 | 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:16:17
--> $DIR/const_static_lifetime.rs:17:17
|
16 | const VAR_SIX: &'static u8 = &5;
| ^^^^^^^
|
= help: consider removing `'static`
17 | const VAR_SIX: &'static u8 = &5;
| ^^^^^^^ help: consider removing `'static`: `&u8`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:18:39
--> $DIR/const_static_lifetime.rs:19:39
|
18 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
| ^^^^^^^
|
= help: consider removing `'static`
19 | 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:20:20
--> $DIR/const_static_lifetime.rs:21:20
|
20 | const VAR_HEIGHT: &'static Foo = &Foo {};
| ^^^^^^^
|
= help: consider removing `'static`
21 | const VAR_HEIGHT: &'static Foo = &Foo {};
| ^^^^^^^ help: consider removing `'static`: `&Foo`