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

diagnostics: dead_code does not track trait impls (false negative: unused struct with Clone derived) #57613

Closed
matthiaskrgr opened this issue Jan 14, 2019 · 6 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

there's no dead_code warning for

#[derive(Clone)]
struct A {
    b: i32,
}

fn main(){}

while

struct A {
    b: i32,
}

fn main(){}

correctly warns:

warning: struct is never constructed: `A`
 --> src/main.rs:2:1
  |
2 | struct A {
  | ^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

reproduces with stable 1.31.1, beta 1.32.0-beta.14 and nightly 1.33.0 2019.01.13

@jethrogb
Copy link
Contributor

Dupe of #47851

@matthiaskrgr
Copy link
Member Author

Interesting..

why is this only a problem for #[derive(Clone)] but not for #[derive(Debug)] though?

@jethrogb
Copy link
Contributor

I'm guessing because clone returns Self

@Centril Centril added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Jan 16, 2019
aloucks added a commit to aloucks/vki that referenced this issue Apr 21, 2019
No lint was being generated because of #[derive(Clone)].

rust-lang/rust#57613
aloucks added a commit to aloucks/vki that referenced this issue Jun 2, 2019
No lint was being generated because of #[derive(Clone)].

rust-lang/rust#57613
@jakubadamw
Copy link
Contributor

jakubadamw commented Aug 6, 2019

Is this really a bug? Surely, in the case of a struct with Clone derived emitting that particular warning that we see in the second example would itself be a bug, because the implementation of Clone does in fact construct a struct of the type it's provided for. Yes, there could perhaps be another class of warning such as: struct is never constructed independently of an existing value of that struct's type. Is it, however, in scope for this lint to reason about construction sites like this? I'm not sure. I think it's fair game for this lint to give non-trivial false negatives. It's a bug if it gives false positives.

@SimonSapin
Copy link
Contributor

If you come up with a very simple definition of "used" and apply it literally, then yes the struct is used in the Clone impl and not emitting a warning is correct by that definition.

That’s not to say that such a definition is a useful one. In this case it’s clear that this struct is dead code. In the spirit of the what motivates this lint to exist, a warning should be emitted. In fact, the very simple definition is not the one currently in rustc:

struct Foo;

fn foo() {
    let _ = Foo;
}

(Playground)

Output:

warning: struct is never constructed: `Foo`
 --> src/lib.rs:1:1
  |
1 | struct Foo;
  | ^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

warning: function is never used: `foo`
 --> src/lib.rs:3:1
  |
3 | fn foo() {
  | ^^^^^^^^

The Foo struct is used in the foo function, but since the function itself is unused that used is considered not to count. This propagation is useful.

It appears that the real issue here is that (methods of) trait impl are not tracked as used/unused the way functions are.

@SimonSapin SimonSapin changed the title diagnostics: dead_code false negative: unused struct with Clone derived diagnostics: dead_code does not track trait impls (false negative: unused struct with Clone derived) Aug 7, 2019
@jonas-schievink jonas-schievink added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 20, 2020
@jonas-schievink
Copy link
Contributor

Closing as a duplicate of #47851

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-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants