]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/const_static_lifetime.rs
Fixed some code in clippy to pass the new, removed formatting changes.
[rust.git] / clippy_lints / src / const_static_lifetime.rs
1 use syntax::ast::{Item, ItemKind, TyKind, Ty};
2 use rustc::lint::{LintPass, EarlyLintPass, LintArray, EarlyContext};
3 use utils::{span_help_and_lint, in_macro};
4
5 /// **What it does:** Checks for constants with an explicit `'static` lifetime.
6 ///
7 /// **Why is this bad?** Adding `'static` to every reference can create very complicated types.
8 ///
9 /// **Known problems:** None.
10 ///
11 /// **Example:**
12 /// ```rust
13 ///  const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = &[..]
14 /// ```
15 /// This code can be rewritten as
16 /// ```rust
17 ///  const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
18 /// ```
19
20 declare_lint! {
21     pub CONST_STATIC_LIFETIME, 
22     Warn,
23     "Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
24 }
25
26 pub struct StaticConst;
27
28 impl LintPass for StaticConst {
29     fn get_lints(&self) -> LintArray {
30         lint_array!(CONST_STATIC_LIFETIME)
31     }
32 }
33
34 impl StaticConst {
35     // Recursively visit types
36     fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext) {
37         match ty.node {
38             // Be carefull of nested structures (arrays and tuples)
39             TyKind::Array(ref ty, _) => {
40                 println!("array");
41                 self.visit_type(&*ty, cx);
42             },
43             TyKind::Tup(ref tup) => {
44                 for tup_ty in tup {
45                     self.visit_type(&*tup_ty, cx);
46                 }
47             },
48             // This is what we are looking for !
49             TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
50                 // Match the 'static lifetime
51                 if let Some(lifetime) = *optional_lifetime {
52                     if let TyKind::Path(_, _) = borrow_type.ty.node {
53                         // Verify that the path is a str
54                         if lifetime.ident.name == "'static" {
55                             span_help_and_lint(cx,
56                                                CONST_STATIC_LIFETIME,
57                                                lifetime.span,
58                                                "Constants have by default a `'static` lifetime",
59                                                "consider removing `'static`");
60                         }
61                     }
62                 }
63                 self.visit_type(&*borrow_type.ty, cx);
64             },
65             TyKind::Slice(ref ty) => {
66                 self.visit_type(ty, cx);
67             },
68             _ => {},
69         }
70     }
71 }
72
73 impl EarlyLintPass for StaticConst {
74     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
75         if !in_macro(item.span) {
76             // Match only constants...
77             if let ItemKind::Const(ref var_type, _) = item.node {
78                 self.visit_type(var_type, cx);
79             }
80         }
81     }
82 }