Skip to content

Commit 9659ff7

Browse files
committed
Auto merge of #7235 - camsteffen:manual-unwrap-or-deref, r=flip1995
Fix another manual_unwrap_or deref FP changelog: none (since this just piggybacks on #7233) Fixes #6960
2 parents 6fcdb8a + 8356c48 commit 9659ff7

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

clippy_lints/src/manual_unwrap_or.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ 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;
1514
use rustc_session::{declare_lint_pass, declare_tool_lint};
1615
use rustc_span::sym;
1716

@@ -54,21 +53,6 @@ impl LateLintPass<'_> for ManualUnwrapOr {
5453
}
5554
}
5655

57-
#[derive(Copy, Clone)]
58-
enum Case {
59-
Option,
60-
Result,
61-
}
62-
63-
impl Case {
64-
fn unwrap_fn_path(&self) -> &str {
65-
match self {
66-
Case::Option => "Option::unwrap_or",
67-
Case::Result => "Result::unwrap_or",
68-
}
69-
}
70-
}
71-
7256
fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
7357
fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
7458
if_chain! {
@@ -87,9 +71,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
8771
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
8872
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
8973
if path_to_local_id(unwrap_arm.body, binding_hir_id);
74+
if cx.typeck_results().expr_adjustments(unwrap_arm.body).is_empty();
9075
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(..))));
9376
then {
9477
Some(or_arm)
9578
} else {
@@ -101,10 +84,10 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
10184
if_chain! {
10285
if let ExprKind::Match(scrutinee, match_arms, _) = expr.kind;
10386
let ty = cx.typeck_results().expr_ty(scrutinee);
104-
if let Some(case) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
105-
Some(Case::Option)
87+
if let Some(ty_name) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
88+
Some("Option")
10689
} else if is_type_diagnostic_item(cx, ty, sym::result_type) {
107-
Some(Case::Result)
90+
Some("Result")
10891
} else {
10992
None
11093
};
@@ -127,7 +110,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
127110
span_lint_and_sugg(
128111
cx,
129112
MANUAL_UNWRAP_OR, expr.span,
130-
&format!("this pattern reimplements `{}`", case.unwrap_fn_path()),
113+
&format!("this pattern reimplements `{}::unwrap_or`", ty_name),
131114
"replace with",
132115
format!(
133116
"{}.unwrap_or({})",

tests/ui/manual_unwrap_or.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,11 @@ fn format_name(name: Option<&Rc<str>>) -> &str {
171171
}
172172
}
173173

174+
fn implicit_deref_ref() {
175+
let _: &str = match Some(&"bye") {
176+
None => "hi",
177+
Some(s) => s,
178+
};
179+
}
180+
174181
fn main() {}

tests/ui/manual_unwrap_or.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,11 @@ fn format_name(name: Option<&Rc<str>>) -> &str {
213213
}
214214
}
215215

216+
fn implicit_deref_ref() {
217+
let _: &str = match Some(&"bye") {
218+
None => "hi",
219+
Some(s) => s,
220+
};
221+
}
222+
216223
fn main() {}

0 commit comments

Comments
 (0)