]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/const_static_lifetime.rs
Try fixing const_with_static_lifetime docs
[rust.git] / clippy_lints / src / const_static_lifetime.rs
1 use syntax::ast::{Item, ItemKind, Ty, TyKind};
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use utils::{in_macro, span_lint_and_then};
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 declare_lint! {
22     pub CONST_STATIC_LIFETIME,
23     Warn,
24     "Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
25 }
26
27 pub struct StaticConst;
28
29 impl LintPass for StaticConst {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(CONST_STATIC_LIFETIME)
32     }
33 }
34
35 impl StaticConst {
36     // Recursively visit types
37     fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext) {
38         match ty.node {
39             // Be careful of nested structures (arrays and tuples)
40             TyKind::Array(ref ty, _) => {
41                 self.visit_type(&*ty, cx);
42             },
43             TyKind::Tup(ref tup) => for tup_ty in tup {
44                 self.visit_type(&*tup_ty, cx);
45             },
46             // This is what we are looking for !
47             TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
48                 // Match the 'static lifetime
49                 if let Some(lifetime) = *optional_lifetime {
50                     match borrow_type.ty.node {
51                         TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) |
52                         TyKind::Tup(..) => {
53                             if lifetime.ident.name == "'static" {
54                                 let mut sug: String = String::new();
55                                 span_lint_and_then(
56                                     cx,
57                                     CONST_STATIC_LIFETIME,
58                                     lifetime.span,
59                                     "Constants have by default a `'static` lifetime",
60                                     |db| {
61                                         db.span_suggestion(lifetime.span, "consider removing `'static`", sug);
62                                     },
63                                 );
64                             }
65                         }
66                         _ => {}
67                     }
68                 }
69                 self.visit_type(&*borrow_type.ty, cx);
70             },
71             TyKind::Slice(ref ty) => {
72                 self.visit_type(ty, cx);
73             },
74             _ => {},
75         }
76     }
77 }
78
79 impl EarlyLintPass for StaticConst {
80     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
81         if !in_macro(item.span) {
82             // Match only constants...
83             if let ItemKind::Const(ref var_type, _) = item.node {
84                 self.visit_type(var_type, cx);
85             }
86         }
87     }
88 }