From 562cc63b7efa47311424f194c95a7d4b7b51f320 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 24 Oct 2019 13:54:18 +0200 Subject: [PATCH] Fix lint_without_lint_pass lint --- clippy_lints/src/utils/internal_lints.rs | 39 ++++++++++++------------ clippy_lints/src/utils/paths.rs | 1 - tests/ui/lint_without_lint_pass.rs | 8 ++++- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index bc8084f85de..b5cd0d17f77 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -1,5 +1,6 @@ use crate::utils::{ - match_def_path, match_type, method_calls, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty, + is_expn_of, match_def_path, match_type, method_calls, paths, span_help_and_lint, span_lint, span_lint_and_sugg, + walk_ptrs_ty, }; use if_chain::if_chain; use rustc::hir; @@ -148,25 +149,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { if is_lint_ref_type(cx, ty) { self.declared_lints.insert(item.ident.name, item.span); } - } else if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.kind { - if_chain! { - if let hir::TraitRef{path, ..} = trait_ref; - if let Res::Def(DefKind::Trait, def_id) = path.res; - if match_def_path(cx, def_id, &paths::LINT_PASS); - then { - let mut collector = LintCollector { - output: &mut self.registered_lints, - cx, - }; - let body_id = cx.tcx.hir().body_owned_by( - impl_item_refs - .iter() - .find(|iiref| iiref.ident.as_str() == "get_lints") - .expect("LintPass needs to implement get_lints") - .id.hir_id - ); - collector.visit_expr(&cx.tcx.hir().body(body_id).value); - } + } else if is_expn_of(item.span, "impl_lint_pass").is_some() + || is_expn_of(item.span, "declare_lint_pass").is_some() + { + if let hir::ItemKind::Impl(.., None, _, ref impl_item_refs) = item.kind { + let mut collector = LintCollector { + output: &mut self.registered_lints, + cx, + }; + let body_id = cx.tcx.hir().body_owned_by( + impl_item_refs + .iter() + .find(|iiref| iiref.ident.as_str() == "get_lints") + .expect("LintPass needs to implement get_lints") + .id + .hir_id, + ); + collector.visit_expr(&cx.tcx.hir().body(body_id).value); } } } diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs index cda3d2024d2..9787517e505 100644 --- a/clippy_lints/src/utils/paths.rs +++ b/clippy_lints/src/utils/paths.rs @@ -46,7 +46,6 @@ pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator pub const LATE_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "LateContext"]; pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"]; pub const LINT: [&str; 3] = ["rustc", "lint", "Lint"]; -pub const LINT_PASS: [&str; 3] = ["rustc", "lint", "LintPass"]; pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"]; pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"]; pub const MEM_MAYBEUNINIT: [&str; 4] = ["core", "mem", "maybe_uninit", "MaybeUninit"]; diff --git a/tests/ui/lint_without_lint_pass.rs b/tests/ui/lint_without_lint_pass.rs index 98c4cfba76a..dc93bbfd752 100644 --- a/tests/ui/lint_without_lint_pass.rs +++ b/tests/ui/lint_without_lint_pass.rs @@ -20,6 +20,12 @@ declare_clippy_lint! { "" } +declare_clippy_lint! { + pub TEST_LINT_REGISTERED_ONLY_IMPL, + correctness, + "" +} + pub struct Pass; impl LintPass for Pass { fn name(&self) -> &'static str { @@ -30,6 +36,6 @@ impl LintPass for Pass { declare_lint_pass!(Pass2 => [TEST_LINT_REGISTERED]); pub struct Pass3; -impl_lint_pass!(Pass3 => [TEST_LINT_REGISTERED]); +impl_lint_pass!(Pass3 => [TEST_LINT_REGISTERED_ONLY_IMPL]); fn main() {}