]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/internal_lints.rs
Merge pull request #2920 from rust-lang-nursery/rustup
[rust.git] / clippy_lints / src / utils / internal_lints.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use crate::utils::{match_qpath, paths, span_lint};
5 use syntax::symbol::LocalInternedString;
6 use syntax::ast::{Crate as AstCrate, ItemKind, Name, NodeId};
7 use syntax::codemap::Span;
8 use std::collections::{HashMap, HashSet};
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_clippy_lint! {
19     pub CLIPPY_LINTS_INTERNAL,
20     internal,
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_clippy_lint! {
49     pub LINT_WITHOUT_LINT_PASS,
50     internal,
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
67             .module
68             .items
69             .iter()
70             .find(|item| item.ident.name == "utils")
71         {
72             if let ItemKind::Mod(ref utils_mod) = utils.node {
73                 if let Some(paths) = utils_mod
74                     .items
75                     .iter()
76                     .find(|item| item.ident.name == "paths")
77                 {
78                     if let ItemKind::Mod(ref paths_mod) = paths.node {
79                         let mut last_name: Option<LocalInternedString> = None;
80                         for item in &paths_mod.items {
81                             let name = item.ident.as_str();
82                             if let Some(ref last_name) = last_name {
83                                 if **last_name > *name {
84                                     span_lint(
85                                         cx,
86                                         CLIPPY_LINTS_INTERNAL,
87                                         item.span,
88                                         "this constant should be before the previous constant due to lexical \
89                                          ordering",
90                                     );
91                                 }
92                             }
93                             last_name = Some(name);
94                         }
95                     }
96                 }
97             }
98         }
99     }
100 }
101
102
103
104 #[derive(Clone, Debug, Default)]
105 pub struct LintWithoutLintPass {
106     declared_lints: HashMap<Name, Span>,
107     registered_lints: HashSet<Name>,
108 }
109
110
111 impl LintPass for LintWithoutLintPass {
112     fn get_lints(&self) -> LintArray {
113         lint_array!(LINT_WITHOUT_LINT_PASS)
114     }
115 }
116
117
118 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
119     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
120         if let ItemStatic(ref ty, MutImmutable, body_id) = item.node {
121             if is_lint_ref_type(ty) {
122                 self.declared_lints.insert(item.name, item.span);
123             } else if is_lint_array_type(ty) && item.name == "ARRAY" {
124                 if let VisibilityKind::Inherited = item.vis.node {
125                     let mut collector = LintCollector {
126                         output: &mut self.registered_lints,
127                         cx,
128                     };
129                     collector.visit_expr(&cx.tcx.hir.body(body_id).value);
130                 }
131             }
132         }
133     }
134
135     fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
136         for (lint_name, &lint_span) in &self.declared_lints {
137             // When using the `declare_lint!` macro, the original `lint_span`'s
138             // file points to "<rustc macros>".
139             // `compiletest-rs` thinks that's an error in a different file and
140             // just ignores it. This causes the test in compile-fail/lint_pass
141             // not able to capture the error.
142             // Therefore, we need to climb the macro expansion tree and find the
143             // actual span that invoked `declare_lint!`:
144             let lint_span = lint_span
145                 .ctxt()
146                 .outer()
147                 .expn_info()
148                 .map(|ei| ei.call_site)
149                 .expect("unable to get call_site");
150
151             if !self.registered_lints.contains(lint_name) {
152                 span_lint(
153                     cx,
154                     LINT_WITHOUT_LINT_PASS,
155                     lint_span,
156                     &format!("the lint `{}` is not added to any `LintPass`", lint_name),
157                 );
158             }
159         }
160     }
161 }
162
163
164 fn is_lint_ref_type(ty: &Ty) -> bool {
165     if let TyRptr(
166         _,
167         MutTy {
168             ty: ref inner,
169             mutbl: MutImmutable,
170         },
171     ) = ty.node
172     {
173         if let TyPath(ref path) = inner.node {
174             return match_qpath(path, &paths::LINT);
175         }
176     }
177     false
178 }
179
180
181 fn is_lint_array_type(ty: &Ty) -> bool {
182     if let TyPath(ref path) = ty.node {
183         match_qpath(path, &paths::LINT_ARRAY)
184     } else {
185         false
186     }
187 }
188
189 struct LintCollector<'a, 'tcx: 'a> {
190     output: &'a mut HashSet<Name>,
191     cx: &'a LateContext<'a, 'tcx>,
192 }
193
194 impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
195     fn visit_expr(&mut self, expr: &'tcx Expr) {
196         walk_expr(self, expr);
197     }
198
199     fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
200         if path.segments.len() == 1 {
201             self.output.insert(path.segments[0].ident.name);
202         }
203     }
204     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
205         NestedVisitorMap::All(&self.cx.tcx.hir)
206     }
207 }