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

use anyhow::Result;treated as unused import if only tests use it #9236

Open
SichangHe opened this issue Jul 24, 2022 · 5 comments
Open

use anyhow::Result;treated as unused import if only tests use it #9236

SichangHe opened this issue Jul 24, 2022 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@SichangHe
Copy link

Summary

If I add

use anyhow::Result;

and use Result<T> only in my tests, clippy thinks this import is unused.

Lint Name

unused_imports

Reproducer

This is all the code you need to have this false warning:

use anyhow::Result;

fn main() {
    println!("Hello, world!");
}

#[test]
fn test() -> Result<()> {
    Ok(())
}

The warning:

$ cargo clippy
warning: unused import: `anyhow::Result`
 --> blah/src/main.rs:1:5
  |
1 | use anyhow::Result;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `blah` generated 1 warning

To show that this import is used, remove it:

fn main() {
    println!("Hello, world!");
}

#[test]
fn test() -> Result<()> {
    Ok(())
}

If you run the test, it shows an error:

$ cargo t
   Compiling blah
error[E0107]: this enum takes 2 generic arguments but 1 generic argument was supplied
   --> blah/src/main.rs:6:14
    |
6   | fn test() -> Result<()> {
    |              ^^^^^^ -- supplied 1 generic argument
    |              |
    |              expected 2 generic arguments
    |
note: enum defined here, with 2 generic parameters: `T`, `E`
   --> ~/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:504:10
    |
504 | pub enum Result<T, E> {
    |          ^^^^^^ -  -
help: add missing generic argument
    |
6   | fn test() -> Result<(), E> {
    |                       +++

For more information about this error, try `rustc --explain E0107`.
error: could not compile `blah` due to previous error

Version

rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: aarch64-apple-darwin
release: 1.62.1
LLVM version: 14.0.5

Additional Labels

No response

@SichangHe SichangHe added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 24, 2022
@Alexendoo
Copy link
Member

This warning is from rustc itself rather than Clippy. #[test] fns have #[cfg(test)] applied to them, so when not compiled in test mode the Result import is unused. The usual way to do tests is within a #[cfg(test)] module, with the imports also in the test module

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    use anyhow::Result;

    #[test]
    fn test() -> Result<()> {
        Ok(())
    }
}

@SichangHe
Copy link
Author

So, it is a bug from rustc I guess.

@Alexendoo
Copy link
Member

It's not a bug, the program when not compiling tests is essentially

use anyhow::Result;

fn main() {
    println!("Hello, world!");
}

Where the anyhow::Result import is unused. Test only imports should only be enabled when the tests themselves are enabled

@SichangHe
Copy link
Author

Good point. The import is unused from the point of building. It still looks wonky I guess.

Also, why are we not suggesting people to separate tests to a module? Is that too much restriction?

@Alexendoo
Copy link
Member

It's a reasonable suggestion, we don't have a lint for it probably because of some combination of it being difficult (due to how clippy works with conditional compilation) and nobody taking the time to implement it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants