]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/const_static_lifetime.rs
Fix merge issues.
[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
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 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                     if let TyKind::Path(_, _) = borrow_type.ty.node {
52                         // Verify that the path is a str
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                 self.visit_type(&*borrow_type.ty, cx);
68             },
69             TyKind::Slice(ref ty) => {
70                 self.visit_type(ty, cx);
71             },
72             _ => {},
73         }
74     }
75 }
76
77 impl EarlyLintPass for StaticConst {
78     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
79         if !in_macro(item.span) {
80             // Match only constants...
81             if let ItemKind::Const(ref var_type, _) = item.node {
82                 self.visit_type(var_type, cx);
83             }
84         }
85     }
86 }