Skip to content

Commit ebf065e

Browse files
committed
Fix a manual_unwrap_or FP with deref coercion
1 parent 48dad26 commit ebf065e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

clippy_lints/src/manual_unwrap_or.rs

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::{Arm, Expr, ExprKind, PatKind};
1111
use rustc_lint::LintContext;
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::lint::in_external_macro;
14+
use rustc_middle::ty::adjustment::Adjust;
1415
use rustc_session::{declare_lint_pass, declare_tool_lint};
1516
use rustc_span::sym;
1617

@@ -87,6 +88,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
8788
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
8889
if path_to_local_id(unwrap_arm.body, binding_hir_id);
8990
if !contains_return_break_continue_macro(or_arm.body);
91+
if !cx.typeck_results().expr_adjustments(unwrap_arm.body).iter()
92+
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
9093
then {
9194
Some(or_arm)
9295
} else {

tests/ui/manual_unwrap_or.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ mod issue6965 {
163163
}
164164
}
165165

166+
use std::rc::Rc;
167+
fn format_name(name: Option<&Rc<str>>) -> &str {
168+
match name {
169+
None => "<anon>",
170+
Some(name) => name,
171+
}
172+
}
173+
166174
fn main() {}

tests/ui/manual_unwrap_or.rs

+8
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,12 @@ mod issue6965 {
205205
}
206206
}
207207

208+
use std::rc::Rc;
209+
fn format_name(name: Option<&Rc<str>>) -> &str {
210+
match name {
211+
None => "<anon>",
212+
Some(name) => name,
213+
}
214+
}
215+
208216
fn main() {}

0 commit comments

Comments
 (0)