1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use rustc::lint::*;
use rustc::hir::*;
use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
use utils::{paths, match_path, span_lint};
use syntax::symbol::InternedString;
use syntax::ast::{Name, NodeId, ItemKind, Crate as AstCrate};
use syntax::codemap::Span;
use std::collections::{HashSet, HashMap};
declare_lint! {
pub CLIPPY_LINTS_INTERNAL,
Allow,
"various things that will negatively affect your clippy experience"
}
declare_lint! {
pub LINT_WITHOUT_LINT_PASS,
Warn,
"declaring a lint without associating it in a LintPass"
}
#[derive(Copy, Clone)]
pub struct Clippy;
impl LintPass for Clippy {
fn get_lints(&self) -> LintArray {
lint_array!(CLIPPY_LINTS_INTERNAL)
}
}
impl EarlyLintPass for Clippy {
fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name == "utils") {
if let ItemKind::Mod(ref utils_mod) = utils.node {
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name == "paths") {
if let ItemKind::Mod(ref paths_mod) = paths.node {
let mut last_name: Option<InternedString> = None;
for item in &paths_mod.items {
let name = item.ident.name.as_str();
if let Some(ref last_name) = last_name {
if **last_name > *name {
span_lint(cx,
CLIPPY_LINTS_INTERNAL,
item.span,
"this constant should be before the previous constant due to lexical \
ordering");
}
}
last_name = Some(name);
}
}
}
}
}
}
}
#[derive(Clone, Debug, Default)]
pub struct LintWithoutLintPass {
declared_lints: HashMap<Name, Span>,
registered_lints: HashSet<Name>,
}
impl LintPass for LintWithoutLintPass {
fn get_lints(&self) -> LintArray {
lint_array!(LINT_WITHOUT_LINT_PASS)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let ItemStatic(ref ty, MutImmutable, body_id) = item.node {
if is_lint_ref_type(ty) {
self.declared_lints.insert(item.name, item.span);
} else if is_lint_array_type(ty) && item.vis == Visibility::Inherited && item.name == "ARRAY" {
let mut collector = LintCollector {
output: &mut self.registered_lints,
cx: cx,
};
collector.visit_expr(&cx.tcx.map.body(body_id).value);
}
}
}
fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
for (lint_name, &lint_span) in &self.declared_lints {
let lint_span = cx.sess().codemap().source_callsite(lint_span);
if !self.registered_lints.contains(lint_name) {
span_lint(cx,
LINT_WITHOUT_LINT_PASS,
lint_span,
&format!("the lint `{}` is not added to any `LintPass`", lint_name));
}
}
}
}
fn is_lint_ref_type(ty: &Ty) -> bool {
if let TyRptr(Some(_), MutTy { ty: ref inner, mutbl: MutImmutable }) = ty.node {
if let TyPath(ref path) = inner.node {
return match_path(path, &paths::LINT);
}
}
false
}
fn is_lint_array_type(ty: &Ty) -> bool {
if let TyPath(ref path) = ty.node {
match_path(path, &paths::LINT_ARRAY)
} else {
false
}
}
struct LintCollector<'a, 'tcx: 'a> {
output: &'a mut HashSet<Name>,
cx: &'a LateContext<'a, 'tcx>,
}
impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr) {
walk_expr(self, expr);
}
fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
if path.segments.len() == 1 {
self.output.insert(path.segments[0].name);
}
}
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::All(&self.cx.tcx.map)
}
}