]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/const_static_lifetime.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / const_static_lifetime.rs
1 use syntax::ast::*;
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_lint, lint_array};
4 use crate::utils::{in_macro, snippet, span_lint_and_then};
5
6 /// **What it does:** Checks for constants with an explicit `'static` lifetime.
7 ///
8 /// **Why is this bad?** Adding `'static` to every reference can create very
9 /// complicated types.
10 ///
11 /// **Known problems:** None.
12 ///
13 /// **Example:**
14 /// ```rust
15 /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
16 /// &[...]
17 /// ```
18 /// This code can be rewritten as
19 /// ```rust
20 ///  const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
21 /// ```
22 declare_clippy_lint! {
23     pub CONST_STATIC_LIFETIME,
24     style,
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 careful of nested structures (arrays and tuples)
41             TyKind::Array(ref ty, _) => {
42                 self.visit_type(&*ty, cx);
43             },
44             TyKind::Tup(ref tup) => for tup_ty in tup {
45                 self.visit_type(&*tup_ty, cx);
46             },
47             // This is what we are looking for !
48             TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
49                 // Match the 'static lifetime
50                 if let Some(lifetime) = *optional_lifetime {
51                     match borrow_type.ty.node {
52                         TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) |
53                         TyKind::Tup(..) => {
54                             if lifetime.ident.name == "'static" {
55                                 let snip = snippet(cx, borrow_type.ty.span, "<type>");
56                                 let sugg = format!("&{}", snip);
57                                 span_lint_and_then(
58                                     cx,
59                                     CONST_STATIC_LIFETIME,
60                                     lifetime.ident.span,
61                                     "Constants have by default a `'static` lifetime",
62                                     |db| {
63                                         db.span_suggestion(ty.span, "consider removing `'static`", sugg);
64                                     },
65                                 );
66                             }
67                         }
68                         _ => {}
69                     }
70                 }
71                 self.visit_type(&*borrow_type.ty, cx);
72             },
73             TyKind::Slice(ref ty) => {
74                 self.visit_type(ty, cx);
75             },
76             _ => {},
77         }
78     }
79 }
80
81 impl EarlyLintPass for StaticConst {
82     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
83         if !in_macro(item.span) {
84             // Match only constants...
85             if let ItemKind::Const(ref var_type, _) = item.node {
86                 self.visit_type(var_type, cx);
87             }
88         }
89     }
90
91     // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
92 }