Skip to content

Commit 10d2fed

Browse files
committed
Fix const_static_lifetime
1 parent ff83b3e commit 10d2fed

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

clippy_lints/src/const_static_lifetime.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use syntax::ast::{Item, ItemKind, Ty, TyKind};
1+
use syntax::ast::*;
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
33
use utils::{in_macro, snippet, span_lint_and_then};
44

@@ -86,4 +86,22 @@ impl EarlyLintPass for StaticConst {
8686
}
8787
}
8888
}
89+
90+
fn check_trait_item(&mut self, cx: &EarlyContext, item: &TraitItem) {
91+
if !in_macro(item.span) {
92+
// Match only constants...
93+
if let TraitItemKind::Const(ref var_type, _) = item.node {
94+
self.visit_type(var_type, cx);
95+
}
96+
}
97+
}
98+
99+
fn check_impl_item(&mut self, cx: &EarlyContext, item: &ImplItem) {
100+
if !in_macro(item.span) {
101+
// Match only constants...
102+
if let ImplItemKind::Const(ref var_type, _) = item.node {
103+
self.visit_type(var_type, cx);
104+
}
105+
}
106+
}
89107
}

tests/ui/const_static_lifetime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ fn main() {
3535
println!("{:?}", VAR_HEIGHT);
3636
println!("{}", false_positive);
3737
}
38+
39+
trait Bar {
40+
const TRAIT_VAR: &'static str;
41+
}
42+
43+
impl Foo {
44+
const IMPL_VAR: &'static str = "var";
45+
}
46+
47+
impl Bar for Foo {
48+
const TRAIT_VAR: &'static str = "foo";
49+
}

tests/ui/const_static_lifetime.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,23 @@ error: Constants have by default a `'static` lifetime
7878
24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
7979
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
8080

81-
error: aborting due to 13 previous errors
81+
error: Constants have by default a `'static` lifetime
82+
--> $DIR/const_static_lifetime.rs:40:23
83+
|
84+
40 | const TRAIT_VAR: &'static str;
85+
| -^^^^^^^---- help: consider removing `'static`: `&str`
86+
87+
error: Constants have by default a `'static` lifetime
88+
--> $DIR/const_static_lifetime.rs:44:22
89+
|
90+
44 | const IMPL_VAR: &'static str = "var";
91+
| -^^^^^^^---- help: consider removing `'static`: `&str`
92+
93+
error: Constants have by default a `'static` lifetime
94+
--> $DIR/const_static_lifetime.rs:48:23
95+
|
96+
48 | const TRAIT_VAR: &'static str = "foo";
97+
| -^^^^^^^---- help: consider removing `'static`: `&str`
98+
99+
error: aborting due to 16 previous errors
82100

0 commit comments

Comments
 (0)