about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/lib.rs
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-02-13 03:21:25 +0000
committerJubilee Young <workingjubilee@gmail.com>2025-02-14 00:44:10 -0800
commit6d71251cf9e40326461f90f8ff9a7024706aea87 (patch)
tree9632ff91f2996a68f9d9c4ebdb3f3ebf8cfa64f4 /compiler/rustc_errors/src/lib.rs
parentf6406dfd4efceb6f713e503aecda587304135ed9 (diff)
downloadrust-6d71251cf9e40326461f90f8ff9a7024706aea87.tar.gz
rust-6d71251cf9e40326461f90f8ff9a7024706aea87.zip
Trim suggestion parts to the subset that is purely additive
Diffstat (limited to 'compiler/rustc_errors/src/lib.rs')
-rw-r--r--compiler/rustc_errors/src/lib.rs18
1 files changed, 18 insertions, 0 deletions
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 {