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