diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e91365c69a3..48237d15fbff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4055,6 +4055,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 diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 5716ef71641c..6e65500fccff 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -192,6 +192,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; @@ -912,6 +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_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 new file mode 100644 index 000000000000..ec536d2a1ec8 --- /dev/null +++ b/clippy_lints/src/mod_lib.rs @@ -0,0 +1,46 @@ +use clippy_utils::diagnostics::span_lint_and_help; +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! { + /// ### 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" +} + +#[derive(Default)] +pub struct ModLib; + +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 { + 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", + ); + } + } + } +} diff --git a/src/docs/mod_lib.txt b/src/docs/mod_lib.txt new file mode 100644 index 000000000000..92a0504a7dde --- /dev/null +++ b/src/docs/mod_lib.txt @@ -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 +``` \ No newline at end of file diff --git a/tests/ui/mod_lib/lib.rs b/tests/ui/mod_lib/lib.rs new file mode 100644 index 000000000000..00d01a6bfd3f --- /dev/null +++ b/tests/ui/mod_lib/lib.rs @@ -0,0 +1,3 @@ +pub fn something() -> i32 { + 42 +} diff --git a/tests/ui/mod_lib/lib.stderr b/tests/ui/mod_lib/lib.stderr new file mode 100644 index 000000000000..5aa85d70bc27 --- /dev/null +++ b/tests/ui/mod_lib/lib.stderr @@ -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`. diff --git a/tests/ui/mod_lib/main.rs b/tests/ui/mod_lib/main.rs new file mode 100644 index 000000000000..7e97a1e2c7d2 --- /dev/null +++ b/tests/ui/mod_lib/main.rs @@ -0,0 +1,8 @@ +#![allow(unused)] +#![warn(clippy::mod_lib)] + +mod lib; + +fn main() { + println!("{}", lib::something()); +} diff --git a/tests/ui/mod_lib/main.stderr b/tests/ui/mod_lib/main.stderr new file mode 100644 index 000000000000..0b9aac11f7bf --- /dev/null +++ b/tests/ui/mod_lib/main.stderr @@ -0,0 +1,20 @@ +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 + | +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: aborting due to 2 previous errors +