2 use rustc::{declare_lint, lint_array};
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, NodeId};
9 use syntax::codemap::Span;
10 use std::collections::{HashMap, HashSet};
13 /// **What it does:** Checks for various things we like to keep tidy in clippy.
15 /// **Why is this bad?** We like to pretend we're an example of tidy code.
17 /// **Known problems:** None.
19 /// **Example:** Wrong ordering of the util::paths constants.
20 declare_clippy_lint! {
21 pub CLIPPY_LINTS_INTERNAL,
23 "various things that will negatively affect your clippy experience"
27 /// **What it does:** Ensures every lint is associated to a `LintPass`.
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.
33 /// **Known problems:** Only checks for lints associated using the `lint_array!`
38 /// declare_lint! { pub LINT_1, ... }
39 /// declare_lint! { pub LINT_2, ... }
40 /// declare_lint! { pub FORGOTTEN_LINT, ... }
43 /// impl LintPass for Pass {
44 /// fn get_lints(&self) -> LintArray {
45 /// lint_array![LINT_1, LINT_2]
46 /// // missing FORGOTTEN_LINT
50 declare_clippy_lint! {
51 pub LINT_WITHOUT_LINT_PASS,
53 "declaring a lint without associating it in a LintPass"
57 #[derive(Copy, Clone)]
60 impl LintPass for Clippy {
61 fn get_lints(&self) -> LintArray {
62 lint_array!(CLIPPY_LINTS_INTERNAL)
66 impl EarlyLintPass for Clippy {
67 fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
68 if let Some(utils) = krate
72 .find(|item| item.ident.name == "utils")
74 if let ItemKind::Mod(ref utils_mod) = utils.node {
75 if let Some(paths) = utils_mod
78 .find(|item| item.ident.name == "paths")
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 {
88 CLIPPY_LINTS_INTERNAL,
90 "this constant should be before the previous constant due to lexical \
95 last_name = Some(name);
106 #[derive(Clone, Debug, Default)]
107 pub struct LintWithoutLintPass {
108 declared_lints: HashMap<Name, Span>,
109 registered_lints: HashSet<Name>,
113 impl LintPass for LintWithoutLintPass {
114 fn get_lints(&self) -> LintArray {
115 lint_array!(LINT_WITHOUT_LINT_PASS)
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,
131 collector.visit_expr(&cx.tcx.hir.body(body_id).value);
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
150 .map(|ei| ei.call_site)
151 .expect("unable to get call_site");
153 if !self.registered_lints.contains(lint_name) {
156 LINT_WITHOUT_LINT_PASS,
158 &format!("the lint `{}` is not added to any `LintPass`", lint_name),
166 fn is_lint_ref_type(ty: &Ty) -> bool {
175 if let TyKind::Path(ref path) = inner.node {
176 return match_qpath(path, &paths::LINT);
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)
191 struct LintCollector<'a, 'tcx: 'a> {
192 output: &'a mut HashSet<Name>,
193 cx: &'a LateContext<'a, 'tcx>,
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);
201 fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
202 if path.segments.len() == 1 {
203 self.output.insert(path.segments[0].ident.name);
206 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
207 NestedVisitorMap::All(&self.cx.tcx.hir)