Skip to content

Commit 404665f

Browse files
committed
Fix manual_map suggestion for if let.. else ... if let.. else chain
1 parent 54def1e commit 404665f

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

clippy_lints/src/manual_map.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::Applicability;
1212
use rustc_hir::{
1313
def::Res,
1414
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
15-
Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath,
15+
Arm, BindingAnnotation, Block, Expr, ExprKind, MatchSource, Mutability, Node, Pat, PatKind, Path, QPath,
1616
};
1717
use rustc_lint::{LateContext, LateLintPass, LintContext};
1818
use rustc_middle::lint::in_external_macro;
@@ -55,8 +55,11 @@ impl LateLintPass<'_> for ManualMap {
5555
return;
5656
}
5757

58-
if let ExprKind::Match(scrutinee, [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], _) =
59-
expr.kind
58+
if let ExprKind::Match(
59+
scrutinee,
60+
[arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }],
61+
match_kind,
62+
) = expr.kind
6063
{
6164
let (scrutinee_ty, ty_ref_count, ty_mutability) =
6265
peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
@@ -182,13 +185,36 @@ impl LateLintPass<'_> for ManualMap {
182185
expr.span,
183186
"manual implementation of `Option::map`",
184187
"try this",
185-
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str),
188+
if matches!(match_kind, MatchSource::IfLetDesugar { .. }) && is_else_of_if_let_else(cx, expr) {
189+
format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str)
190+
} else {
191+
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str)
192+
},
186193
app,
187194
);
188195
}
189196
}
190197
}
191198

199+
fn is_else_of_if_let_else(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
200+
let map = cx.tcx.hir();
201+
let mut iter = map.parent_iter(expr.hir_id);
202+
let arm_id = match iter.next() {
203+
Some((id, Node::Arm(..))) => id,
204+
_ => return false,
205+
};
206+
match iter.next() {
207+
Some((
208+
_,
209+
Node::Expr(Expr {
210+
kind: ExprKind::Match(_, [_, else_arm], kind),
211+
..
212+
}),
213+
)) => else_arm.hir_id == arm_id && matches!(kind, MatchSource::IfLetDesugar { .. }),
214+
_ => false,
215+
}
216+
}
217+
192218
// Checks if the expression can be moved into a closure as is.
193219
fn can_move_expr_to_closure(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
194220
struct V<'cx, 'tcx> {

tests/ui/manual_map_option.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,9 @@ fn main() {
128128
None => None,
129129
};
130130
}
131+
132+
// #6847
133+
if let Some(x) = Some(0) {
134+
Some(0)
135+
} else { Some(0).map(|x| x + 1) };
131136
}

tests/ui/manual_map_option.rs

+9
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,13 @@ fn main() {
186186
None => None,
187187
};
188188
}
189+
190+
// #6847
191+
if let Some(x) = Some(0) {
192+
Some(0)
193+
} else if let Some(x) = Some(0) {
194+
Some(x + 1)
195+
} else {
196+
None
197+
};
189198
}

tests/ui/manual_map_option.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,16 @@ LL | | None => None,
172172
LL | | };
173173
| |_____^ help: try this: `option_env!("").map(String::from)`
174174

175-
error: aborting due to 19 previous errors
175+
error: manual implementation of `Option::map`
176+
--> $DIR/manual_map_option.rs:193:12
177+
|
178+
LL | } else if let Some(x) = Some(0) {
179+
| ____________^
180+
LL | | Some(x + 1)
181+
LL | | } else {
182+
LL | | None
183+
LL | | };
184+
| |_____^ help: try this: `{ Some(0).map(|x| x + 1) }`
185+
186+
error: aborting due to 20 previous errors
176187

0 commit comments

Comments
 (0)