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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
let index = self.typeck_results().field_index(pat.hir_id);
self.insert_def_id(variant.fields[index].did);
}
self.check_def_id(variant.def_id);
}

fn handle_tuple_field_pattern_match(
Expand All @@ -265,6 +266,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
}
self.insert_def_id(variant.fields[FieldIdx::from_usize(idx)].did);
}
self.check_def_id(variant.def_id);
}

fn handle_offset_of(&mut self, expr: &'tcx hir::Expr<'tcx>) {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/lint/dead-code/lint-dead-code-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Y {
a: String,
b: i32,
b: i32, //~ ERROR fields `b` and `c` are never read
c: i32,
},
Z
Expand Down
19 changes: 14 additions & 5 deletions tests/ui/lint/dead-code/lint-dead-code-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ note: the lint level is defined here
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: variants `X` and `Y` are never constructed
error: fields `b` and `c` are never read
--> $DIR/lint-dead-code-4.rs:18:9
|
LL | Y {
| - fields in this variant
LL | a: String,
LL | b: i32,
| ^
LL | c: i32,
| ^

error: variant `X` is never constructed
--> $DIR/lint-dead-code-4.rs:15:5
|
LL | enum XYZ {
| --- variants in this enum
| --- variant in this enum
LL | X,
| ^
LL | Y {
| ^

error: enum `ABC` is never used
--> $DIR/lint-dead-code-4.rs:24:6
Expand Down Expand Up @@ -62,5 +71,5 @@ LL | b: bool,
LL | c: bool,
| ^

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

17 changes: 17 additions & 0 deletions tests/ui/lint/dead-code/only-used-for-destructuring-struct.rs
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();
}
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();
}
Loading