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 import of trait is not warned about if similar trait is defined and used inside the crate #25730

Closed
jtepe opened this issue May 23, 2015 · 1 comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-resolve Area: Path resolution

Comments

@jtepe
Copy link

jtepe commented May 23, 2015

If I import a trait from an external crate into my crate and not use it, the compiler warns me about the unused import. But if I have a trait inside my crate with the same method and implement it, the compiler does not emit a warning about the unused trait from outside.

Let's say I have this trait in its own library crate:

// library crate 'testtrait'
pub trait OutsideTrait {
    fn method(&self) -> String;
}

In my executable crate I have the following code:

// executable crate
extern crate testtrait;

use testtrait::OutsideTrait;

trait InsideTrait {
    fn function(&self) -> String;
}

#[derive(Debug)]
struct Foo;

impl InsideTrait for Foo {
    fn function(&self) -> String {
        format!("{:?}", *self)
    }
}

fn main() {
    let b = Foo;
    println!("{}", b.function());
}

Now the compiler warns me about the unused import testtrait::OutsideTrait
However, if I change the name of method to function in order to make OutsideTrait match InsideTrait, the compiler will not warn me about the unused import of testtrait::OutsideTrait.

pub trait OutsideTrait {
    fn function(&self) -> String;
}

This happens regardless of whether I add an additional parameter to the function in OutsideTrait or InsideTrait.
Note, that if I implement neither trait, the compiler will again warn me about the unused import of OutsideTrait, which is totally fine.

The warning is not emitted by the compiler but the issue is still valid. OutsideTrait is an unused import and should be reported as such.

@Gankra Gankra added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label May 23, 2015
@alexcrichton alexcrichton added the A-resolve Area: Path resolution label May 26, 2015
@alexcrichton
Copy link
Member

This is an artifact of the current implementation which is entirely housed in librustc_resolve. The resolution pass collects all traits which could be candidates for a function call, and then it relies on type checking later on to figure out which trait was actually used. All candidates, however, are considered "used imports" and the typechecking phase doesn't later come along and say "oh I didn't actually use this trait".

This may be difficult and/or invasive to fix.

Manishearth added a commit to Manishearth/rust that referenced this issue Apr 22, 2016
bors added a commit that referenced this issue May 4, 2016
Warn unused trait imports, rebased

Rebase of #30021.

Fix #25730.
bors added a commit that referenced this issue May 8, 2016
Warn unused trait imports, rebased

Rebase of #30021.

Fix #25730.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-resolve Area: Path resolution
Projects
None yet
Development

No branches or pull requests

3 participants