-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Show notice about "never used" of Debug for enum #124460
Open
long-long-float
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
long-long-float:show-notice-about-enum-with-debug
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−1
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
#![deny(dead_code)] | ||
|
||
#[derive(Clone)] | ||
enum Enum { | ||
enum Enum1 { | ||
Variant1, //~ ERROR: variant `Variant1` is never constructed | ||
Variant2, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Enum2 { | ||
Variant1(i32), //~ ERROR: variant `Variant1` is never constructed | ||
long-long-float marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Variant2, | ||
} | ||
|
||
fn main() { | ||
let e = Enum::Variant2; | ||
let e = Enum1::Variant2; | ||
e.clone(); | ||
|
||
let _ = Enum2::Variant2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
error: variant `Variant1` is never constructed | ||
--> $DIR/unused-variant.rs:5:5 | ||
| | ||
LL | enum Enum { | ||
| ---- variant in this enum | ||
LL | enum Enum1 { | ||
| ----- variant in this enum | ||
LL | Variant1, | ||
| ^^^^^^^^ | ||
| | ||
= note: `Enum` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis | ||
= note: `Enum1` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis | ||
note: the lint level is defined here | ||
--> $DIR/unused-variant.rs:1:9 | ||
| | ||
LL | #![deny(dead_code)] | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
error: variant `Variant1` is never constructed | ||
--> $DIR/unused-variant.rs:11:5 | ||
| | ||
LL | enum Enum2 { | ||
| ----- variant in this enum | ||
LL | Variant1(i32), | ||
| ^^^^^^^^ | ||
| | ||
= note: `Enum2` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis | ||
|
||
error: aborting due to 2 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is making me nervous.
If I understand correctly, this is putting in a bit of an ad-hoc workaround, specifically into the dead-code lint pass, to hop up two parents instead of just one parent if the second parent is itself identified to be an enum.
i infer that is because it is trying to follow a line of reasoning like "the second parent will be an enum if and only if the first parent is an enum variant and the original item is a data field attached to that enum variant.
It is not immediately obvious to me that such an "if and only if" actually holds.
Even if it does hold, my second worry is that I imagine this same mistake probably presents itself in a bunch of other places (i.e., places where people assumed that the parent of a data field would be the ADT type that holds that field, but in fact one has to go up two parent-hops instead of one in cases like this), and I wonder if we would be better off using an API that let one express that intention directly, rather than putting logic like this everywhere it needs to go...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen issues around the Enum/Variant/CTor trichotomy, where the
DefId
of the item is expected for some analysis but you get passed one of the other two, so I'm sure there are more lurking around.Could you let us know what the contents of
parent_item
(what it points to, really) and offirst_item
are?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I talked to @estebank independently.
While I do think better API's might exist, I don't think I want to block this PR on hypothetical API improvements.
What I do want from this PR is for it to change the code slightly to include a few more, potentially redundant, safe-guards where you have the code check, as it does its double hop, that each item kind it encounters is the one you expect (e.g. taken from Enum/Variant/Ctor as appropriate.)
@rustbot author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for comment and sorry for the late reply.
I'd like to confirm my understanding. We need to check
encl_def_id
is a justVariant
(or other kind?) because other item such asCtor
also hasEnum
parent.