rust icon indicating copy to clipboard operation
rust copied to clipboard

dead_code warning on an used module.

Open Aex12 opened this issue 3 years ago • 0 comments

I've got recently a warning about unused_code when the code was in fact being used. This warning was reported both by my IDE and by the cargo run / build commands.

The warning was caused because I was using "mod some_module" in both main.rs and lib.rs, but I was importing it on my main.rs through lib.rs instead.

I think the compiler should throw the warning on the "mod some_module" statement in main.rs instead of reporting it at some_module.rs

This repo contains a minimal reproducible error: https://github.com/Aex12/rust_error_unused_code

// main.rs
mod some_module;

use my_crate::some_module::some_function;

fn main() {
    let input = "lorem ipsum";
    let output = some_function(input);
    println!("{}", output);
}

// lib.rs
pub mod some_module;

// some_module.rs
pub fn some_function (input: &str) -> &str {
    input
}

The current output is:

warning: function `some_function` is never used
 --> src/some_module.rs:2:8
  |
2 | pub fn some_function (input: &str) -> &str {
  |        ^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `error_unused_code` (bin "error_unused_code") generated 1 warning

Ideally the output should look like:

warning: mod statement `mod some_module` is never used
 --> src/main.rs:1:1
  |
2 | mod some_module
  |     ^^^^^^^^^^^

Or maybe something like this:

warning: mod statement `mod some_module` is already declared on lib.rs:1:1
 --> src/main.rs:1:1
  |
2 | mod some_module
  |     ^^^^^^^^^^^

Aex12 avatar Aug 29 '22 18:08 Aex12