diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index 293d63daaaa..2872cb12729 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -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); + } + } + } } diff --git a/tests/ui/const_static_lifetime.rs b/tests/ui/const_static_lifetime.rs index a033f2b368e..745821a1503 100644 --- a/tests/ui/const_static_lifetime.rs +++ b/tests/ui/const_static_lifetime.rs @@ -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"; +} diff --git a/tests/ui/const_static_lifetime.stderr b/tests/ui/const_static_lifetime.stderr index db33744c7a9..b1059d2ef01 100644 --- a/tests/ui/const_static_lifetime.stderr +++ b/tests/ui/const_static_lifetime.stderr @@ -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