3 use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
4 use utils::{paths, match_path, span_lint};
5 use syntax::symbol::InternedString;
6 use syntax::ast::{Name, NodeId, ItemKind, Crate as AstCrate};
7 use syntax::codemap::Span;
8 use std::collections::{HashSet, HashMap};
11 /// **What it does:** Checks for various things we like to keep tidy in clippy.
13 /// **Why is this bad?** We like to pretend we're an example of tidy code.
15 /// **Known problems:** None.
17 /// **Example:** Wrong ordering of the util::paths constants.
19 pub CLIPPY_LINTS_INTERNAL,
21 "various things that will negatively affect your clippy experience"
25 /// **What it does:** Ensures every lint is associated to a `LintPass`.
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.
31 /// **Known problems:** Only checks for lints associated using the `lint_array!`
36 /// declare_lint! { pub LINT_1, ... }
37 /// declare_lint! { pub LINT_2, ... }
38 /// declare_lint! { pub FORGOTTEN_LINT, ... }
41 /// impl LintPass for Pass {
42 /// fn get_lints(&self) -> LintArray {
43 /// lint_array![LINT_1, LINT_2]
44 /// // missing FORGOTTEN_LINT
49 pub LINT_WITHOUT_LINT_PASS,
51 "declaring a lint without associating it in a LintPass"
55 #[derive(Copy, Clone)]
58 impl LintPass for Clippy {
59 fn get_lints(&self) -> LintArray {
60 lint_array!(CLIPPY_LINTS_INTERNAL)
64 impl EarlyLintPass for Clippy {
65 fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
66 if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name == "utils") {
67 if let ItemKind::Mod(ref utils_mod) = utils.node {
68 if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name == "paths") {
69 if let ItemKind::Mod(ref paths_mod) = paths.node {
70 let mut last_name: Option<InternedString> = None;
71 for item in &paths_mod.items {
72 let name = item.ident.name.as_str();
73 if let Some(ref last_name) = last_name {
74 if **last_name > *name {
76 CLIPPY_LINTS_INTERNAL,
78 "this constant should be before the previous constant due to lexical \
82 last_name = Some(name);
93 #[derive(Clone, Debug, Default)]
94 pub struct LintWithoutLintPass {
95 declared_lints: HashMap<Name, Span>,
96 registered_lints: HashSet<Name>,
100 impl LintPass for LintWithoutLintPass {
101 fn get_lints(&self) -> LintArray {
102 lint_array!(LINT_WITHOUT_LINT_PASS)
107 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
108 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
109 if let ItemStatic(ref ty, MutImmutable, body_id) = item.node {
110 if is_lint_ref_type(ty) {
111 self.declared_lints.insert(item.name, item.span);
112 } else if is_lint_array_type(ty) && item.vis == Visibility::Inherited && item.name == "ARRAY" {
113 let mut collector = LintCollector {
114 output: &mut self.registered_lints,
117 collector.visit_expr(&cx.tcx.hir.body(body_id).value);
122 fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
123 for (lint_name, &lint_span) in &self.declared_lints {
124 // When using the `declare_lint!` macro, the original `lint_span`'s
125 // file points to "<rustc macros>".
126 // `compiletest-rs` thinks that's an error in a different file and
127 // just ignores it. This causes the test in compile-fail/lint_pass
128 // not able to capture the error.
129 // Therefore, we need to climb the macro expansion tree and find the
130 // actual span that invoked `declare_lint!`:
131 let lint_span = cx.sess().codemap().source_callsite(lint_span);
133 if !self.registered_lints.contains(lint_name) {
135 LINT_WITHOUT_LINT_PASS,
137 &format!("the lint `{}` is not added to any `LintPass`", lint_name));
144 fn is_lint_ref_type(ty: &Ty) -> bool {
145 if let TyRptr(_, MutTy { ty: ref inner, mutbl: MutImmutable }) = ty.node {
146 if let TyPath(ref path) = inner.node {
147 return match_path(path, &paths::LINT);
154 fn is_lint_array_type(ty: &Ty) -> bool {
155 if let TyPath(ref path) = ty.node {
156 match_path(path, &paths::LINT_ARRAY)
162 struct LintCollector<'a, 'tcx: 'a> {
163 output: &'a mut HashSet<Name>,
164 cx: &'a LateContext<'a, 'tcx>,
167 impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
168 fn visit_expr(&mut self, expr: &'tcx Expr) {
169 walk_expr(self, expr);
172 fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
173 if path.segments.len() == 1 {
174 self.output.insert(path.segments[0].name);
177 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
178 NestedVisitorMap::All(&self.cx.tcx.hir)