Skip to content

Commit f468d82

Browse files
committed
Fix manual_map suggestion for if let.. else ... if let.. else chain
1 parent 6595d55 commit f468d82

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

clippy_lints/src/manual_map.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
44
use clippy_utils::ty::{can_partially_move_ty, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable};
5-
use clippy_utils::{is_allowed, match_def_path, match_var, paths, peel_hir_expr_refs};
5+
use clippy_utils::{is_allowed, is_else_clause_of_if_let_else, match_def_path, match_var, paths, peel_hir_expr_refs};
66
use rustc_ast::util::parser::PREC_POSTFIX;
77
use rustc_errors::Applicability;
88
use rustc_hir::{
99
def::Res,
1010
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
11-
Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath,
11+
Arm, BindingAnnotation, Block, Expr, ExprKind, MatchSource, Mutability, Pat, PatKind, Path, QPath,
1212
};
1313
use rustc_lint::{LateContext, LateLintPass, LintContext};
1414
use rustc_middle::lint::in_external_macro;
@@ -51,8 +51,11 @@ impl LateLintPass<'_> for ManualMap {
5151
return;
5252
}
5353

54-
if let ExprKind::Match(scrutinee, [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], _) =
55-
expr.kind
54+
if let ExprKind::Match(
55+
scrutinee,
56+
[arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }],
57+
match_kind,
58+
) = expr.kind
5659
{
5760
let (scrutinee_ty, ty_ref_count, ty_mutability) =
5861
peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
@@ -178,7 +181,12 @@ impl LateLintPass<'_> for ManualMap {
178181
expr.span,
179182
"manual implementation of `Option::map`",
180183
"try this",
181-
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str),
184+
if matches!(match_kind, MatchSource::IfLetDesugar { .. }) && is_else_clause_of_if_let_else(cx.tcx, expr)
185+
{
186+
format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str)
187+
} else {
188+
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str)
189+
},
182190
app,
183191
);
184192
}

clippy_utils/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,26 @@ pub fn get_parent_as_impl(tcx: TyCtxt<'_>, id: HirId) -> Option<&Impl<'_>> {
798798
}
799799
}
800800

801+
/// Checks if the given expression is the else clause in the expression `if let .. {} else {}`
802+
pub fn is_else_clause_of_if_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
803+
let map = tcx.hir();
804+
let mut iter = map.parent_iter(expr.hir_id);
805+
let arm_id = match iter.next() {
806+
Some((id, Node::Arm(..))) => id,
807+
_ => return false,
808+
};
809+
match iter.next() {
810+
Some((
811+
_,
812+
Node::Expr(Expr {
813+
kind: ExprKind::Match(_, [_, else_arm], kind),
814+
..
815+
}),
816+
)) => else_arm.hir_id == arm_id && matches!(kind, MatchSource::IfLetDesugar { .. }),
817+
_ => false,
818+
}
819+
}
820+
801821
/// Checks whether the given expression is a constant integer of the given value.
802822
/// unlike `is_integer_literal`, this version does const folding
803823
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)