Skip to content

Commit 1c9e965

Browse files
committed
Auto merge of rust-lang#12610 - ARandomDev99:manual_unwrap_or_default-12564, r=dswij
[`manual_unwrap_or_default`]: Check for Default trait implementation in initial condition when linting and use `IfLetOrMatch` Fixes rust-lang#12564 changelog: Fix [`manual_unwrap_or_default`] false positive when initial `match`/`if let` condition doesn't implement `Default` but the return type does.
2 parents 367f7aa + ac18c24 commit 1c9e965

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

clippy_lints/src/manual_unwrap_or_default.rs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use clippy_utils::sugg::Sugg;
21
use rustc_errors::Applicability;
32
use rustc_hir::def::Res;
43
use rustc_hir::{Arm, Expr, ExprKind, HirId, LangItem, MatchSource, Pat, PatKind, QPath};
54
use rustc_lint::{LateContext, LateLintPass, LintContext};
5+
use rustc_middle::ty::GenericArgKind;
66
use rustc_session::declare_lint_pass;
77
use rustc_span::sym;
88

99
use clippy_utils::diagnostics::span_lint_and_sugg;
10-
use clippy_utils::source::snippet_opt;
10+
use clippy_utils::higher::IfLetOrMatch;
11+
use clippy_utils::sugg::Sugg;
1112
use clippy_utils::ty::implements_trait;
1213
use clippy_utils::{in_constant, is_default_equivalent, peel_blocks, span_contains_comment};
1314

@@ -105,28 +106,49 @@ fn get_some_and_none_bodies<'tcx>(
105106
}
106107
}
107108

108-
fn handle_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
109-
let ExprKind::Match(match_expr, [arm1, arm2], MatchSource::Normal | MatchSource::ForLoopDesugar) = expr.kind else {
110-
return false;
109+
#[allow(clippy::needless_pass_by_value)]
110+
fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, expr: &'tcx Expr<'tcx>) {
111+
// Get expr_name ("if let" or "match" depending on kind of expression), the condition, the body for
112+
// the some arm, the body for the none arm and the binding id of the some arm
113+
let (expr_name, condition, body_some, body_none, binding_id) = match if_let_or_match {
114+
IfLetOrMatch::Match(condition, [arm1, arm2], MatchSource::Normal | MatchSource::ForLoopDesugar)
115+
// Make sure there are no guards to keep things simple
116+
if arm1.guard.is_none()
117+
&& arm2.guard.is_none()
118+
// Get the some and none bodies and the binding id of the some arm
119+
&& let Some(((body_some, binding_id), body_none)) = get_some_and_none_bodies(cx, arm1, arm2) =>
120+
{
121+
("match", condition, body_some, body_none, binding_id)
122+
},
123+
IfLetOrMatch::IfLet(condition, pat, if_expr, Some(else_expr), _)
124+
if let Some(binding_id) = get_some(cx, pat) =>
125+
{
126+
("if let", condition, if_expr, else_expr, binding_id)
127+
},
128+
_ => {
129+
// All other cases (match with number of arms != 2, if let without else, etc.)
130+
return;
131+
},
111132
};
112-
// We don't want conditions on the arms to simplify things.
113-
if arm1.guard.is_none()
114-
&& arm2.guard.is_none()
115-
// We check that the returned type implements the `Default` trait.
116-
&& let match_ty = cx.typeck_results().expr_ty(expr)
117-
&& let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
118-
&& implements_trait(cx, match_ty, default_trait_id, &[])
119-
// We now get the bodies for both the `Some` and `None` arms.
120-
&& let Some(((body_some, binding_id), body_none)) = get_some_and_none_bodies(cx, arm1, arm2)
133+
134+
// We check if the return type of the expression implements Default.
135+
let expr_type = cx.typeck_results().expr_ty(expr);
136+
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
137+
&& implements_trait(cx, expr_type, default_trait_id, &[])
138+
// We check if the initial condition implements Default.
139+
&& let Some(condition_ty) = cx.typeck_results().expr_ty(condition).walk().nth(1)
140+
&& let GenericArgKind::Type(condition_ty) = condition_ty.unpack()
141+
&& implements_trait(cx, condition_ty, default_trait_id, &[])
121142
// We check that the `Some(x) => x` doesn't do anything apart "returning" the value in `Some`.
122143
&& let ExprKind::Path(QPath::Resolved(_, path)) = peel_blocks(body_some).kind
123144
&& let Res::Local(local_id) = path.res
124145
&& local_id == binding_id
125146
// We now check the `None` arm is calling a method equivalent to `Default::default`.
126147
&& let body_none = peel_blocks(body_none)
127148
&& is_default_equivalent(cx, body_none)
128-
&& let Some(receiver) = Sugg::hir_opt(cx, match_expr).map(Sugg::maybe_par)
149+
&& let Some(receiver) = Sugg::hir_opt(cx, condition).map(Sugg::maybe_par)
129150
{
151+
// Machine applicable only if there are no comments present
130152
let applicability = if span_contains_comment(cx.sess().source_map(), expr.span) {
131153
Applicability::MaybeIncorrect
132154
} else {
@@ -136,57 +158,22 @@ fn handle_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
136158
cx,
137159
MANUAL_UNWRAP_OR_DEFAULT,
138160
expr.span,
139-
"match can be simplified with `.unwrap_or_default()`",
161+
format!("{expr_name} can be simplified with `.unwrap_or_default()`"),
140162
"replace it with",
141163
format!("{receiver}.unwrap_or_default()"),
142164
applicability,
143165
);
144166
}
145-
true
146-
}
147-
148-
fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
149-
if let ExprKind::If(cond, if_block, Some(else_expr)) = expr.kind
150-
&& let ExprKind::Let(let_) = cond.kind
151-
&& let ExprKind::Block(_, _) = else_expr.kind
152-
// We check that the returned type implements the `Default` trait.
153-
&& let match_ty = cx.typeck_results().expr_ty(expr)
154-
&& let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
155-
&& implements_trait(cx, match_ty, default_trait_id, &[])
156-
&& let Some(binding_id) = get_some(cx, let_.pat)
157-
// We check that the `Some(x) => x` doesn't do anything apart "returning" the value in `Some`.
158-
&& let ExprKind::Path(QPath::Resolved(_, path)) = peel_blocks(if_block).kind
159-
&& let Res::Local(local_id) = path.res
160-
&& local_id == binding_id
161-
// We now check the `None` arm is calling a method equivalent to `Default::default`.
162-
&& let body_else = peel_blocks(else_expr)
163-
&& is_default_equivalent(cx, body_else)
164-
&& let Some(if_let_expr_snippet) = snippet_opt(cx, let_.init.span)
165-
{
166-
let applicability = if span_contains_comment(cx.sess().source_map(), expr.span) {
167-
Applicability::MaybeIncorrect
168-
} else {
169-
Applicability::MachineApplicable
170-
};
171-
span_lint_and_sugg(
172-
cx,
173-
MANUAL_UNWRAP_OR_DEFAULT,
174-
expr.span,
175-
"if let can be simplified with `.unwrap_or_default()`",
176-
"replace it with",
177-
format!("{if_let_expr_snippet}.unwrap_or_default()"),
178-
applicability,
179-
);
180-
}
181167
}
182168

