Skip to content

Commit 20bf5b1

Browse files
committed
Properly handle removal suggestion rendering
Do not leave a `+ ` line with only whitespace. In reality, the user will want to remove the entire line.
1 parent 7852986 commit 20bf5b1

10 files changed

+20
-24
lines changed

compiler/rustc_errors/src/emitter.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -2273,9 +2273,26 @@ impl HumanEmitter {
22732273
&normalize_whitespace(last_line),
22742274
Style::NoStyle,
22752275
);
2276-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2277-
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2278-
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
2276+
if !line_to_add.trim().is_empty() {
2277+
// Check if after the removal, the line is left with only whitespace. If so, we
2278+
// will not show an "addition" line, as removing the whole line is what the user
2279+
// would really want.
2280+
// For example, for the following:
2281+
// |
2282+
// 2 - .await
2283+
// 2 + (note the left over whitepsace)
2284+
// |
2285+
// We really want
2286+
// |
2287+
// 2 - .await
2288+
// |
2289+
// *row_num -= 1;
2290+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2291+
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2292+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
2293+
} else {
2294+
*row_num -= 1;
2295+
}
22792296
} else {
22802297
*row_num -= 2;
22812298
}

src/tools/clippy/tests/ui/dbg_macro/dbg_macro.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ LL | dbg!();
8686
help: remove the invocation before committing it to a version control system
8787
|
8888
LL - dbg!();
89-
LL +
9089
|
9190

9291
error: the `dbg!` macro is intended as a debugging tool
@@ -146,7 +145,6 @@ LL | expand_to_dbg!();
146145
help: remove the invocation before committing it to a version control system
147146
|
148147
LL - dbg!();
149-
LL +
150148
|
151149

152150
error: the `dbg!` macro is intended as a debugging tool

src/tools/clippy/tests/ui/dbg_macro/dbg_macro_unfixable.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | dbg!();
99
help: remove the invocation before committing it to a version control system
1010
|
1111
LL - dbg!();
12-
LL +
1312
|
1413

1514
error: the `dbg!` macro is intended as a debugging tool

src/tools/clippy/tests/ui/manual_split_once.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,10 @@ LL | let (l, r) = "a.b.c".split_once('.').unwrap();
160160
help: remove the `iter` usages
161161
|
162162
LL - let l = iter.next().unwrap();
163-
LL +
164163
|
165164
help: remove the `iter` usages
166165
|
167166
LL - let r = iter.next().unwrap();
168-
LL +
169167
|
170168

171169
error: manual implementation of `split_once`
@@ -185,12 +183,10 @@ LL | let (l, r) = "a.b.c".split_once('.')?;
185183
help: remove the `iter` usages
186184
|
187185
LL - let l = iter.next()?;
188-
LL +
189186
|
190187
help: remove the `iter` usages
191188
|
192189
LL - let r = iter.next()?;
193-
LL +
194190
|
195191

196192
error: manual implementation of `rsplit_once`
@@ -210,12 +206,10 @@ LL | let (l, r) = "a.b.c".rsplit_once('.').unwrap();
210206
help: remove the `iter` usages
211207
|
212208
LL - let r = iter.next().unwrap();
213-
LL +
214209
|
215210
help: remove the `iter` usages
216211
|
217212
LL - let l = iter.next().unwrap();
218-
LL +
219213
|
220214

221215
error: manual implementation of `rsplit_once`
@@ -235,12 +229,10 @@ LL | let (l, r) = "a.b.c".rsplit_once('.')?;
235229
help: remove the `iter` usages
236230
|
237231
LL - let r = iter.next()?;
238-
LL +
239232
|
240233
help: remove the `iter` usages
241234
|
242235
LL - let l = iter.next()?;
243-
LL +
244236
|
245237

246238
error: manual implementation of `split_once`
@@ -271,12 +263,10 @@ LL | let (a, b) = "a.b.c".split_once('.').unwrap();
271263
help: remove the `iter` usages
272264
|
273265
LL - let a = iter.next().unwrap();
274-
LL +
275266
|
276267
help: remove the `iter` usages
277268
|
278269
LL - let b = iter.next().unwrap();
279-
LL +
280270
|
281271

282272
error: aborting due to 19 previous errors

src/tools/clippy/tests/ui/needless_if.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | if (true) {}
99
help: you can remove it
1010
|
1111
LL - if (true) {}
12-
LL +
1312
|
1413

1514
error: this `if` branch is empty

src/tools/clippy/tests/ui/non_canonical_clone_impl.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ LL - fn clone_from(&mut self, source: &Self) {
2929
LL - source.clone();
3030
LL - *self = source.clone();
3131
LL - }
32-
LL +
3332
|
3433

3534
error: non-canonical implementation of `clone` on a `Copy` type
@@ -61,7 +60,6 @@ LL - fn clone_from(&mut self, source: &Self) {
6160
LL - source.clone();
6261
LL - *self = source.clone();
6362
LL - }
64-
LL +
6563
|
6664

6765
error: aborting due to 4 previous errors

src/tools/clippy/tests/ui/significant_drop_tightening.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ LL + let rslt0 = mutex.lock().unwrap().abs();
6464
help: remove separated single usage
6565
|
6666
LL - let rslt0 = lock.abs();
67-
LL +
6867
|
6968

7069
error: temporary with significant `Drop` can be early dropped
@@ -88,7 +87,6 @@ LL + mutex.lock().unwrap().clear();
8887
help: remove separated single usage
8988
|
9089
LL - lock.clear();
91-
LL +
9290
|
9391

9492
error: aborting due to 4 previous errors

src/tools/clippy/tests/ui/single_component_path_imports.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | use regex;
2020
help: remove it entirely
2121
|
2222
LL - use regex;
23-
LL +
2423
|
2524

2625
error: aborting due to 2 previous errors

src/tools/clippy/tests/ui/unused_unit.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ LL | ()
7272
help: remove the final `()`
7373
|
7474
LL - ()
75-
LL +
7675
|
7776

7877
error: unneeded unit return type

tests/ui/async-await/issue-101715.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | .await
1010
help: remove the `.await`
1111
|
1212
LL - .await
13-
LL +
1413
|
1514

1615
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)