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};
12 /// **What it does:** Checks for various things we like to keep tidy in clippy.
14 /// **Why is this bad?** We like to pretend we're an example of tidy code.
16 /// **Known problems:** None.
18 /// **Example:** Wrong ordering of the util::paths constants.
19 declare_clippy_lint! {
20 pub CLIPPY_LINTS_INTERNAL,
22 "various things that will negatively affect your clippy experience"
26 /// **What it does:** Ensures every lint is associated to a `LintPass`.
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.
32 /// **Known problems:** Only checks for lints associated using the `lint_array!`
37 /// declare_lint! { pub LINT_1, ... }
38 /// declare_lint! { pub LINT_2, ... }
39 /// declare_lint! { pub FORGOTTEN_LINT, ... }
42 /// impl LintPass for Pass {
43 /// fn get_lints(&self) -> LintArray {
44 /// lint_array![LINT_1, LINT_2]
45 /// // missing FORGOTTEN_LINT
49 declare_clippy_lint! {
50 pub LINT_WITHOUT_LINT_PASS,
52 "declaring a lint without associating it in a LintPass"
56 #[derive(Copy, Clone)]
59 impl LintPass for Clippy {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(CLIPPY_LINTS_INTERNAL)
65 impl EarlyLintPass for Clippy {
66 fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
67 if let Some(utils) = krate
71 .find(|item| item.ident.name == "utils")
73 if let ItemKind::Mod(ref utils_mod) = utils.node {
74 if let Some(paths) = utils_mod
77 .find(|item| item.ident.name == "paths")
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 {
87 CLIPPY_LINTS_INTERNAL,
89 "this constant should be before the previous constant due to lexical \
94 last_name = Some(name);
105 #[derive(Clone, Debug, Default)]
106 pub struct LintWithoutLintPass {
107 declared_lints: HashMap<Name, Span>,
108 registered_lints: HashSet<Name>,
112 impl LintPass for LintWithoutLintPass {
113 fn get_lints(&self) -> LintArray {
114 lint_array!(LINT_WITHOUT_LINT_PASS)
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,
130 collector.visit_expr(&cx.tcx.hir.body(body_id).value);
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
149 .map(|ei| ei.call_site)
150 .expect("unable to get call_site");
152 if !self.registered_lints.contains(lint_name) {
155 LINT_WITHOUT_LINT_PASS,
157 &format!("the lint `{}` is not added to any `LintPass`", lint_name),
165 fn is_lint_ref_type(ty: &Ty) -> bool {
174 if let TyKind::Path(ref path) = inner.node {
175 return match_qpath(path, &paths::LINT);
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)
190 struct LintCollector<'a, 'tcx: 'a> {
191 output: &'a mut HashSet<Name>,
192 cx: &'a LateContext<'a, 'tcx>,
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);
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);
205 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
206 NestedVisitorMap::All(&self.cx.tcx.hir)