183169
impl<'tcx> LateLintPass<'tcx> for ManualUnwrapOrDefault {
184170
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
185171
if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
186172
return;
187173
}
188-
if !handle_match(cx, expr) {
189-
handle_if_let(cx, expr);
174+
// Call handle only if the expression is `if let` or `match`
175+
if let Some(if_let_or_match) = IfLetOrMatch::parse(cx, expr) {
176+
handle(cx, if_let_or_match, expr);
190177
}
191178
}
192179
}

tests/ui/manual_unwrap_or_default.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ fn main() {
1616

1717
let x: Option<Vec<String>> = None;
1818
x.unwrap_or_default();
19+
20+
// Issue #12564
21+
// No error as &Vec<_> doesn't implement std::default::Default
22+
let mut map = std::collections::HashMap::from([(0, vec![0; 3]), (1, vec![1; 3]), (2, vec![2])]);
23+
let x: &[_] = if let Some(x) = map.get(&0) { x } else { &[] };
24+
// Same code as above written using match.
25+
let x: &[_] = match map.get(&0) {
26+
Some(x) => x,
27+
None => &[],
28+
};
1929
}
2030

2131
// Issue #12531

tests/ui/manual_unwrap_or_default.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ fn main() {
3737
} else {
3838
Vec::default()
3939
};
40+
41+
// Issue #12564
42+
// No error as &Vec<_> doesn't implement std::default::Default
43+
let mut map = std::collections::HashMap::from([(0, vec![0; 3]), (1, vec![1; 3]), (2, vec![2])]);
44+
let x: &[_] = if let Some(x) = map.get(&0) { x } else { &[] };
45+
// Same code as above written using match.
46+
let x: &[_] = match map.get(&0) {
47+
Some(x) => x,
48+
None => &[],
49+
};
4050
}
4151

4252
// Issue #12531

tests/ui/manual_unwrap_or_default.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ LL | | };
5353
| |_____^ help: replace it with: `x.unwrap_or_default()`
5454

5555
error: match can be simplified with `.unwrap_or_default()`
56-
--> tests/ui/manual_unwrap_or_default.rs:46:20
56+
--> tests/ui/manual_unwrap_or_default.rs:56:20
5757
|
5858
LL | Some(_) => match *b {
5959
| ____________________^

0 commit comments

Comments
 (0)