Misleading dead code warning when loading separate modules that use the same source file #133197
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
util.rs
:lib.rs
:Current output
Desired output
Option 1: Disable dead code warning if code is used in any module
From this point of view, code that is used somewhere is not considered dead code, regardless of the module that uses it. However, if loading separate modules from the same source file was a mistake, this may make finding the issue harder.
Option 2: Enhance the dead code warning with module information
warning: function `get_random_number` is never used
, we could havewarning: function `util::get_random_number` is never used
orwarning: function `get_random_number` in module `util` is never used
note: function `get_random_number` was used as part of other modules: `util_lib`
Option 3: Disable dead code warning if code is used in any module, warn about separate modules using different names
This is the most obvious way to detect 2 modules mistakenly using the same file. But if there are many legitimate use case for using the same file for 2+ modules, something like
#[allow(mod_use_same_source)]
will need to be added to these existing projects to avoid the warning, which may be cumbersome.Rationale and extra context
The current warning is misleading, as the function is actually used. This is not dead code, just "dead code in the context of a specific module", but the warning doesn't indicate that.
In some cases it's easy to miss that the module is loaded twice under different names. If the module was loaded twice under the same name,
cargo check
would have returnederror[E0428]: the name util is defined multiple times
, which is very helpful.Other cases
The same warning is displayed if
#[cfg_attr(unix, path = "util.rs")]
is used instead of#[path = "util.rs"]
.Rust Version
Anything else?
Minimal reproduction: https://github.com/ilai-deutel/cfg-attr-dead-code/blob/main/src/lib.rs
Real occurrence: ilai-deutel/kibi#330
The text was updated successfully, but these errors were encountered: