Skip to content

Commit 0d15bf2

Browse files
authored
Rollup merge of rust-lang#146121 - Muscraft:filter-suggestion-parts, r=petrochenkov
fix: Filter suggestion parts that match existing code While testing my changes to make `rustc` use `annotate-snippets`, I encountered a new `clippy` test failure stemming from [two](https://github.com/rust-lang/rust/pull/145273/files#diff-6e8403e31463539666afbc00479cb416dc767a518f562b6e2960630953ee7da2R275-R278) [suggestion](https://github.com/rust-lang/rust/pull/145273/files#diff-6e8403e31463539666afbc00479cb416dc767a518f562b6e2960630953ee7da2R289-R292) output changes in rust-lang#145273. The new output in these two cases feels like a regression as it is not as clear as the old output, and adds unnecessary information. Before rust-lang#145273 (`Diff` style) ![before](https://github.com/user-attachments/assets/36f33635-cbce-45f1-823d-0cbe6f0cfe46) After rust-lang#145273 ("multi-line" style) ![after](https://github.com/user-attachments/assets/d4cb00b8-5a42-436e-9329-db84347138f0) The reason for the change was that a new suggestion part (which matches existing code) was added on a different line than the existing parts, causing the suggestion style to change from `Diff` to "multi-line". Since this new part matches existing code, no code changes show up in the output for it, but it still makes the suggestion style "multi-line" when it doesn't need to be. To get the old output back, I made it so that suggestion parts that perfectly match existing code get filtered out.
2 parents 7e2c6ed + f696cd8 commit 0d15bf2

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ impl CodeSuggestion {
378378
})
379379
.cloned()
380380
.filter_map(|mut substitution| {
381+
// Account for cases where we are suggesting the same code that's already
382+
// there. This shouldn't happen often, but in some cases for multipart
383+
// suggestions it's much easier to handle it here than in the origin.
384+
substitution.parts.retain(|p| is_different(sm, &p.snippet, p.span));
385+
381386
// Assumption: all spans are in the same file, and all spans
382387
// are disjoint. Sort in ascending order.
383388
substitution.parts.sort_by_key(|part| part.span.lo());
@@ -470,16 +475,12 @@ impl CodeSuggestion {
470475
_ => 1,
471476
})
472477
.sum();
473-
if !is_different(sm, &part.snippet, part.span) {
474-
// Account for cases where we are suggesting the same code that's already
475-
// there. This shouldn't happen often, but in some cases for multipart
476-
// suggestions it's much easier to handle it here than in the origin.
477-
} else {
478-
line_highlight.push(SubstitutionHighlight {
479-
start: (cur_lo.col.0 as isize + acc) as usize,
480-
end: (cur_lo.col.0 as isize + acc + len) as usize,
481-
});
482-
}
478+
479+
line_highlight.push(SubstitutionHighlight {
480+
start: (cur_lo.col.0 as isize + acc) as usize,
481+
end: (cur_lo.col.0 as isize + acc + len) as usize,
482+
});
483+
483484
buf.push_str(&part.snippet);
484485
let cur_hi = sm.lookup_char_pos(part.span.hi());
485486
// Account for the difference between the width of the current code and the

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,8 @@ LL | assert_eq!(a!(), true);
272272
|
273273
help: replace it with `assert!(..)`
274274
|
275-
LL | true
276-
...
277-
LL |
278-
LL ~ assert!(a!());
275+
LL - assert_eq!(a!(), true);
276+
LL + assert!(a!());
279277
|
280278

281279
error: used `assert_eq!` with a literal bool
@@ -286,10 +284,8 @@ LL | assert_eq!(true, b!());
286284
|
287285
help: replace it with `assert!(..)`
288286
|
289-
LL | true
290-
...
291-
LL |
292-
LL ~ assert!(b!());
287+
LL - assert_eq!(true, b!());
288+
LL + assert!(b!());
293289
|
294290

295291
error: used `debug_assert_eq!` with a literal bool

0 commit comments

Comments
 (0)