Skip to content

Commit

Permalink
Auto merge of rust-lang#127904 - flip1995:clippy-beta-backport, r=Mar…
Browse files Browse the repository at this point in the history
…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`
  • Loading branch information
bors committed Jul 20, 2024
2 parents b06e8ad + db0c947 commit 7466ba0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/tools/clippy/clippy_lints/src/manual_unwrap_or_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,14 @@ 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
&& let PatKind::Binding(_, pat_id, _, _) = pat.kind
&& let Some(def_id) = path.res.opt_def_id()
// Since it comes from a pattern binding, we need to get the parent to actually match
// against it.
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
&& cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
{
let mut bindings = Vec::new();
pat.each_binding(|_, id, _, _| bindings.push(id));
if let &[id] = bindings.as_slice() {
Some(id)
} else {
None
}
Some(pat_id)
} else {
None
}
Expand Down
21 changes: 21 additions & 0 deletions src/tools/clippy/tests/ui/manual_unwrap_or_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,24 @@ fn issue_12569() {
0
};
}

// Should not warn!
fn issue_12928() {
let x = Some((1, 2));
let y = if let Some((a, _)) = x { a } else { 0 };
let y = if let Some((a, ..)) = x { a } else { 0 };
let x = Some([1, 2]);
let y = if let Some([a, _]) = x { a } else { 0 };
let y = if let Some([a, ..]) = x { a } else { 0 };

struct X {
a: u8,
b: u8,
}
let x = Some(X { a: 0, b: 0 });
let y = if let Some(X { a, .. }) = x { a } else { 0 };
struct Y(u8, u8);
let x = Some(Y(0, 0));
let y = if let Some(Y(a, _)) = x { a } else { 0 };
let y = if let Some(Y(a, ..)) = x { a } else { 0 };
}
21 changes: 21 additions & 0 deletions src/tools/clippy/tests/ui/manual_unwrap_or_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,24 @@ fn issue_12569() {
0
};
}

// Should not warn!
fn issue_12928() {
let x = Some((1, 2));
let y = if let Some((a, _)) = x { a } else { 0 };
let y = if let Some((a, ..)) = x { a } else { 0 };
let x = Some([1, 2]);
let y = if let Some([a, _]) = x { a } else { 0 };
let y = if let Some([a, ..]) = x { a } else { 0 };

struct X {
a: u8,
b: u8,
}
let x = Some(X { a: 0, b: 0 });
let y = if let Some(X { a, .. }) = x { a } else { 0 };
struct Y(u8, u8);
let x = Some(Y(0, 0));
let y = if let Some(Y(a, _)) = x { a } else { 0 };
let y = if let Some(Y(a, ..)) = x { a } else { 0 };
}

0 comments on commit 7466ba0

Please sign in to comment.