Skip to content

Wrong unused_import warning #50537

Closed
Closed
@dbrgn

Description

@dbrgn

With this code:

fn main() { }

#[cfg(test)]
mod test_helpers {
    pub(crate) trait Doubler {
        fn doubled(&self) -> Self;
    }

    impl Doubler for u8 {
        fn doubled(&self) -> u8 { self * 2 }
    }
}

#[cfg(test)]
mod tests {
    use ::test_helpers::Doubler;

    mod inner {
        use super::*;

        #[test]
        fn test_doubler() {
            assert_eq!(21u8.doubled(), 42);
        }
    }
}

...I get the following warning when running cargo test:

warning: unused import: `::test_helpers::Doubler`
  --> src/main.rs:16:9
   |
16 |     use ::test_helpers::Doubler;
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: #[warn(unused_imports)] on by default

If the warning is correct, I should be able to remove it and the code should still work. But of course if I remove the import, the follwing happens:

warning: unused import: `super::*`
  --> src/main.rs:17:13
   |
17 |         use super::*;
   |             ^^^^^^^^
   |
   = note: #[warn(unused_imports)] on by default

error[E0599]: no method named `doubled` found for type `u8` in the current scope
  --> src/main.rs:21:29
   |
21 |             assert_eq!(21u8.doubled(), 42);
   |                             ^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
           candidate #1: `use test_helpers::Doubler;`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions