Skip to content

dead code lint is incorrectly reporting code as being unused with static methods #19613

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
erickt opened this issue Dec 7, 2014 · 6 comments
Closed
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@erickt
Copy link
Contributor

erickt commented Dec 7, 2014

I was playing with passing around a trait that only has static methods. It works okay, but it appears that this confuses the dead code lint. Here's an example:

trait Visitor<R> {
    fn foo();
}

fn visit<V: Visitor<R>, R>() {
    Visitor::<R>::foo()
}

struct V;

impl Visitor<()> for V {
    fn foo() { println!("hi") }
}

pub fn main() {
    visit()
}

Which warns with:

serde2.rs:9:1: 9:10 warning: struct is never used: `V`, #[warn(dead_code)] on by default
serde2.rs:9 struct V;
            ^~~~~~~~~

As best as I can tell, Rust is able to infer that there's only one implementation possible and uses it, but this information isn't conveyed to the lint, so it marks the struct as being unused.

@ghost
Copy link

ghost commented Dec 7, 2014

This should be as simple as visiting param substitutions for method calls in dead.rs.

@ghost ghost self-assigned this Dec 12, 2014
@kmcallister kmcallister added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 18, 2015
@TimNN
Copy link
Contributor

TimNN commented Aug 22, 2015

Adjusting the code for current rust, this no longer produces any warnings (although I did have to use V explicitly at one place, but I don't think this can be avoided):

trait Visitor<R> {
    fn foo();
}

fn visit<V: Visitor<R>, R>() {
    V::foo()
}

struct V;

impl Visitor<()> for V {
    fn foo() { println!("hi") }
}

fn main() {
    visit::<V, ()>()
}

@sfackler sfackler added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 22, 2015
@apasel422
Copy link
Contributor

I'm not sure that @TimNN's updated code exhibits the original issue, because it explicitly uses V.

@TimNN
Copy link
Contributor

TimNN commented Oct 27, 2015

I was thinking about that when I adapted the code. Do you know a way to avoid explicitly mentioning V?

@nagisa nagisa removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jun 14, 2016
@nagisa
Copy link
Member

nagisa commented Jun 14, 2016

From the duplicate issue:

struct A;
trait As {
    fn banana();
}

impl As for A {
    fn banana() { panic!("banana"); }
}

trait Bs<T: As> {
    fn do_it(&self) { T::banana() }
}

struct B;

impl Bs<A> for B {}

fn main() {
    Bs::do_it(&B)
}

When compiled, the dead_code lint fires, claiming that A is not used.

This code, when run, panics with banana, thus proving that A is, in fact, used.


Removing E-needstest, because the issue is not resolved.

@Mark-Simulacrum
Copy link
Member

Closing in favor of #18290.

lnicola pushed a commit to lnicola/rust that referenced this issue Apr 28, 2025
fix: Fix a panic when a trait method in an impl declares a lifetime parameter not in the trait declaration
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.
Projects
None yet
Development

No branches or pull requests

7 participants