const_static_lifetime: this applies not only to path types

For example, &'static [u8] or &'static (t1, t2).
This commit is contained in:
Georg Brandl
2017-11-18 16:10:28 +01:00
parent 2d005b58c5
commit 296edda3a9
3 changed files with 52 additions and 13 deletions

View File

@@ -48,20 +48,23 @@ impl StaticConst {
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
// Match the 'static lifetime // Match the 'static lifetime
if let Some(lifetime) = *optional_lifetime { if let Some(lifetime) = *optional_lifetime {
if let TyKind::Path(_, _) = borrow_type.ty.node { match borrow_type.ty.node {
// Verify that the path is a str TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) |
if lifetime.ident.name == "'static" { TyKind::Tup(..) => {
let mut sug: String = String::new(); if lifetime.ident.name == "'static" {
span_lint_and_then( let mut sug: String = String::new();
cx, span_lint_and_then(
CONST_STATIC_LIFETIME, cx,
lifetime.span, CONST_STATIC_LIFETIME,
"Constants have by default a `'static` lifetime", lifetime.span,
|db| { "Constants have by default a `'static` lifetime",
db.span_suggestion(lifetime.span, "consider removing `'static`", sug); |db| {
}, db.span_suggestion(lifetime.span, "consider removing `'static`", sug);
); },
);
}
} }
_ => {}
} }
} }
self.visit_type(&*borrow_type.ty, cx); self.visit_type(&*borrow_type.ty, cx);

View File

@@ -17,6 +17,12 @@ const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"]
const VAR_HEIGHT: &'static Foo = &Foo {}; const VAR_HEIGHT: &'static Foo = &Foo {};
const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static.
const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static.
const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
fn main() { fn main() {
let false_positive: &'static str = "test"; let false_positive: &'static str = "test";
println!("{}", VAR_ONE); println!("{}", VAR_ONE);

View File

@@ -24,6 +24,12 @@ error: Constants have by default a `'static` lifetime
10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static 10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static
| ^^^^^^^ help: consider removing `'static` | ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:12:18
|
12 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static
| ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:12:30 --> $DIR/const_static_lifetime.rs:12:30
| |
@@ -36,6 +42,12 @@ error: Constants have by default a `'static` lifetime
14 | const VAR_SIX: &'static u8 = &5; 14 | const VAR_SIX: &'static u8 = &5;
| ^^^^^^^ help: consider removing `'static` | ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:16:29
|
16 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])];
| ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:16:39 --> $DIR/const_static_lifetime.rs:16:39
| |
@@ -48,3 +60,21 @@ error: Constants have by default a `'static` lifetime
18 | const VAR_HEIGHT: &'static Foo = &Foo {}; 18 | const VAR_HEIGHT: &'static Foo = &Foo {};
| ^^^^^^^ help: consider removing `'static` | ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:20:19
|
20 | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static.
| ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:22:19
|
22 | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static.
| ^^^^^^^ help: consider removing `'static`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:24:19
|
24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
| ^^^^^^^ help: consider removing `'static`