From 6d71251cf9e40326461f90f8ff9a7024706aea87 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 13 Feb 2025 03:21:25 +0000 Subject: Trim suggestion parts to the subset that is purely additive --- compiler/rustc_errors/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'compiler/rustc_errors/src/lib.rs') diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 855b9734cdc..8ff5dc12596 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -246,6 +246,24 @@ impl SubstitutionPart { sm.span_to_snippet(self.span) .map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty()) } + + /// Try to turn a replacement into an addition when the span that is being + /// overwritten matches either the prefix or suffix of the replacement. + fn trim_trivial_replacements(&mut self, sm: &SourceMap) { + if self.snippet.is_empty() { + return; + } + let Ok(snippet) = sm.span_to_snippet(self.span) else { + return; + }; + if self.snippet.starts_with(&snippet) { + self.span = self.span.shrink_to_hi(); + self.snippet = self.snippet[snippet.len()..].to_string(); + } else if self.snippet.ends_with(&snippet) { + self.span = self.span.shrink_to_lo(); + self.snippet = self.snippet[..self.snippet.len() - snippet.len()].to_string(); + } + } } impl CodeSuggestion { -- cgit 1.4.1-3-g733a5