]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/const_static_lifetime.rs
Auto merge of #3596 - xfix:remove-crate-from-paths, r=flip1995
[rust.git] / clippy_lints / src / const_static_lifetime.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::utils::{in_macro, snippet, span_lint_and_then};
11 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
12 use rustc::{declare_tool_lint, lint_array};
13 use rustc_errors::Applicability;
14 use syntax::ast::*;
15
16 /// **What it does:** Checks for constants with an explicit `'static` lifetime.
17 ///
18 /// **Why is this bad?** Adding `'static` to every reference can create very
19 /// complicated types.
20 ///
21 /// **Known problems:** None.
22 ///
23 /// **Example:**
24 /// ```rust
25 /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
26 /// &[...]
27 /// ```
28 /// This code can be rewritten as
29 /// ```rust
30 ///  const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
31 /// ```
32 declare_clippy_lint! {
33     pub CONST_STATIC_LIFETIME,
34     style,
35     "Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
36 }
37
38 pub struct StaticConst;
39
40 impl LintPass for StaticConst {
41     fn get_lints(&self) -> LintArray {
42         lint_array!(CONST_STATIC_LIFETIME)
43     }
44 }
45
46 impl StaticConst {
47     // Recursively visit types
48     fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
49         match ty.node {
50             // Be careful of nested structures (arrays and tuples)
51             TyKind::Array(ref ty, _) => {
52                 self.visit_type(&*ty, cx);
53             },
54             TyKind::Tup(ref tup) => {
55                 for tup_ty in tup {
56                     self.visit_type(&*tup_ty, cx);
57                 }
58             },
59             // This is what we are looking for !
60             TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
61                 // Match the 'static lifetime
62                 if let Some(lifetime) = *optional_lifetime {
63                     match borrow_type.ty.node {
64                         TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
65                             if lifetime.ident.name == "'static" {
66                                 let snip = snippet(cx, borrow_type.ty.span, "<type>");
67                                 let sugg = format!("&{}", snip);
68                                 span_lint_and_then(
69                                     cx,
70                                     CONST_STATIC_LIFETIME,
71                                     lifetime.ident.span,
72                                     "Constants have by default a `'static` lifetime",
73                                     |db| {
74                                         db.span_suggestion_with_applicability(
75                                             ty.span,
76                                             "consider removing `'static`",
77                                             sugg,
78                                             Applicability::MachineApplicable, //snippet
79                                         );
80                                     },
81                                 );
82                             }
83                         },
84                         _ => {},
85                     }
86                 }
87                 self.visit_type(&*borrow_type.ty, cx);
88             },
89             TyKind::Slice(ref ty) => {
90                 self.visit_type(ty, cx);
91             },
92             _ => {},
93         }
94     }
95 }
96
97 impl EarlyLintPass for StaticConst {
98     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
99         if !in_macro(item.span) {
100             // Match only constants...
101             if let ItemKind::Const(ref var_type, _) = item.node {
102                 self.visit_type(var_type, cx);
103             }
104         }
105     }
106
107     // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
108 }