]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/internal_lints.rs
Merge pull request #1986 from koivunej/manualtesting-help
[rust.git] / clippy_lints / src / utils / internal_lints.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
4 use utils::{paths, match_qpath, span_lint};
5 use syntax::symbol::InternedString;
6 use syntax::ast::{Name, NodeId, ItemKind, Crate as AstCrate};
7 use syntax::codemap::Span;
8 use std::collections::{HashSet, HashMap};
9
10
11 /// **What it does:** Checks for various things we like to keep tidy in clippy.
12 ///
13 /// **Why is this bad?** We like to pretend we're an example of tidy code.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:** Wrong ordering of the util::paths constants.
18 declare_lint! {
19     pub CLIPPY_LINTS_INTERNAL,
20     Allow,
21     "various things that will negatively affect your clippy experience"
22 }
23
24
25 /// **What it does:** Ensures every lint is associated to a `LintPass`.
26 ///
27 /// **Why is this bad?** The compiler only knows lints via a `LintPass`. Without
28 /// putting a lint to a `LintPass::get_lints()`'s return, the compiler will not
29 /// know the name of the lint.
30 ///
31 /// **Known problems:** Only checks for lints associated using the `lint_array!`
32 /// macro.
33 ///
34 /// **Example:**
35 /// ```rust
36 /// declare_lint! { pub LINT_1, ... }
37 /// declare_lint! { pub LINT_2, ... }
38 /// declare_lint! { pub FORGOTTEN_LINT, ... }
39 /// // ...
40 /// pub struct Pass;
41 /// impl LintPass for Pass {
42 ///     fn get_lints(&self) -> LintArray {
43 ///         lint_array![LINT_1, LINT_2]
44 ///         // missing FORGOTTEN_LINT
45 ///     }
46 /// }
47 /// ```
48 declare_lint! {
49     pub LINT_WITHOUT_LINT_PASS,
50     Warn,
51     "declaring a lint without associating it in a LintPass"
52 }
53
54
55 #[derive(Copy, Clone)]
56 pub struct Clippy;
57
58 impl LintPass for Clippy {
59     fn get_lints(&self) -> LintArray {
60         lint_array!(CLIPPY_LINTS_INTERNAL)
61     }
62 }
63
64 impl EarlyLintPass for Clippy {
65     fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
66         if let Some(utils) = krate.module.items.iter().find(
67             |item| item.ident.name == "utils",
68         )
69         {
70             if let ItemKind::Mod(ref utils_mod) = utils.node {
71                 if let Some(paths) = utils_mod.items.iter().find(
72                     |item| item.ident.name == "paths",
73                 )
74                 {
75                     if let ItemKind::Mod(ref paths_mod) = paths.node {
76                         let mut last_name: Option<InternedString> = None;
77                         for item in &paths_mod.items {
78                             let name = item.ident.name.as_str();
79                             if let Some(ref last_name) = last_name {
80                                 if **last_name > *name {
81                                     span_lint(
82                                         cx,
83                                         CLIPPY_LINTS_INTERNAL,
84                                         item.span,
85                                         "this constant should be before the previous constant due to lexical \
86                                                ordering",
87                                     );
88                                 }
89                             }
90                             last_name = Some(name);
91                         }
92                     }
93                 }
94             }
95         }
96     }
97 }
98
99
100
101 #[derive(Clone, Debug, Default)]
102 pub struct LintWithoutLintPass {
103     declared_lints: HashMap<Name, Span>,
104     registered_lints: HashSet<Name>,
105 }
106
107
108 impl LintPass for LintWithoutLintPass {
109     fn get_lints(&self) -> LintArray {
110         lint_array!(LINT_WITHOUT_LINT_PASS)
111     }
112 }
113
114
115 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
116     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
117         if let ItemStatic(ref ty, MutImmutable, body_id) = item.node {
118             if is_lint_ref_type(ty) {
119                 self.declared_lints.insert(item.name, item.span);
120             } else if is_lint_array_type(ty) && item.vis == Visibility::Inherited && item.name == "ARRAY" {
121                 let mut collector = LintCollector {
122                     output: &mut self.registered_lints,
123                     cx: cx,
124                 };
125                 collector.visit_expr(&cx.tcx.hir.body(body_id).value);
126             }
127         }
128     }
129
130     fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
131         for (lint_name, &lint_span) in &self.declared_lints {
132             // When using the `declare_lint!` macro, the original `lint_span`'s
133             // file points to "<rustc macros>".
134             // `compiletest-rs` thinks that's an error in a different file and
135             // just ignores it. This causes the test in compile-fail/lint_pass
136             // not able to capture the error.
137             // Therefore, we need to climb the macro expansion tree and find the
138             // actual span that invoked `declare_lint!`:
139             let lint_span = lint_span
140                 .ctxt
141                 .outer()
142                 .expn_info()
143                 .map(|ei| ei.call_site)
144                 .expect("unable to get call_site");
145
146             if !self.registered_lints.contains(lint_name) {
147                 span_lint(
148                     cx,
149                     LINT_WITHOUT_LINT_PASS,
150                     lint_span,
151                     &format!("the lint `{}` is not added to any `LintPass`", lint_name),
152                 );
153             }
154         }
155     }
156 }
157
158
159 fn is_lint_ref_type(ty: &Ty) -> bool {
160     if let TyRptr(ref lt,
161                   MutTy {
162                       ty: ref inner,
163                       mutbl: MutImmutable,
164                   }) = ty.node
165     {
166         if lt.is_elided() {
167             return false;
168         }
169         if let TyPath(ref path) = inner.node {
170             return match_qpath(path, &paths::LINT);
171         }
172     }
173     false
174 }
175
176
177 fn is_lint_array_type(ty: &Ty) -> bool {
178     if let TyPath(ref path) = ty.node {
179         match_qpath(path, &paths::LINT_ARRAY)
180     } else {
181         false
182     }
183 }
184
185 struct LintCollector<'a, 'tcx: 'a> {
186     output: &'a mut HashSet<Name>,
187     cx: &'a LateContext<'a, 'tcx>,
188 }
189
190 impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
191     fn visit_expr(&mut self, expr: &'tcx Expr) {
192         walk_expr(self, expr);
193     }
194
195     fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
196         if path.segments.len() == 1 {
197             self.output.insert(path.segments[0].name);
198         }
199     }
200     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
201         NestedVisitorMap::All(&self.cx.tcx.hir)
202     }
203 }