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

Fix incorrect suggestion for manual_unwrap_or_default #12961

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jun 19, 2024

Fixes #12928.

If this not a "simple" pattern, better not emit the lint.

changelog: Fix incorrect suggestion for manual_unwrap_or_default

@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2024

r? @Alexendoo

rustbot has assigned @Alexendoo.
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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 19, 2024
Comment on lines 63 to 66
if let PatKind::Tuple(elements, dotdot) = pat.kind
&& (elements.len() > 1 || dotdot.as_opt_usize().is_some())
{
return None;
}
let mut bindings = Vec::new();
pat.each_binding(|_, id, _, _| bindings.push(id));
if let &[id] = bindings.as_slice() {
Copy link
Member

Choose a reason for hiding this comment

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

Are pats other than Binding where this works? If not we should check that directly instead of the .each_binding + if let

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry I didn't understand what you said.

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry I missed a word or two there

Does the lint work with any patterns that aren't PatKind::Binding? I would guess if it doesn't work for tuple patterns it also wouldn't work for struct/slice patterns

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point, I should exclude everything that is not a variable binding.

@GuillaumeGomez GuillaumeGomez force-pushed the fix-manual_unwrap_or_default branch from f08b47f to b570002 Compare June 20, 2024 21:30
@GuillaumeGomez
Copy link
Member Author

I restricted the lint to bindings and added tests.

@@ -53,6 +53,7 @@ declare_lint_pass!(ManualUnwrapOrDefault => [MANUAL_UNWRAP_OR_DEFAULT]);

fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
if let PatKind::TupleStruct(QPath::Resolved(_, path), &[pat], _) = pat.kind
&& matches!(pat.kind, PatKind::Binding(..))
Copy link
Member

Choose a reason for hiding this comment

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

If we make this && let PatKind::Binding(....) to get the ID we can return that directly instead of https://github.com/rust-lang/rust-clippy/pull/12961/files#diff-4b97f32b70a6481322fccdf7cc1c61f815ba832fd5e961607e2f708c42d83ffcR64-R70

Copy link
Member Author

Choose a reason for hiding this comment

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

It's so obvious I wonder how I could not see it... Thanks!

@GuillaumeGomez GuillaumeGomez force-pushed the fix-manual_unwrap_or_default branch from b570002 to 54b45f7 Compare June 21, 2024 16:18
@GuillaumeGomez
Copy link
Member Author

Updated!

@Alexendoo
Copy link
Member

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Jun 21, 2024

📌 Commit 54b45f7 has been approved by Alexendoo

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jun 21, 2024

⌛ Testing commit 54b45f7 with merge 0ce07f6...

@bors
Copy link
Contributor

bors commented Jun 21, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Alexendoo
Pushing 0ce07f6 to master...

@bors bors merged commit 0ce07f6 into rust-lang:master Jun 21, 2024
8 checks passed
@GuillaumeGomez GuillaumeGomez deleted the fix-manual_unwrap_or_default branch June 21, 2024 17:49
@Alexendoo Alexendoo added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jun 25, 2024
@flip1995 flip1995 added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. labels Jul 18, 2024
@flip1995
Copy link
Member

rust-lang/rust#127904

@xFrednet xFrednet removed the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jul 18, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 20, 2024
…k-Simulacrum

[beta] Clippy backport

r? `@Mark-Simulacrum`

Really small backport this time:

- rust-lang/rust-clippy#12961

Fixes a quite annoying bug of this lint, that got into the last stable release 1.79.0. There were already 3 issues opened about it in the Clippy repo. It would be great to get this fixed for the next stable release.

I confirmed that this commit is already part of `master`
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.

manual_unwrap_or_default does not consider type changes and suggests wrong code
6 participants