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

Resolve the false positive dead_code lint when type is only used via destructuring #133128

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

m-ysk
Copy link
Contributor

@m-ysk m-ysk commented Nov 17, 2024

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.

@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2024

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@m-ysk m-ysk changed the title Resolve false positive dead_code lint when type is only used via destructuring Resolve the false positive dead_code lint when type is only used via destructuring Nov 17, 2024
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 17, 2024
@rust-log-analyzer

This comment has been minimized.

@jieyouxu jieyouxu added L-dead_code Lint: dead_code S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2024
@rust-log-analyzer

This comment has been minimized.

@m-ysk m-ysk force-pushed the feature/issue-132874 branch from 47d326f to b57a8bf Compare November 17, 2024 14:27
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@m-ysk m-ysk marked this pull request as ready for review November 18, 2024 14:06
@m-ysk
Copy link
Contributor Author

m-ysk commented Nov 18, 2024

@nnethercote
Hello. I've done all the works I can do for now though I'm not so confident. Could you review this?

@@ -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
Copy link
Contributor

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?

Copy link
Contributor

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.)

Copy link
Contributor

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.)

Copy link
Contributor Author

@m-ysk m-ysk Nov 24, 2024

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)

Copy link
Contributor Author

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?

Copy link
Contributor

@nnethercote nnethercote Dec 4, 2024

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.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 19, 2024
@m-ysk m-ysk requested a review from nnethercote November 28, 2024 15:19
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L-dead_code Lint: dead_code S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive dead_code lint when type is only used via destructuring
5 participants