fix: Filter suggestion parts that match existing code

This commit is contained in:
Scott Schafer
2025-08-27 20:33:50 -06:00
parent f196f50d66
commit b307a1146b
2 changed files with 15 additions and 18 deletions

View File

@@ -388,6 +388,11 @@ impl CodeSuggestion {
"all spans must be disjoint", "all spans must be disjoint",
); );
// Account for cases where we are suggesting the same code that's already
// there. This shouldn't happen often, but in some cases for multipart
// suggestions it's much easier to handle it here than in the origin.
substitution.parts.retain(|p| is_different(sm, &p.snippet, p.span));
// Find the bounding span. // Find the bounding span.
let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?; let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?;
let hi = substitution.parts.iter().map(|part| part.span.hi()).max()?; let hi = substitution.parts.iter().map(|part| part.span.hi()).max()?;
@@ -476,16 +481,12 @@ impl CodeSuggestion {
_ => 1, _ => 1,
}) })
.sum(); .sum();
if !is_different(sm, &part.snippet, part.span) {
// Account for cases where we are suggesting the same code that's already
// there. This shouldn't happen often, but in some cases for multipart
// suggestions it's much easier to handle it here than in the origin.
} else {
line_highlight.push(SubstitutionHighlight { line_highlight.push(SubstitutionHighlight {
start: (cur_lo.col.0 as isize + acc) as usize, start: (cur_lo.col.0 as isize + acc) as usize,
end: (cur_lo.col.0 as isize + acc + len) as usize, end: (cur_lo.col.0 as isize + acc + len) as usize,
}); });
}
buf.push_str(&part.snippet); buf.push_str(&part.snippet);
let cur_hi = sm.lookup_char_pos(part.span.hi()); let cur_hi = sm.lookup_char_pos(part.span.hi());
// Account for the difference between the width of the current code and the // Account for the difference between the width of the current code and the

View File

@@ -272,10 +272,8 @@ LL | assert_eq!(a!(), true);
| |
help: replace it with `assert!(..)` help: replace it with `assert!(..)`
| |
LL | true LL - assert_eq!(a!(), true);
... LL + assert!(a!());
LL |
LL ~ assert!(a!());
| |
error: used `assert_eq!` with a literal bool error: used `assert_eq!` with a literal bool
@@ -286,10 +284,8 @@ LL | assert_eq!(true, b!());
| |
help: replace it with `assert!(..)` help: replace it with `assert!(..)`
| |
LL | true LL - assert_eq!(true, b!());
... LL + assert!(b!());
LL |
LL ~ assert!(b!());
| |
error: used `debug_assert_eq!` with a literal bool error: used `debug_assert_eq!` with a literal bool