Skip to content

Commit 37f62a5

Browse files
committed
Show wider and more accurate suggestion for const_static_lifetime
fixes #2365
1 parent 61e2b7a commit 37f62a5

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

clippy_lints/src/const_static_lifetime.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syntax::ast::{Item, ItemKind, Ty, TyKind};
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3-
use utils::{in_macro, span_lint_and_then};
3+
use utils::{in_macro, snippet, span_lint_and_then};
44

55
/// **What it does:** Checks for constants with an explicit `'static` lifetime.
66
///
@@ -51,14 +51,15 @@ impl StaticConst {
5151
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) |
5252
TyKind::Tup(..) => {
5353
if lifetime.ident.name == "'static" {
54-
let mut sug: String = String::new();
54+
let snip = snippet(cx, borrow_type.ty.span, "<type>");
55+
let sugg = format!("&{}", snip);
5556
span_lint_and_then(
5657
cx,
5758
CONST_STATIC_LIFETIME,
5859
lifetime.span,
5960
"Constants have by default a `'static` lifetime",
6061
|db| {
61-
db.span_suggestion(lifetime.span, "consider removing `'static`", sug);
62+
db.span_suggestion(ty.span, "consider removing `'static`", sugg);
6263
},
6364
);
6465
}

tests/ui/const_static_lifetime.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,79 @@ error: Constants have by default a `'static` lifetime
22
--> $DIR/const_static_lifetime.rs:4:17
33
|
44
4 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static.
5-
| ^^^^^^^ help: consider removing `'static`
5+
| -^^^^^^^---- help: consider removing `'static`: `&str`
66
|
77
= note: `-D const-static-lifetime` implied by `-D warnings`
88

99
error: Constants have by default a `'static` lifetime
1010
--> $DIR/const_static_lifetime.rs:8:21
1111
|
1212
8 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static
13-
| ^^^^^^^ help: consider removing `'static`
13+
| -^^^^^^^---- help: consider removing `'static`: `&str`
1414

1515
error: Constants have by default a `'static` lifetime
1616
--> $DIR/const_static_lifetime.rs:10:32
1717
|
1818
10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
19-
| ^^^^^^^ help: consider removing `'static`
19+
| -^^^^^^^---- help: consider removing `'static`: `&str`
2020

2121
error: Constants have by default a `'static` lifetime
2222
--> $DIR/const_static_lifetime.rs:10:47
2323
|
2424
10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
25-
| ^^^^^^^ help: consider removing `'static`
25+
| -^^^^^^^---- help: consider removing `'static`: `&str`
2626

2727
error: Constants have by default a `'static` lifetime
2828
--> $DIR/const_static_lifetime.rs:12:18
2929
|
3030
12 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
31-
| ^^^^^^^ help: consider removing `'static`
31+
| -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]`
3232

3333
error: Constants have by default a `'static` lifetime
3434
--> $DIR/const_static_lifetime.rs:12:30
3535
|
3636
12 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
37-
| ^^^^^^^ help: consider removing `'static`
37+
| -^^^^^^^---- help: consider removing `'static`: `&str`
3838

3939
error: Constants have by default a `'static` lifetime
4040
--> $DIR/const_static_lifetime.rs:14:17
4141
|
4242
14 | const VAR_SIX: &'static u8 = &5;
43-
| ^^^^^^^ help: consider removing `'static`
43+
| -^^^^^^^--- help: consider removing `'static`: `&u8`
4444

4545
error: Constants have by default a `'static` lifetime
4646
--> $DIR/const_static_lifetime.rs:16:29
4747
|
4848
16 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
49-
| ^^^^^^^ help: consider removing `'static`
49+
| -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]`
5050

5151
error: Constants have by default a `'static` lifetime
5252
--> $DIR/const_static_lifetime.rs:16:39
5353
|
5454
16 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
55-
| ^^^^^^^ help: consider removing `'static`
55+
| -^^^^^^^---- help: consider removing `'static`: `&str`
5656

5757
error: Constants have by default a `'static` lifetime
5858
--> $DIR/const_static_lifetime.rs:18:20
5959
|
6060
18 | const VAR_HEIGHT: &'static Foo = &Foo {};
61-
| ^^^^^^^ help: consider removing `'static`
61+
| -^^^^^^^---- help: consider removing `'static`: `&Foo`
6262

6363
error: Constants have by default a `'static` lifetime
6464
--> $DIR/const_static_lifetime.rs:20:19
6565
|
6666
20 | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static.
67-
| ^^^^^^^ help: consider removing `'static`
67+
| -^^^^^^^----- help: consider removing `'static`: `&[u8]`
6868

6969
error: Constants have by default a `'static` lifetime
7070
--> $DIR/const_static_lifetime.rs:22:19
7171
|
7272
22 | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static.
73-
| ^^^^^^^ help: consider removing `'static`
73+
| -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)`
7474

7575
error: Constants have by default a `'static` lifetime
7676
--> $DIR/const_static_lifetime.rs:24:19
7777
|
7878
24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
79-
| ^^^^^^^ help: consider removing `'static`
79+
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
8080

0 commit comments

Comments
 (0)