-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Resolve the false positive dead_code lint when type is only used via destructuring #133128
base: master
Are you sure you want to change the base?
Conversation
r? @nnethercote rustbot has assigned @nnethercote. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
47d326f
to
b57a8bf
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@nnethercote |
@@ -12,10 +12,10 @@ fn field_read(f: Foo) -> usize { | |||
} | |||
|
|||
enum XYZ { | |||
X, //~ ERROR variants `X` and `Y` are never constructed | |||
X, //~ ERROR variant `X` is never constructed |
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.
Hmm, I'm not sure about this change. Because the old statement that Y
is never constructed is correct. And if a variant appears in a pattern but is never constructed I think it should be warned about, because that could genuinely indicate dead code.
So now I'm questioning the whole premise of #132874 and this PR. If anything, I think let Foo(_x) = get_thing::<Foo>();
should be made to give a warning. Does that seem reasonable?
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.
Also, it would be better if the commits were squashed together because they are all part of the same logical change. (But that may be moot if the PR doesn't go ahead.)
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.
(Also, you've done a nice job on the code and test changes, they look good if we are willing to go in this direction.)
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 your review. I understood that the code given in the issue #132874 should be warned as you explained.
Now, I'm thinking that there's a possibility that the issue owner actually found a bug, but the MRE in the issue was over-generalized because he said he found the issue when using zerocopy::FromBytes::read_from_bytes and that function contains extra language features (derive macro, trait) that have been missed in the MRE.
So, I'm asking to give us another MRE closer to the original code.
#132874 (comment)
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.
@nnethercote
The issue author gave us another repro as follows, which is closer to the real code.
#132874 (comment)
The new code indicates there are cases where a struct is actually used without being constructed in the normal way. I think this code should be compiled without any warning.
Though the best is to achieve both allowing the above code to be compiled without any warning and keeping the warning to the cases like tests/ui/lint/dead-code/lint-dead-code-4.rs
, I'm not sure whether it can be achieved.
So, can I have your advice on which direction 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.
That is a tricky case: a PacketHeader
is constructed, but via binary deserialization, which the compiler cannot understand. It seems like a false positive that is hard to avoid without introducing false negatives in other cases. I'm not sure how best to handle it. I have asked for other opinions in this Zulip thread.
Fixes #132874
Resolves the false positive dead_code lint when type is only used via destructuring reported by the issue #132874 .
The existing code seems to have forgotten to mark the variant itself as a live symbol while it marks the variants fields. This PR adds the code to mark the variant itself as a live symbol in order to resolve the false positive dead code.