Fix `const_static_lifetime`

This commit is contained in:
HMPerson1 2018-02-02 02:03:21 -05:00
parent ff83b3ecb9
commit 10d2feddba
No known key found for this signature in database
GPG Key ID: 1FB477DDD27821CE
3 changed files with 50 additions and 2 deletions

View File

@ -1,4 +1,4 @@
use syntax::ast::{Item, ItemKind, Ty, TyKind};
use syntax::ast::*;
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use utils::{in_macro, snippet, span_lint_and_then};
@ -86,4 +86,22 @@ impl EarlyLintPass for StaticConst {
}
}
}
fn check_trait_item(&mut self, cx: &EarlyContext, item: &TraitItem) {
if !in_macro(item.span) {
// Match only constants...
if let TraitItemKind::Const(ref var_type, _) = item.node {
self.visit_type(var_type, cx);
}
}
}
fn check_impl_item(&mut self, cx: &EarlyContext, item: &ImplItem) {
if !in_macro(item.span) {
// Match only constants...
if let ImplItemKind::Const(ref var_type, _) = item.node {
self.visit_type(var_type, cx);
}
}
}
}

View File

@ -35,3 +35,15 @@ fn main() {
println!("{:?}", VAR_HEIGHT);
println!("{}", false_positive);
}
trait Bar {
const TRAIT_VAR: &'static str;
}
impl Foo {
const IMPL_VAR: &'static str = "var";
}
impl Bar for Foo {
const TRAIT_VAR: &'static str = "foo";
}

View File

@ -78,5 +78,23 @@ error: Constants have by default a `'static` lifetime
24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
error: aborting due to 13 previous errors
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:40:23
|
40 | const TRAIT_VAR: &'static str;
| -^^^^^^^---- help: consider removing `'static`: `&str`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:44:22
|
44 | const IMPL_VAR: &'static str = "var";
| -^^^^^^^---- help: consider removing `'static`: `&str`
error: Constants have by default a `'static` lifetime
--> $DIR/const_static_lifetime.rs:48:23
|
48 | const TRAIT_VAR: &'static str = "foo";
| -^^^^^^^---- help: consider removing `'static`: `&str`
error: aborting due to 16 previous errors