Skip to content

Commit

Permalink
Better linting : use of span_lint_and_then.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbip committed Oct 20, 2017
1 parent fbce504 commit 4bbda68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
13 changes: 8 additions & 5 deletions clippy_lints/src/const_static_lifetime.rs
@@ -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
Expand Down Expand Up @@ -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);});
}
}
}
Expand Down
65 changes: 26 additions & 39 deletions tests/ui/const_static_lifetime.stderr
@@ -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
|
10 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
| ^^^^^^^
--> $DIR/const_static_lifetime.rs:11:21
|
= 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
|
12 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
| ^^^^^^^
--> $DIR/const_static_lifetime.rs:13:47
|
= 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
|
16 | const VAR_SIX: &'static u8 = &5;
| ^^^^^^^
--> $DIR/const_static_lifetime.rs:17:17
|
= 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
|
20 | const VAR_HEIGHT: &'static Foo = &Foo {};
| ^^^^^^^
--> $DIR/const_static_lifetime.rs:21:20
|
= help: consider removing `'static`
21 | const VAR_HEIGHT: &'static Foo = &Foo {};
| ^^^^^^^ help: consider removing `'static`: `&Foo`

0 comments on commit 4bbda68

Please sign in to comment.