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