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 191c983 commit 258a205
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,7 @@ Released 2018-09-13
[`mistyped_literal_suffixes`]: https://rust-lang.github.io/rust-clippy/master/index.html#mistyped_literal_suffixes
[`mixed_case_hex_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_case_hex_literals
[`mixed_read_write_in_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_read_write_in_expression
[`mod_lib`]: https://rust-lang.github.io/rust-clippy/master/index.html#mod_lib
[`mod_module_files`]: https://rust-lang.github.io/rust-clippy/master/index.html#mod_module_files
[`module_inception`]: https://rust-lang.github.io/rust-clippy/master/index.html#module_inception
[`module_name_repetitions`]: https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ store.register_lints(&[
missing_trait_methods::MISSING_TRAIT_METHODS,
mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION,
mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION,
mod_lib::MOD_LIB,
module_style::MOD_MODULE_FILES,
module_style::SELF_NAMED_MODULE_FILES,
multi_assignments::MULTI_ASSIGNMENTS,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_pedantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
LintId::of(methods::UNNECESSARY_JOIN),
LintId::of(misc::USED_UNDERSCORE_BINDING),
LintId::of(mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER),
LintId::of(mod_lib::MOD_LIB),
LintId::of(mut_mut::MUT_MUT),
LintId::of(needless_continue::NEEDLESS_CONTINUE),
LintId::of(needless_for_each::NEEDLESS_FOR_EACH),
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ mod missing_enforced_import_rename;
mod missing_inline;
mod missing_trait_methods;
mod mixed_read_write_in_expression;
mod mod_lib;
mod module_style;
mod multi_assignments;
mod mut_key;
Expand Down Expand Up @@ -920,6 +921,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));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
52 changes: 52 additions & 0 deletions clippy_lints/src/mod_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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_span::sym;

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.66.0"]
pub MOD_LIB,
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);
}
}

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);

if item.ident.name == sym::lib {
span_lint_and_help(
cx,
MOD_LIB,
item.span,
"uncommon use of mod::lib",
None,
"you probably meant use::package instead",
);
}
}
}
}
1 change: 1 addition & 0 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ docs! {
"mistyped_literal_suffixes",
"mixed_case_hex_literals",
"mixed_read_write_in_expression",
"mod_lib",
"mod_module_files",
"module_inception",
"module_name_repetitions",
Expand Down
12 changes: 12 additions & 0 deletions src/docs/mod_lib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### What it does

### Why is this bad?

### Example
```
// example code where clippy issues a warning
```
Use instead:
```
// example code which does not raise clippy warning
```
3 changes: 3 additions & 0 deletions tests/ui/mod_lib/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn something() -> i32 {
42
}
9 changes: 9 additions & 0 deletions tests/ui/mod_lib/lib.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0601]: `main` function not found in crate `lib`
--> $DIR/lib.rs:3:2
|
LL | }
| ^ consider adding a `main` function to `$DIR/lib.rs`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0601`.
8 changes: 8 additions & 0 deletions tests/ui/mod_lib/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![allow(unused)]
#![warn(clippy::mod_lib)]

mod lib;

fn main() {
println!("{}", lib::something());
}
21 changes: 21 additions & 0 deletions tests/ui/mod_lib/main.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: found module declaration for lib.rs
--> $DIR/main.rs:4:1
|
LL | mod lib;
| ^^^^^^^^
|
= note: lib.rs is the root of this crate's library target
= 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 258a205

Please sign in to comment.