-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Open
m-ysk
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
m-ysk:feature/issue-132874
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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.
+48
−7
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a65eb9
Add a test for issue-132874
m-ysk bd0faf4
Mark the variant itself as a live symbol
m-ysk b57a8bf
Use check_def_id
m-ysk a2e5e5c
Make the test file name descriptive
m-ysk 468e4c3
Add a test for struct case
m-ysk b4de452
Mark the variant itself as a live symbol in struct case
m-ysk bd79e23
Fix an affected test case
m-ysk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
17 changes: 17 additions & 0 deletions
17
tests/ui/lint/dead-code/only-used-for-destructuring-struct.rs
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//@ check-pass | ||
|
||
// Make sure we don't have any false positives here. | ||
|
||
#![deny(dead_code)] | ||
|
||
struct Foo { | ||
bar: usize, | ||
} | ||
|
||
fn get_thing<T>() -> T { | ||
todo!() | ||
} | ||
|
||
pub fn main() { | ||
let Foo { bar: _x } = get_thing(); | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/ui/lint/dead-code/only-used-for-destructuring-tuple-struct.rs
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//@ check-pass | ||
|
||
// Make sure we don't have any false positives here. | ||
|
||
#![deny(dead_code)] | ||
|
||
struct Foo(usize); | ||
|
||
fn get_thing<T>() -> T { todo!() } | ||
|
||
pub fn main() { | ||
let Foo(_x) = get_thing(); | ||
} |
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.
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.