Skip to content

Commit f62f269

Browse files
authored
Fix manual_unwrap_or_default FP on ref binding (#14731)
Closes #14716 changelog: [`manual_unwrap_or_default`] fix FP on ref binding
2 parents 003fc6c + 7106e21 commit f62f269

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

clippy_lints/src/matches/manual_unwrap_or.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::consts::ConstEvalCtxt;
22
use clippy_utils::source::{SpanRangeExt as _, indent_of, reindent_multiline};
3+
use rustc_ast::{BindingMode, ByRef};
34
use rustc_errors::Applicability;
45
use rustc_hir::def::Res;
56
use rustc_hir::{Arm, Expr, ExprKind, HirId, LangItem, Pat, PatExpr, PatExprKind, PatKind, QPath};
@@ -16,7 +17,7 @@ use super::{MANUAL_UNWRAP_OR, MANUAL_UNWRAP_OR_DEFAULT};
1617

1718
fn get_some(cx: &LateContext<'_>, pat: &Pat<'_>) -> Option<HirId> {
1819
if let PatKind::TupleStruct(QPath::Resolved(_, path), &[pat], _) = pat.kind
19-
&& let PatKind::Binding(_, pat_id, _, _) = pat.kind
20+
&& let PatKind::Binding(BindingMode(ByRef::No, _), pat_id, _, _) = pat.kind
2021
&& let Some(def_id) = path.res.opt_def_id()
2122
// Since it comes from a pattern binding, we need to get the parent to actually match
2223
// against it.

tests/ui/manual_unwrap_or_default.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,16 @@ fn issue_12928() {
106106
fn allowed_manual_unwrap_or_zero() -> u32 {
107107
Some(42).unwrap_or_default()
108108
}
109+
110+
mod issue14716 {
111+
struct Foo {
112+
name: Option<String>,
113+
}
114+
115+
fn bar(project: &Foo) {
116+
let _name = match project.name {
117+
Some(ref x) => x,
118+
None => "",
119+
};
120+
}
121+
}

tests/ui/manual_unwrap_or_default.rs

+13
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ fn allowed_manual_unwrap_or_zero() -> u32 {
147147
0
148148
}
149149
}
150+
151+
mod issue14716 {
152+
struct Foo {
153+
name: Option<String>,
154+
}
155+
156+
fn bar(project: &Foo) {
157+
let _name = match project.name {
158+
Some(ref x) => x,
159+
None => "",
160+
};
161+
}
162+
}

0 commit comments

Comments
 (0)