Skip to content

Commit 630833e

Browse files
committed
dedent better?
1 parent ffcd069 commit 630833e

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

clippy_lints/src/duplicate_match_guards.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet_with_applicability};
2+
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet_with_applicability, trim_span};
33
use clippy_utils::{eq_expr_value, span_contains_comment};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Arm, ExprKind};
@@ -119,27 +119,31 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
119119
then.span
120120
};
121121

122+
let sugg_span = trim_span(sm, sugg_span);
123+
122124
let sugg = snippet_with_applicability(cx, sugg_span, "..", &mut applicability);
123125

124-
// we want to bring `then_without_curlies` to the level of indentation that
125-
// `arm_body_expr` used to be at
126-
//
126+
// since we remove one level of curlies, we should be able to dedent `then` left one
127+
// level, so this:
128+
// ```
127129
// <pat> if <guard> => {
128130
// if <cond> {
129131
// then
130132
// without
131133
// curlies
132134
// }
133135
// }
134-
//
136+
// ```
137+
// becomes this:
138+
// ```
135139
// <pat> if <guard> => {
136140
// then
137141
// without
138142
// curlies
139143
// }
140-
//
141-
let indent = indent_of(cx, arm_body_expr.span);
142-
let sugg = reindent_multiline(&sugg, false, indent);
144+
// ```
145+
let indent = indent_of(cx, sugg_span);
146+
let sugg = reindent_multiline(&sugg, true, indent.map(|i| i.saturating_sub(4)));
143147

144148
span_lint_and_sugg(
145149
cx,

tests/ui/duplicate_match_guards.fixed

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,39 @@ fn main() {
88

99
match 0u32 {
1010
0 if true => {
11-
12-
//~^ duplicate_match_guards
13-
return;
14-
11+
//~^ duplicate_match_guards
12+
return;
1513
},
1614
0 if a > b => {
17-
18-
//~^ duplicate_match_guards
19-
return;
20-
15+
//~^ duplicate_match_guards
16+
return;
2117
},
2218
// not _identical_, but the meaning is the same
2319
0 if a > b => {
24-
25-
//~^ duplicate_match_guards
26-
return;
27-
20+
//~^ duplicate_match_guards
21+
return;
2822
},
2923
// a bit more complicated
3024
0 if a > 0 && b > 0 => {
31-
32-
//~^ duplicate_match_guards
33-
return;
34-
25+
//~^ duplicate_match_guards
26+
return;
3527
},
3628
// comments inside the inner block are preserved
3729
// (for comments _outside_ the inner block, see below)
3830
#[rustfmt::skip]
3931
0 if a > b => {
4032
//~ duplicate_match_guards
41-
/*before*/
42-
return;
43-
/* after
44-
*/
33+
/*before*/
34+
return;
35+
/* after
36+
*/
4537
},
4638
// no curlies around arm body
4739
#[rustfmt::skip] // would add the outer curlies
4840
0 if true => {
49-
//~^ duplicate_match_guards
50-
return;
51-
},
41+
//~^ duplicate_match_guards
42+
return;
43+
},
5244

5345
// no warnings
5446
0 if true => {

tests/ui/duplicate_match_guards.stderr

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ LL | | }
1111
= help: to override `-D warnings` add `#[allow(clippy::duplicate_match_guards)]`
1212
help: remove the condition
1313
|
14-
LL ~
15-
LL +
16-
LL + return;
17-
LL +
14+
LL ~
15+
LL + return;
1816
|
1917

2018
error: condition duplicates match guard
@@ -28,10 +26,8 @@ LL | | }
2826
|
2927
help: remove the condition
3028
|
31-
LL ~
32-
LL +
33-
LL + return;
34-
LL +
29+
LL ~
30+
LL + return;
3531
|
3632

3733
error: condition duplicates match guard
@@ -45,10 +41,8 @@ LL | | }
4541
|
4642
help: remove the condition
4743
|
48-
LL ~
49-
LL +
50-
LL + return;
51-
LL +
44+
LL ~
45+
LL + return;
5246
|
5347

5448
error: condition duplicates match guard
@@ -62,10 +56,8 @@ LL | | }
6256
|
6357
help: remove the condition
6458
|
65-
LL ~
66-
LL +
67-
LL + return;
68-
LL +
59+
LL ~
60+
LL + return;
6961
|
7062

7163
error: condition duplicates match guard
@@ -81,10 +73,10 @@ LL | | */}
8173
help: remove the condition
8274
|
8375
LL ~
84-
LL + /*before*/
85-
LL + return;
86-
LL + /* after
87-
LL + */
76+
LL + /*before*/
77+
LL + return;
78+
LL + /* after
79+
LL + */
8880
|
8981

9082
error: condition duplicates match guard
@@ -101,8 +93,8 @@ help: remove the condition
10193
|
10294
LL ~ 0 if true => {
10395
LL +
104-
LL + return;
105-
LL ~ },
96+
LL + return;
97+
LL ~ },
10698
|
10799

108100
error: aborting due to 6 previous errors

0 commit comments

Comments
 (0)