Skip to content

Commit 4042468

Browse files
committed
Adding only suggestion when span not empty
1 parent 17e3b73 commit 4042468

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,17 @@ impl<'a> Parser<'a> {
13741374
kind: IncDecRecovery,
13751375
(pre_span, post_span): (Span, Span),
13761376
) -> MultiSugg {
1377+
let mut patches = Vec::new();
1378+
1379+
if !pre_span.is_empty() {
1380+
patches.push((pre_span, String::new()));
1381+
}
1382+
1383+
patches.push((post_span, format!(" {}= 1", kind.op.chr())));
1384+
13771385
MultiSugg {
13781386
msg: format!("use `{}= 1` instead", kind.op.chr()),
1379-
patches: vec![(pre_span, String::new()), (post_span, format!(" {}= 1", kind.op.chr()))],
1387+
patches,
13801388
applicability: Applicability::MachineApplicable,
13811389
}
13821390
}

src/test/ui/parser/increment-notfixed.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ help: use `+= 1` instead
88
|
99
LL | { let tmp = i; i += 1; tmp };
1010
| +++++++++++ ~~~~~~~~~~~~~~~
11+
LL | i += 1;
12+
| ~~~~
1113

1214
error: Rust has no postfix increment operator
1315
--> $DIR/increment-notfixed.rs:17:12
@@ -21,6 +23,8 @@ help: use `+= 1` instead
2123
|
2224
LL | while { let tmp = i; i += 1; tmp } < 5 {
2325
| +++++++++++ ~~~~~~~~~~~~~~~
26+
LL | while i += 1 < 5 {
27+
| ~~~~
2428

2529
error: Rust has no postfix increment operator
2630
--> $DIR/increment-notfixed.rs:25:8
@@ -32,6 +36,8 @@ help: use `+= 1` instead
3236
|
3337
LL | { let tmp_ = tmp; tmp += 1; tmp_ };
3438
| ++++++++++++ ~~~~~~~~~~~~~~~~~~
39+
LL | tmp += 1;
40+
| ~~~~
3541

3642
error: Rust has no postfix increment operator
3743
--> $DIR/increment-notfixed.rs:31:14
@@ -45,6 +51,8 @@ help: use `+= 1` instead
4551
|
4652
LL | while { let tmp_ = tmp; tmp += 1; tmp_ } < 5 {
4753
| ++++++++++++ ~~~~~~~~~~~~~~~~~~
54+
LL | while tmp += 1 < 5 {
55+
| ~~~~
4856

4957
error: Rust has no postfix increment operator
5058
--> $DIR/increment-notfixed.rs:39:16
@@ -56,6 +64,8 @@ help: use `+= 1` instead
5664
|
5765
LL | { let tmp = foo.bar.qux; foo.bar.qux += 1; tmp };
5866
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
67+
LL | foo.bar.qux += 1;
68+
| ~~~~
5969

6070
error: Rust has no postfix increment operator
6171
--> $DIR/increment-notfixed.rs:49:10
@@ -67,6 +77,8 @@ help: use `+= 1` instead
6777
|
6878
LL | { let tmp = s.tmp; s.tmp += 1; tmp };
6979
| +++++++++++ ~~~~~~~~~~~~~~~~~~~
80+
LL | s.tmp += 1;
81+
| ~~~~
7082

7183
error: Rust has no prefix increment operator
7284
--> $DIR/increment-notfixed.rs:56:5

0 commit comments

Comments
 (0)