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

Warn about dead tuple struct fields #92972

Closed

Conversation

FabianWolff
Copy link
Contributor

Fixes #92790. Since the introduction of warnings about unused fields (commit 0271224 in 2014), only named fields got warnings, but not positional fields in tuple structs or tuple enum variants.

Warning about positional fields is probably somewhat controversial because

  1. It is harder to fix than removing named fields, because removing a positional field changes the indices of the following fields, and pattern matches will also have to be adapted.
  2. It will uncover lots of dead fields in existing code, as it already did in the compiler code, which will lead to similar issues as in Ignore derived Clone and Debug implementations during dead code analysis #85200/Unactionable "field is never read" warning for printed structs with the Debug derive #88900.

I am putting this up for discussion now. Maybe this should be nominated for language team sign-off as well?

cc @shepmaster
r? @camelid

@rust-highfive
Copy link
Collaborator

Some changes occurred in clean/types.rs.

cc @camelid

Changes rustc_apfloat. rustc_apfloat is currently in limbo and you almost certainly don't want to change it (see #55993).

cc @eddyb

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jan 16, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 16, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@@ -2312,7 +2312,7 @@ impl TypeBinding {
crate enum SubstParam {
Type(Type),
Lifetime(Lifetime),
Constant(Constant),
Constant,
Copy link
Member

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. I'll have to look into it.

@@ -30,7 +30,7 @@ pub type DoubleDouble = DoubleFloat<ieee::Double>;
// FIXME: Implement all operations in DoubleDouble, and delete these
// semantics.
// FIXME(eddyb) This shouldn't need to be `pub`, it's only used in bounds.
pub struct FallbackS<F>(F);
pub struct FallbackS<F>(#[allow(dead_code)] F);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we had a better name for this than "dead code". I'm actually surprised it's not "unused" instead.

I suppose this change is small enough, and just a lint attribute, that it's probably fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you could use unused as well, but that is the lint group containing the dead_code lint, so the latter is more precise (not that it makes a difference here). Also, just to be clear: I didn't come up with the name here, it is also called dead_code for named fields.

In any case: Thanks for having a look!

@camelid
Copy link
Member

camelid commented Jan 21, 2022

I don't think I have enough knowledge of this code nor time to review this PR in its entirety, but I'll try to review some of the rustdoc changes when I get a chance. Thanks for working on this! :)

r? rust-lang/compiler

@rust-highfive rust-highfive assigned jackh726 and unassigned camelid Jan 21, 2022
@joshtriplett joshtriplett added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jan 21, 2022
@joshtriplett
Copy link
Member

I do think this needs a lang signoff; nominating.

@joshtriplett
Copy link
Member

Given that this may be harder for people to change, I think it deserves a separate sub-lint of dead_code, such as dead_tuple_field.

I do think this is a good idea, and we should make this change.

@joshtriplett
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 21, 2022
@bors
Copy link
Contributor

bors commented Jan 21, 2022

⌛ Trying commit ee9a896 with merge 4b8f3ac2f6e62978663873ba7b287f5df5f314d9...

@rust-log-analyzer
Copy link
Collaborator

The job dist-x86_64-linux failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] build_script_build test:false 0.574
error: field is never read: `0`
  --> library/core/src/iter/sources/empty.rs:28:23
   |
28 | struct FnReturning<T>(fn() -> T);
   |
   |
   = note: `-D dead-code` implied by `-D warnings`
[RUSTC-TIMING] core test:false 11.151
error: could not compile `core` due to previous error
Build completed unsuccessfully in 0:09:42

@bors
Copy link
Contributor

bors commented Jan 21, 2022

💔 Test failed - checks-actions

@bors bors 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-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2022
@joshtriplett
Copy link
Member

We discussed this in today's @rust-lang/lang meeting. We'd like to go with a gradual approach:

  1. Introduce this as dead_tuple_field, allow-by-default at first.
  2. Announce this, and tell people to try it.
  3. Later, bump it up to warn. People will have the option of disabling dead_tuple_field specifically, if they need to.

Thanks for working on this!

@Mark-Simulacrum Mark-Simulacrum removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Jan 25, 2022
@pnkfelix
Copy link
Member

pnkfelix commented Jan 26, 2022

I couldn't tell from skimming the PR: What does this warning do for fields of unit type, ()?

I ask because my first reaction when I read:

Warning about positional fields is probably somewhat controversial because

  1. It is harder to fix than removing named fields, because removing a positional field changes the indices of the following fields, and pattern matches will also have to be adapted.
    [...]

is that I thought: "well, we should let people include () fields (i.e. not warn about them), and perhaps even have the lint diagnostic here suggest either removing the field or making it have () type. That would keep the indices unchanged."

But, like I said above, I cannot tell from a quick read what this does for (). (I did see it does something special for unit structs (struct S;), but that is a slightly different situation and the handling there is to address a different problem.)

@bors
Copy link
Contributor

bors commented Feb 1, 2022

☔ The latest upstream changes (presumably #93548) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

Ping from triage:
@FabianWolff can you please address the comments, merge conflicts, build issues, and post your status on this PR?

@Dylan-DPC
Copy link
Member

closing this as inactive

@Dylan-DPC Dylan-DPC closed this Apr 8, 2022
@Dylan-DPC Dylan-DPC added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 8, 2022
@FabianWolff
Copy link
Contributor Author

@rustbot label -S-inactive

@Dylan-DPC Sorry for the delay here, I didn't mean to abandon this. I've updated the branch, can you reopen this PR, please?

Specifically, I have added this functionality as a new allow-by-default lint unused_tuple_struct_fields (name bikesheddable) instead of as part of dead_code, as requested in #92972 (comment).

Furthermore, I have disabled the lint for fields of unit type as requested in #92972 (comment), so error messages now look like this:

error: field is never read: `1`
  --> $DIR/tuple-struct-field.rs:6:21
   |
LL | struct Wrapper(i32, [u8; LEN], String);
   |                     ^^^^^^^^^
   |
help: change the field to unit type to suppress this warning while preserving the field numbering
   |
LL | struct Wrapper(i32, (), String);
   |                     ~~

@rustbot rustbot removed the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Apr 12, 2022
@mati865
Copy link
Contributor

mati865 commented Apr 12, 2022

@FabianWolff if you force push closed PR it can no longer be reopened.
Please create new one and include link to this one.

@FabianWolff
Copy link
Contributor Author

Ah, I didn't know this, I thought I was lacking the necessary permissions for reopening. Thanks for the hint @mati865!

@shepmaster
Copy link
Member

@Dylan-DPC you should mention the force-push caveat as part of your canned closing message.

@bjorn3
Copy link
Member

bjorn3 commented May 12, 2022

I believe you can reopen a PR if you change the commit of the branch back to what it was when you closed it.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Aug 2, 2022
… r=estebank

Warn about dead tuple struct fields

Continuation of rust-lang#92972. Fixes rust-lang#92790.

The language team has already commented on this in rust-lang#92972 (comment); I have incorporated their requests here. Specifically, there is now a new allow-by-default `unused_tuple_struct_fields` lint (name bikesheddable), and fields of unit type are ignored (rust-lang#92972 (comment)), so error messages look like this:
```
error: field is never read: `1`
  --> $DIR/tuple-struct-field.rs:6:21
   |
LL | struct Wrapper(i32, [u8; LEN], String);
   |                     ^^^^^^^^^
   |
help: change the field to unit type to suppress this warning while preserving the field numbering
   |
LL | struct Wrapper(i32, (), String);
   |                     ~~
```
r? `@joshtriplett`
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 5, 2022
…=estebank

Warn about dead tuple struct fields

Continuation of rust-lang#92972. Fixes rust-lang#92790.

The language team has already commented on this in rust-lang#92972 (comment); I have incorporated their requests here. Specifically, there is now a new allow-by-default `unused_tuple_struct_fields` lint (name bikesheddable), and fields of unit type are ignored (rust-lang#92972 (comment)), so error messages look like this:
```
error: field is never read: `1`
  --> $DIR/tuple-struct-field.rs:6:21
   |
LL | struct Wrapper(i32, [u8; LEN], String);
   |                     ^^^^^^^^^
   |
help: change the field to unit type to suppress this warning while preserving the field numbering
   |
LL | struct Wrapper(i32, (), String);
   |                     ~~
```
r? `@joshtriplett`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 19, 2024
…ent, r=Nilstrieb

dead_code treats #[repr(transparent)] the same as #[repr(C)]

In rust-lang#92972 we enabled linting on unused fields in tuple structs. In rust-lang#118297 that lint was enabled by default. That exposed issues like rust-lang#119659, where the fields of a struct marked `#[repr(transparent)]` were reported by the `dead_code` lint. The language team [decided](rust-lang#119659 (comment)) that the lint should treat `repr(transparent)` the same as `#[repr(C)]`.

Fixes rust-lang#119659
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 19, 2024
Rollup merge of rust-lang#120107 - shepmaster:dead-code-repr-transparent, r=Nilstrieb

dead_code treats #[repr(transparent)] the same as #[repr(C)]

In rust-lang#92972 we enabled linting on unused fields in tuple structs. In rust-lang#118297 that lint was enabled by default. That exposed issues like rust-lang#119659, where the fields of a struct marked `#[repr(transparent)]` were reported by the `dead_code` lint. The language team [decided](rust-lang#119659 (comment)) that the lint should treat `repr(transparent)` the same as `#[repr(C)]`.

Fixes rust-lang#119659
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-perf Status: Waiting on a perf run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dead_code lint does not trigger for tuple structs with unread fields