Skip to content

Commit 7466ba0

Browse files
committed
Auto merge of #127904 - flip1995:clippy-beta-backport, r=Mark-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`
2 parents b06e8ad + db0c947 commit 7466ba0

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/tools/clippy/clippy_lints/src/manual_unwrap_or_default.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,14 @@ declare_lint_pass!(ManualUnwrapOrDefault => [MANUAL_UNWRAP_OR_DEFAULT]);
5353

5454
fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
5555
if let PatKind::TupleStruct(QPath::Resolved(_, path), &[pat], _) = pat.kind
56+
&& let PatKind::Binding(_, pat_id, _, _) = pat.kind
5657
&& let Some(def_id) = path.res.opt_def_id()
5758
// Since it comes from a pattern binding, we need to get the parent to actually match
5859
// against it.
5960
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
6061
&& cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
6162
{
62-
let mut bindings = Vec::new();
63-
pat.each_binding(|_, id, _, _| bindings.push(id));
64-
if let &[id] = bindings.as_slice() {
65-
Some(id)
66-
} else {
67-
None
68-
}
63+
Some(pat_id)
6964
} else {
7065
None
7166
}

src/tools/clippy/tests/ui/manual_unwrap_or_default.fixed

+21
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,24 @@ fn issue_12569() {
7272
0
7373
};
7474
}
75+
76+
// Should not warn!
77+
fn issue_12928() {
78+
let x = Some((1, 2));
79+
let y = if let Some((a, _)) = x { a } else { 0 };
80+
let y = if let Some((a, ..)) = x { a } else { 0 };
81+
let x = Some([1, 2]);
82+
let y = if let Some([a, _]) = x { a } else { 0 };
83+
let y = if let Some([a, ..]) = x { a } else { 0 };
84+
85+
struct X {
86+
a: u8,
87+
b: u8,
88+
}
89+
let x = Some(X { a: 0, b: 0 });
90+
let y = if let Some(X { a, .. }) = x { a } else { 0 };
91+
struct Y(u8, u8);
92+
let x = Some(Y(0, 0));
93+
let y = if let Some(Y(a, _)) = x { a } else { 0 };
94+
let y = if let Some(Y(a, ..)) = x { a } else { 0 };
95+
}

src/tools/clippy/tests/ui/manual_unwrap_or_default.rs

+21
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,24 @@ fn issue_12569() {
9696
0
9797
};
9898
}
99+
100+
// Should not warn!
101+
fn issue_12928() {
102+
let x = Some((1, 2));
103+
let y = if let Some((a, _)) = x { a } else { 0 };
104+
let y = if let Some((a, ..)) = x { a } else { 0 };
105+
let x = Some([1, 2]);
106+
let y = if let Some([a, _]) = x { a } else { 0 };
107+
let y = if let Some([a, ..]) = x { a } else { 0 };
108+
109+
struct X {
110+
a: u8,
111+
b: u8,
112+
}
113+
let x = Some(X { a: 0, b: 0 });
114+
let y = if let Some(X { a, .. }) = x { a } else { 0 };
115+
struct Y(u8, u8);
116+
let x = Some(Y(0, 0));
117+
let y = if let Some(Y(a, _)) = x { a } else { 0 };
118+
let y = if let Some(Y(a, ..)) = x { a } else { 0 };
119+
}

0 commit comments

Comments
 (0)