Skip to content
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

unused_imports false positive #120422

Closed
Kixunil opened this issue Jan 27, 2024 · 6 comments
Closed

unused_imports false positive #120422

Kixunil opened this issue Jan 27, 2024 · 6 comments
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.

Comments

@Kixunil
Copy link
Contributor

Kixunil commented Jan 27, 2024

Code

#![no_std]

#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

#[cfg(test)]
mod test {
    #[test]
    #[cfg(feature = "alloc")]
    fn test() {
        format!("foo");
    }
}

Current output

warning: unused `#[macro_use]` import
 --> src/lib.rs:4:1
  |
4 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Desired output

No warning.

Rationale and extra context

#[macro_use] is actually used. When I removed it I get compilation failure. And while it can be argued, this is not the best style and use alloc::format; is better, the lint should say it if it's the case. (But really, I think this belongs to clippy, not rustc.)

Other cases

No response

Rust Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6
rustc 1.77.0-nightly (5bd5d214e 2024-01-25)
binary: rustc
commit-hash: 5bd5d214effd494f4bafb29b3a7a2f6c2070ca5c
commit-date: 2024-01-25
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6


### Anything else?

_No response_
@Kixunil Kixunil added 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. labels Jan 27, 2024
@ChrisDenton
Copy link
Member

It's only used in #[cfg(test)] though? So it will be unused without that cfg.

@Noratrieb
Copy link
Member

Yeah, cfgs are not (and cannot really be) factored into these lints.

@Kixunil
Copy link
Contributor Author

Kixunil commented Jan 27, 2024

@ChrisDenton oh, good point, that's both true and still confusing.

@Nilstrieb IIRC there was some work to be able to at least suggest things in lints based on inactive cfg, maybe the same could work here?

@Noratrieb
Copy link
Member

There was (and I happened to do that work :3) but this is pretty unrelated and would require properly checking cfg-ed out code, which is exactly what we don't want to do and can't do.

@Kixunil
Copy link
Contributor Author

Kixunil commented Jan 27, 2024

Maybe a different approach would work: when running cargo test run with test config first, make a list of used items in tests and apply that list next when compiling non-test to emit "Note: this is used in test but not in non-test" message. Some hint like that would be very useful even if it's not perfect and there are probably edge cases.

@petrochenkov
Copy link
Contributor

All unused lints work after cfgs are expanded, this one is no different, so I'm going to close the issue.

The recent extension of unused_imports had some large effect on users in general, that is discussed in #121708 which is still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

4 participants