-
Notifications
You must be signed in to change notification settings - Fork 2.4k
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False Positive: unused import #11050
Comments
Smaller reproduction (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4425eac0ba9d8e3ba7edd3ca594ac000): use std::collections::HashMap;
#[cfg(test)]
mod foo {
use super::*;
fn bar() {
_ = HashMap::<u32, u32>::new();
}
} |
Cargo fix is just forwarding the diagnostic from the compiler, not emitting it itself. This should be moved to rust-lang/rust. I understand why it looks wrong but also why the compiler emits the diagnostic; there are infinite possible cfgs and having a use behind a cfg shouldn't automatically make it count as used IMO. That said maybe we can improve the diagnostics, or improve the error for cfg(test) / --check-cfg values specifically |
But the cfg is active, no? |
Look at the output from the playground link:
Notice how the warning about the unused function only appears once, as does the warning about the import, but they appear in different sections: one says It's possible cargo could be smarter and not build a library if there are no doctests, but that seems quite tricky, and still doesn't help if there are doctests or integration tests. |
@jyn514 is really correct. From the perspective of a library target, it knows nothing inside
That's true. And I don't think it is feasible without touching rustdoc itself, right? IIRC only after compiling with rustdoc can cargo know if there are doctests. |
@weihanglo correct, at a minimum it requires a rust parser to know if there are any doc-comments at all and to be reliable it needs a full markdown parser. I don't think it's a good investment of time. |
It seems to change this behavior the tooling needs to change significantly. So, I close this because it does not seem to be a big issue. |
Problem
1 -
$ cargo test
(and rust analyzer in vscode) say this line is an unused import:use std::collections::HashMap;
. WhileHashMap
is used in this line:let mut map: HashMap<String, u8> = HashMap::new();
.What am I missing?
On Mac:
Output of
cargo test
:2 -
$ cargo fix
removes the import, then the build fails:Steps
No response
Possible Solution(s)
No response
Notes
No response
Version
The text was updated successfully, but these errors were encountered: