Skip to content

Commit a7fc679

Browse files
committed
Rewrite registered lint collection
1 parent 267d5d3 commit a7fc679

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

clippy_lints/src/utils/internal_lints.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
use crate::utils::{
12-
match_def_path, match_qpath, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
12+
match_def_path, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
1313
};
1414
use if_chain::if_chain;
1515
use crate::rustc::hir;
@@ -161,16 +161,21 @@ impl LintPass for LintWithoutLintPass {
161161

162162
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
163163
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
164-
if let hir::ItemKind::Static(ref ty, MutImmutable, body_id) = item.node {
165-
164+
if let hir::ItemKind::Static(ref ty, MutImmutable, _) = item.node {
166165
if is_lint_ref_type(cx, ty) {
167166
self.declared_lints.insert(item.name, item.span);
168-
} else if is_lint_array_type(ty) && item.name == "ARRAY" {
169-
if let VisibilityKind::Inherited = item.vis.node {
167+
}
168+
} else if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
169+
if_chain! {
170+
if let hir::TraitRef{path, ..} = trait_ref;
171+
if let Def::Trait(def_id) = path.def;
172+
if match_def_path(cx.tcx, def_id, &paths::LINT_PASS);
173+
then {
170174
let mut collector = LintCollector {
171175
output: &mut self.registered_lints,
172176
cx,
173177
};
178+
let body_id = cx.tcx.hir.body_owned_by(impl_item_refs[0].id.node_id);
174179
collector.visit_expr(&cx.tcx.hir.body(body_id).value);
175180
}
176181
}
@@ -223,14 +228,6 @@ fn is_lint_ref_type<'tcx>(cx: &LateContext<'_, 'tcx>, ty: &Ty) -> bool {
223228
false
224229
}
225230

226-
fn is_lint_array_type(ty: &Ty) -> bool {
227-
if let TyKind::Path(ref path) = ty.node {
228-
match_qpath(path, &paths::LINT_ARRAY)
229-
} else {
230-
false
231-
}
232-
}
233-
234231
struct LintCollector<'a, 'tcx: 'a> {
235232
output: &'a mut FxHashSet<Name>,
236233
cx: &'a LateContext<'a, 'tcx>,

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub const ITERATOR: [&str; 4] = ["core", "iter", "iterator", "Iterator"];
5656
pub const LATE_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "LateContext"];
5757
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
5858
pub const LINT: [&str; 3] = ["rustc", "lint", "Lint"];
59-
pub const LINT_ARRAY: [&str; 3] = ["rustc", "lint", "LintArray"];
59+
pub const LINT_PASS: [&str; 3] = ["rustc", "lint", "LintPass"];
6060
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
6161
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
6262
pub const MEM_REPLACE: [&str; 3] = ["core", "mem", "replace"];

tests/ui/lint_without_lint_pass.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@
44

55
#[macro_use]
66
extern crate rustc;
7+
use rustc::lint;
78

89
#[macro_use]
910
extern crate clippy_lints;
1011

11-
declare_clippy_lint!
12-
{
12+
declare_clippy_lint! {
1313
pub TEST_LINT,
1414
correctness,
1515
""
1616
}
1717

18+
declare_clippy_lint! {
19+
pub TEST_LINT_REGISTERED,
20+
correctness,
21+
""
22+
}
23+
24+
pub struct Pass;
25+
impl lint::LintPass for Pass {
26+
fn get_lints(&self) -> lint::LintArray {
27+
lint_array!(TEST_LINT_REGISTERED)
28+
}
29+
}
30+
1831
fn main() {
1932
}

0 commit comments

Comments
 (0)