Skip to content

Commit

Permalink
Fixes rust-lang#9677 - Lint against mod lib;
Browse files Browse the repository at this point in the history
  • Loading branch information
st3fan committed Oct 25, 2022
1 parent a12a8d7 commit 003b974
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}

Expand Down
25 changes: 10 additions & 15 deletions clippy_lints/src/mod_lib.rs
Original file line number Diff line number Diff line change
@@ -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! {
Expand All @@ -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<Item>]) {
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,
Expand Down
17 changes: 8 additions & 9 deletions tests/ui/mod_lib/main.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand All @@ -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

0 comments on commit 003b974

Please sign in to comment.