rust icon indicating copy to clipboard operation
rust copied to clipboard

`special_module_name` triggered for a directory module

Open Tuetuopay opened this issue 3 years ago • 1 comments

Summary

I have a bin crate that has at its source root a directory named lib/ for a module named, well, lib.

When upgrading to Rust 1.65, I get a new lint warning telling me that there is a "module declaration for lib.rs" which is wrong because I have no lib.rs.

I know that it's a bad name for a module, and I'll probably rename it, but I wanted to let you know!

Lint Name

special_module_name

Reproducer

I tried this code:

# main.rs
mod lib;

fn main() {
    lib::foo();
}

# lib/mod.rs
pub fn foo() {
    println!("foo!");
}

I saw this happen:

warning: found module declaration for lib.rs
  --> api/src/main.rs:1:1
   |
 1 | mod lib;
   | ^^^^^^^^
   |
   = note: `#[warn(special_module_name)]` on by default
   = 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

I expected to see this happen: no lint warning.

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0

Additional Labels

No response

Tuetuopay avatar Nov 08 '22 18:11 Tuetuopay

I encounter this warning when I try to have the lib inside the bin project, aka the lib.rs and the main.rs in src/, like the following:

$ cargo new dual_bin_and_lib_special_module_name Use this src/main.rs contents:

mod lib;

fn main() {
    println!("Hello, world!{}", lib::add(1,2));
}

Make this file src/lib.rs:

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

$ cargo run:

$ cargo run
   Compiling dual_bin_and_lib_special_module_name v0.1.0 (/home/user/sandbox/rust/05_sandbox/dynamic_lib/dual_bin_and_lib_special_module_name)
warning: found module declaration for lib.rs
 --> /home/user/sandbox/rust/05_sandbox/dynamic_lib/dual_bin_and_lib_special_module_name/src/main.rs:1:1
  |
1 | 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: `#[warn(special_module_name)]` on by default

warning: `dual_bin_and_lib_special_module_name` (bin "dual_bin_and_lib_special_module_name") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/dual_bin_and_lib_special_module_name`
Hello, world!3

To workaround it, I've found this is needed:

diff -upr dual_bin_and_lib_special_module_name/Cargo.toml dual_bin_and_lib_special_module_name_fixed/Cargo.toml
--- dual_bin_and_lib_special_module_name/Cargo.toml	2024-04-05 22:57:08.746022868 +0200
+++ dual_bin_and_lib_special_module_name_fixed/Cargo.toml	2024-04-05 22:56:58.386023284 +0200
@@ -4,5 +4,7 @@ version = "0.1.0"
 edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+[lib]
+path = "src/lib2.rs"
 
 [dependencies]
Only in dual_bin_and_lib_special_module_name_fixed/src: lib2.rs
Only in dual_bin_and_lib_special_module_name/src: lib.rs
diff -upr dual_bin_and_lib_special_module_name/src/main.rs dual_bin_and_lib_special_module_name_fixed/src/main.rs
--- dual_bin_and_lib_special_module_name/src/main.rs	2024-04-05 22:57:18.569355835 +0200
+++ dual_bin_and_lib_special_module_name_fixed/src/main.rs	2024-04-05 22:56:58.386023284 +0200
@@ -1,5 +1,5 @@
-mod lib;
+mod lib2;
 
 fn main() {
-    println!("Hello, world!{}", lib::add(1,2));
+    println!("Hello, world!{}", lib2::add(1,2));
 }

cargo run

$ cargo run
   Compiling dual_bin_and_lib_special_module_name v0.1.0 (/home/user/sandbox/rust/05_sandbox/dynamic_lib/dual_bin_and_lib_special_module_name_fixed)
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/dual_bin_and_lib_special_module_name`
Hello, world!3

Is there a better way ? Thanks.

$ cargo --version
cargo 1.76.0-nightly

$ rustc -Vv
rustc 1.76.0-nightly (07dca489a 2024-02-04) (gentoo)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.6

correabuscar avatar Apr 05 '24 21:04 correabuscar

This response is a little late but it may help others

For @correabuscar's example the intention would be for you to remove mod lib and refer to it as dual_bin_and_lib_special_module_name::add rather than lib::add

Alexendoo avatar Jul 23 '24 18:07 Alexendoo

This response is a little late but it may help others

For @correabuscar's example the intention would be for you to remove mod lib and refer to it as dual_bin_and_lib_special_module_name::add rather than lib::add

Thanks, that works and I now understand what the warning meant:

= 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: #[warn(special_module_name)] on by default

correabuscar avatar Jul 23 '24 19:07 correabuscar