Skip to content

Commit ce542b1

Browse files
committed
share the enhancements with the other branch as well
1 parent 1bd5b2c commit ce542b1

File tree

1 file changed

+34
-53
lines changed

1 file changed

+34
-53
lines changed

clippy_lints/src/duplicate_match_guards.rs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::Applicability;
55
use rustc_hir::{Arm, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
8-
use rustc_span::{BytePos, Span};
8+
use rustc_span::BytePos;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -73,18 +73,24 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
7373
if let ExprKind::If(cond, then, None) = arm_body_expr.kind
7474
&& eq_expr_value(cx, guard, cond.peel_drop_temps())
7575
{
76-
let ExprKind::Block(then_without_curlies, _) = then.kind else {
77-
unreachable!("the `then` expr in `ExprKind::If` is always `ExprKind::Block`")
78-
};
76+
// make sure that we won't swallow any comments. be extra conservative and bail out on _any_ comment
77+
// outside of `then`:
78+
//
79+
// <pat> if <guard> => { if <cond> <then> }
80+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
81+
let sm = cx.sess().source_map();
82+
if span_contains_comment(sm, arm.span.with_hi(then.span.lo()))
83+
|| span_contains_comment(sm, arm.span.with_lo(then.span.hi()))
84+
{
85+
return;
86+
}
7987

8088
// the two expressions may be syntactically different, even if identical
8189
// semantically -- the user might want to replace the condition in the guard
8290
// with the one in the body
8391
let mut applicability = Applicability::MaybeIncorrect;
8492

85-
let sugg = snippet_with_applicability(cx, then_without_curlies.span, "..", &mut applicability);
86-
87-
if body_has_block {
93+
let sugg_span = if body_has_block {
8894
// the common case:
8995
// ```
9096
// match 0u32 {
@@ -96,59 +102,34 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
96102
// }
97103
// ```
98104
//
99-
// suggest removing the `if` _and_ the curlies of the inner brace,
100-
// since the arm body already has braces
101-
102-
// make sure that we won't swallow any comments. be extra conservative and bail out on _any_ comment
103-
// outside of `then`:
104-
//
105-
// <pat> if <guard> => { if <cond> <then> }
106-
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
107-
let sm = cx.sess().source_map();
108-
if span_contains_comment(sm, arm.span.with_hi(then.span.lo()))
109-
|| span_contains_comment(sm, arm.span.with_lo(then.span.hi()))
110-
{
111-
return;
112-
}
113-
114-
let sugg = snippet_with_applicability(
115-
cx,
116-
then.span
117-
.with_lo(then.span.lo() + BytePos(1))
118-
.with_hi(then.span.hi() - BytePos(1)),
119-
"..",
120-
&mut applicability,
121-
);
122-
123-
span_lint_and_sugg(
124-
cx,
125-
DUPLICATE_MATCH_GUARDS,
126-
arm_body_expr.span,
127-
"condition duplicates match guard",
128-
"remove the condition",
129-
sugg.to_string(),
130-
applicability,
131-
);
105+
// the arm body already has curlies, so we can remove the ones around `then`
106+
then.span
107+
.with_lo(then.span.lo() + BytePos(1))
108+
.with_hi(then.span.hi() - BytePos(1))
132109
} else {
133-
// the uncommon case (rusfmt would add the braces here automatically)
110+
// the uncommon case (rusfmt would add the curlies here automatically)
134111
// ```
135112
// match 0u32 {
136113
// 0 if true => if true { return; }
137114
// }
138115
// ```
139116
//
140-
// suggest removing the `if` but _not_ the curlies of the inner brace,
141-
// since there are no outer braces coming from the arm body
142-
span_lint_and_sugg(
143-
cx,
144-
DUPLICATE_MATCH_GUARDS,
145-
arm_body_expr.span,
146-
"condition duplicates match guard",
147-
"remove the condition",
148-
sugg.to_string(),
149-
applicability,
150-
);
151-
}
117+
// the arm body doesn't have its own curlies,
118+
// so we need to retain the ones around `then`
119+
then.span
120+
};
121+
122+
let sugg = snippet_with_applicability(cx, sugg_span, "..", &mut applicability);
123+
124+
span_lint_and_sugg(
125+
cx,
126+
DUPLICATE_MATCH_GUARDS,
127+
arm_body_expr.span,
128+
"condition duplicates match guard",
129+
"remove the condition",
130+
sugg.to_string(),
131+
applicability,
132+
);
152133
}
153134
}
154135
}

0 commit comments

Comments
 (0)