-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix incorrect suggestion for manual_unwrap_or_default
#12961
Conversation
r? @Alexendoo rustbot has assigned @Alexendoo. Use |
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() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
f08b47f
to
b570002
Compare
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(..)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
b570002
to
54b45f7
Compare
Updated! |
Thanks! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
…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`
Fixes #12928.
If this not a "simple" pattern, better not emit the lint.
changelog: Fix incorrect suggestion for
manual_unwrap_or_default