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

Add new lint unneeded_struct_pattern #13465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SpriteOvO
Copy link
Contributor

Closes #13400.

changelog: [unneeded_struct_pattern]: Add new lint

@rustbot
Copy link
Collaborator

rustbot commented Sep 26, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 26, 2024
@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch 5 times, most recently from d1a94c7 to 4733775 Compare September 26, 2024 19:55
"struct pattern is not needed for a unit variant",
"remove the struct pattern",
String::new(),
Applicability::Unspecified,
Copy link
Member

Choose a reason for hiding this comment

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

Suppose it can be MachineApplicable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. 👍

@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch 2 times, most recently from 3b45390 to 36b1025 Compare October 2, 2024 07:16
@bors
Copy link
Contributor

bors commented Oct 13, 2024

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

@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch 2 times, most recently from 49f5231 to d5ae91d Compare October 15, 2024 18:01
@SpriteOvO
Copy link
Contributor Author

Rebased. r? @alex-semenyuk

@rustbot
Copy link
Collaborator

rustbot commented Oct 15, 2024

Failed to set assignee to alex-semenyuk: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@bors
Copy link
Contributor

bors commented Nov 2, 2024

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

Copy link
Member

@Centri3 Centri3 Nov 2, 2024

Choose a reason for hiding this comment

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

I have a couple cases for you to consider, both whether we should lint them (imo yes) and whether it works on them.

Patterns in if let & let ... else
Patterns in function parameters: fn a(A::Empty {}: A) {}
Patterns in for: A::Empty {}

Fundamentally these are all likely the same, but they shouldn't be forgotten :) There may be some weird edge cases with them. (Though I don't think your changes will ever do so)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, added test cases for them.

clippy_lints/src/unneeded_struct_pattern.rs Show resolved Hide resolved
clippy_lints/src/unneeded_struct_pattern.rs Show resolved Hide resolved
let enum_did = cx.tcx.parent(did);
let variant = cx.tcx.adt_def(enum_did).variant_with_id(did);

let has_fields_brackets = !(variant.ctor.is_some() && variant.fields.is_empty());
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if the negation is done later, in the if. This roundabout way did confuse me at first (considering the name).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. After re-reading this code I wrote 2 months later, I am also getting confused. 😅 Seems I had misnamed the variable.

/// ```
#[clippy::version = "1.83.0"]
pub UNNEEDED_STRUCT_PATTERN,
nursery,
Copy link
Member

Choose a reason for hiding this comment

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

Any specific issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed it to style.

@Centri3
Copy link
Member

Centri3 commented Dec 2, 2024

Hey @SpriteOvO, been almost a month and I'm pinging from triage. Has there been any progress? Is there anything you're stuck on or have any questions?

@SpriteOvO
Copy link
Contributor Author

@Centri3 Oh, sorry, I totally missed this in my inbox. I'll finish it in the next days.

@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch 3 times, most recently from 1e6f710 to 79bed8a Compare December 3, 2024 19:06
Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

Just two things to consider, looks fine otherwise.

_ => 0,
};

if let Custom::HasFields { field: value } = Custom::Init {} // Should be ignored
Copy link
Member

Choose a reason for hiding this comment

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

This is some very misleading formatting 😅 maybe add a statement within the block to make it clear these brackets are a block and not part of the value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I have added a noop(); call in the block.

let Custom::HasBracketsNoFields {} = Custom::Init else {
panic!()
};
// Should be ignored
Copy link
Member

Choose a reason for hiding this comment

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

nit: Note that these could be replaced with error annotations but both are used interchangeably in clippy atm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch from 79bed8a to 3760e75 Compare December 15, 2024 02:51
@SpriteOvO SpriteOvO force-pushed the unneeded_struct_pattern branch from 3760e75 to 52abb8f Compare December 15, 2024 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Encourage removing { .. } from a pattern if enum variant is field-less
5 participants