From 003b9747e1b2eb167b2c71ad95cbccbc63755f85 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Tue, 25 Oct 2022 19:35:21 -0400 Subject: [PATCH] Fixes #9677 - Lint against mod lib; --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/mod_lib.rs | 25 ++++++++++--------------- tests/ui/mod_lib/main.stderr | 17 ++++++++--------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index ac30d5b56d44..6e65500fccff 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -913,7 +913,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_early_pass(|| Box::new(partial_pub_fields::PartialPubFields)); store.register_late_pass(|_| Box::new(missing_trait_methods::MissingTraitMethods)); store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); - store.register_early_pass(|| Box::new(mod_lib::ModLib)); + store.register_late_pass(|_| Box::new(mod_lib::ModLib)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/mod_lib.rs b/clippy_lints/src/mod_lib.rs index 5aa4a91d7280..489511b8e284 100644 --- a/clippy_lints/src/mod_lib.rs +++ b/clippy_lints/src/mod_lib.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_help; -use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind}; -use rustc_lint::{EarlyContext, EarlyLintPass}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_hir::{Item, ItemKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::sym; declare_clippy_lint! { @@ -22,21 +22,16 @@ declare_clippy_lint! { pedantic, "default lint description" } -declare_lint_pass!(ModLib => [MOD_LIB]); -impl EarlyLintPass for ModLib { - fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) { - // println!("MOO Checking crate {:#?}", krate); - check_mod(cx, &krate.items); - } -} +#[derive(Default)] +pub struct ModLib; -fn check_mod(cx: &EarlyContext<'_>, items: &[P]) { - for item in items { - if let ItemKind::Mod(_, ModKind::Loaded(..)) = item.kind { - // println!("MOO Got a Mod: {:#?}", items); - // println!("MOO Got a Mod: {:#?}", item.ident.name); +impl_lint_pass!(ModLib => [MOD_LIB]); +impl<'tcx> LateLintPass<'tcx> for ModLib { + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { + if let ItemKind::Mod(_) = item.kind { + // println!("MOO Found a Mod: {:#?}", item.ident); if item.ident.name == sym::lib { span_lint_and_help( cx, diff --git a/tests/ui/mod_lib/main.stderr b/tests/ui/mod_lib/main.stderr index 77333a89df41..0b9aac11f7bf 100644 --- a/tests/ui/mod_lib/main.stderr +++ b/tests/ui/mod_lib/main.stderr @@ -1,3 +1,11 @@ +error: unknown lint: `clippy::mod_lib` + --> $DIR/main.rs:2:9 + | +LL | #![warn(clippy::mod_lib)] + | ^^^^^^^^^^^^^^^ help: did you mean: `clippy::min_max` + | + = note: `-D unknown-lints` implied by `-D warnings` + error: found module declaration for lib.rs --> $DIR/main.rs:4:1 | @@ -8,14 +16,5 @@ LL | mod lib; = help: to refer to it from other targets, use the library's name as the path = note: `-D special-module-name` implied by `-D warnings` -error: uncommon use of mod::lib - --> $DIR/main.rs:4:1 - | -LL | mod lib; - | ^^^^^^^^ - | - = help: you probably meant use::package instead - = note: `-D clippy::mod-lib` implied by `-D warnings` - error: aborting due to 2 previous errors