Skip to content

Commit 5b4b7bc

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

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

clippy_lints/src/manual_map.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use crate::{
22
map_unit_fn::OPTION_MAP_UNIT_FN,
33
matches::MATCH_AS_REF,
44
utils::{
5-
can_partially_move_ty, is_allowed, is_type_diagnostic_item, match_def_path, match_var, paths,
6-
peel_hir_expr_refs, peel_mid_ty_refs_is_mutable, snippet_with_applicability, snippet_with_context,
7-
span_lint_and_sugg,
5+
can_partially_move_ty, is_allowed, is_else_clause_of_if_let_else, is_type_diagnostic_item, match_def_path,
6+
match_var, paths, peel_hir_expr_refs, peel_mid_ty_refs_is_mutable, snippet_with_applicability,
7+
snippet_with_context, span_lint_and_sugg,
88
},
99
};
1010
use rustc_ast::util::parser::PREC_POSTFIX;
1111
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, 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,7 +185,12 @@ 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_clause_of_if_let_else(cx.tcx, expr)
189+
{
190+
format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str)
191+
} else {
192+
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str)
193+
},
186194
app,
187195
);
188196
}

clippy_utils/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,26 @@ pub fn walk_ptrs_ty_depth(ty: Ty<'_>) -> (Ty<'_>, usize) {
10021002
inner(ty, 0)
10031003
}
10041004

1005+
/// Checks if the given expression is the else clause in the expression `if let .. {} else {}`
1006+
pub fn is_else_clause_of_if_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
1007+
let map = tcx.hir();
1008+
let mut iter = map.parent_iter(expr.hir_id);
1009+
let arm_id = match iter.next() {
1010+
Some((id, Node::Arm(..))) => id,
1011+
_ => return false,
1012+
};
1013+
match iter.next() {
1014+
Some((
1015+
_,
1016+
Node::Expr(Expr {
1017+
kind: ExprKind::Match(_, [_, else_arm], kind),
1018+
..
1019+
}),
1020+
)) => else_arm.hir_id == arm_id && matches!(kind, MatchSource::IfLetDesugar { .. }),
1021+
_ => false,
1022+
}
1023+
}
1024+
10051025
/// Checks whether the given expression is a constant integer of the given value.
10061026
/// unlike `is_integer_literal`, this version does const folding
10071027
pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool {

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 Some(0).is_some() {
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(_) = 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

+20-1
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,24 @@ 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: redundant pattern matching, consider using `is_some()`
176+
--> $DIR/manual_map_option.rs:191:12
177+
|
178+
LL | if let Some(_) = Some(0) {
179+
| -------^^^^^^^---------- help: try this: `if Some(0).is_some()`
180+
|
181+
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
182+
183+
error: manual implementation of `Option::map`
184+
--> $DIR/manual_map_option.rs:193:12
185+
|
186+
LL | } else if let Some(x) = Some(0) {
187+
| ____________^
188+
LL | | Some(x + 1)
189+
LL | | } else {
190+
LL | | None
191+
LL | | };
192+
| |_____^ help: try this: `{ Some(0).map(|x| x + 1) }`
193+
194+
error: aborting due to 21 previous errors
176195

0 commit comments

Comments
 (0)