Skip to content

Commit 13e5ee4

Browse files
committed
Better linting : use of span_lint_and_then.
1 parent 2cdc309 commit 13e5ee4

File tree

2 files changed

+37
-44
lines changed

2 files changed

+37
-44
lines changed

clippy_lints/src/const_static_lifetime.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use syntax::ast::{Item, ItemKind, TyKind, Ty};
22
use rustc::lint::{LintPass, EarlyLintPass, LintArray, EarlyContext};
3-
use utils::{span_help_and_lint, in_macro};
3+
use utils::{span_lint_and_then, in_macro, snippet};
44

55
/// **What it does:** Checks for constants with an explicit `'static` lifetime.
66
///
7-
/// **Why is this bad?** Adding `'static` to every reference can create very complicated types.
7+
/// **Why is this bad?** Adding `'static` to every reference can create very
8+
/// complicated types.
89
///
910
/// **Known problems:** None.
1011
///
1112
/// **Example:**
1213
/// ```rust
13-
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = &[..]
14+
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
15+
/// &[...]
1416
/// ```
1517
/// This code can be rewritten as
1618
/// ```rust
@@ -52,11 +54,15 @@ impl StaticConst {
5254
if let TyKind::Path(_, _) = borrow_type.ty.node {
5355
// Verify that the path is a str
5456
if lifetime.ident.name == "'static" {
55-
span_help_and_lint(cx,
57+
let mut sug: String = String::new();
58+
// This should be ok, since we know that the current type is a borrow.
59+
sug.push('&');
60+
sug += &snippet(cx, borrow_type.ty.span, "<expr>").into_owned();
61+
span_lint_and_then(cx,
5662
CONST_STATIC_LIFETIME,
5763
lifetime.span,
5864
"Constants have by default a `'static` lifetime",
59-
"consider removing `'static`");
65+
|db| {db.span_suggestion(lifetime.span,"consider removing `'static`",sug);});
6066
}
6167
}
6268
}

tests/ui/const_static_lifetime.stderr

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,52 @@
1+
warning: running cargo clippy on a crate that also imports the clippy plugin
2+
13
error: Constants have by default a `'static` lifetime
2-
--> $DIR/const_static_lifetime.rs:6:17
4+
--> $DIR/const_static_lifetime.rs:7:17
35
|
4-
6 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static.
5-
| ^^^^^^^
6+
7 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static.
7+
| ^^^^^^^ help: consider removing `'static`: `&str`
68
|
79
= note: `-D const-static-lifetime` implied by `-D warnings`
8-
= help: consider removing `'static`
910

1011
error: Constants have by default a `'static` lifetime
11-
--> $DIR/const_static_lifetime.rs:10:21
12-
|
13-
10 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
14-
| ^^^^^^^
12+
--> $DIR/const_static_lifetime.rs:11:21
1513
|
16-
= help: consider removing `'static`
14+
11 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
15+
| ^^^^^^^ help: consider removing `'static`: `&str`
1716

1817
error: Constants have by default a `'static` lifetime
19-
--> $DIR/const_static_lifetime.rs:12:32
18+
--> $DIR/const_static_lifetime.rs:13:32
2019
|
21-
12 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
22-
| ^^^^^^^
23-
|
24-
= help: consider removing `'static`
20+
13 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
21+
| ^^^^^^^ help: consider removing `'static`: `&str`
2522

2623
error: Constants have by default a `'static` lifetime
27-
--> $DIR/const_static_lifetime.rs:12:47
28-
|
29-
12 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
30-
| ^^^^^^^
24+
--> $DIR/const_static_lifetime.rs:13:47
3125
|
32-
= help: consider removing `'static`
26+
13 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
27+
| ^^^^^^^ help: consider removing `'static`: `&str`
3328

3429
error: Constants have by default a `'static` lifetime
35-
--> $DIR/const_static_lifetime.rs:14:30
30+
--> $DIR/const_static_lifetime.rs:15:30
3631
|
37-
14 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
38-
| ^^^^^^^
39-
|
40-
= help: consider removing `'static`
32+
15 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
33+
| ^^^^^^^ help: consider removing `'static`: `&str`
4134

4235
error: Constants have by default a `'static` lifetime
43-
--> $DIR/const_static_lifetime.rs:16:17
44-
|
45-
16 | const VAR_SIX: &'static u8 = &5;
46-
| ^^^^^^^
36+
--> $DIR/const_static_lifetime.rs:17:17
4737
|
48-
= help: consider removing `'static`
38+
17 | const VAR_SIX: &'static u8 = &5;
39+
| ^^^^^^^ help: consider removing `'static`: `&u8`
4940

5041
error: Constants have by default a `'static` lifetime
51-
--> $DIR/const_static_lifetime.rs:18:39
42+
--> $DIR/const_static_lifetime.rs:19:39
5243
|
53-
18 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
54-
| ^^^^^^^
55-
|
56-
= help: consider removing `'static`
44+
19 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
45+
| ^^^^^^^ help: consider removing `'static`: `&str`
5746

5847
error: Constants have by default a `'static` lifetime
59-
--> $DIR/const_static_lifetime.rs:20:20
60-
|
61-
20 | const VAR_HEIGHT: &'static Foo = &Foo {};
62-
| ^^^^^^^
48+
--> $DIR/const_static_lifetime.rs:21:20
6349
|
64-
= help: consider removing `'static`
50+
21 | const VAR_HEIGHT: &'static Foo = &Foo {};
51+
| ^^^^^^^ help: consider removing `'static`: `&Foo`
6552

0 commit comments

Comments
 (0)