Skip to content

use Trait; misses trait use within submodule #53682

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

Closed
cramertj opened this issue Aug 24, 2018 · 3 comments
Closed

use Trait; misses trait use within submodule #53682

cramertj opened this issue Aug 24, 2018 · 3 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@cramertj
Copy link
Member

I've seen this a bunch of times and always just thought that trait used-ness detection was broken, but it's actually something much more specific:

(playground)

trait MyTrait {
    fn trait_method(&self) {}
}
impl<T> MyTrait for T {}

pub struct MyStruct;

pub mod outer_mod {
    use super::{MyStruct, MyTrait};
    pub mod inner_mod {
        use super::*;
        pub fn call_trait_method() {
            5i32.trait_method()
        }
        pub fn return_struct() -> MyStruct {
            MyStruct
        }
    }
}

fn main() {
    let _ = outer_mod::inner_mod::call_trait_method();
    let _ = outer_mod::inner_mod::return_struct();
}

The code above produces the following warning:

warning: unused import: `MyTrait`
 --> src/main.rs:9:27
  |
9 |     use super::{MyStruct, MyTrait};
  |                           ^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

The use of used items through use super::*; in the submodule is handled inconsistently for types vs. traits. The use of the struct from the submodule inner_mod is detected and counted as a use of the use statement in outer_mod, but the same is not true of the use of the trait-- rustc misses that it is used via use super::*; in inner_mod and reports it as unused.

@ExpHP
Copy link
Contributor

ExpHP commented Aug 24, 2018

Dupe of #45268 ?

@estebank estebank added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Aug 24, 2018
@RalfJung
Copy link
Member

I just ran into the same problem, my minimized example is

mod helpers {
    pub trait IntExt {
        fn id(self) -> Self;
    }
    
    impl IntExt for i32 {
        fn id(self) -> Self { self }
    }
}

use helpers::IntExt;

pub mod user {
    use super::*;
    
    pub fn foo(x: i32) -> i32 {
        x.id()
    }
}

@jespersm
Copy link
Contributor

jespersm commented May 5, 2019

Yes, this is the same as #45268, which is fixed on master now. I've run your examples, no more wrong warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants