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