mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Auto merge of #12961 - GuillaumeGomez:fix-manual_unwrap_or_default, r=Alexendoo
Fix incorrect suggestion for `manual_unwrap_or_default` Fixes #12928. If this not a "simple" pattern, better not emit the lint. changelog: Fix incorrect suggestion for `manual_unwrap_or_default`
This commit is contained in:
commit
0ce07f61db
3 changed files with 44 additions and 7 deletions
|
@ -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
|
||||
&& 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.
|
||||
|
@ -60,13 +61,7 @@ fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
|
|||
&& (cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
|
||||
|| cx.tcx.lang_items().get(LangItem::ResultOk) == 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
|
||||
}
|
||||
|
|
|
@ -78,3 +78,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 };
|
||||
}
|
||||
|
|
|
@ -111,3 +111,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 };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue