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 02:54:07 +0000
committerJubilee Young <workingjubilee@gmail.com>2025-02-14 00:27:13 -0800
commitb480a9214a7037813d29bc04d9e9dbe92ce10cf3 (patch)
tree748aa792f61b46d7c679f157c8c8a3ad9df380f6 /compiler/rustc_errors/src/lib.rs
parent6dfeab5c9e8a17a6636c1479037baabc4b1e9562 (diff)
downloadrust-b480a9214a7037813d29bc04d9e9dbe92ce10cf3.tar.gz
rust-b480a9214a7037813d29bc04d9e9dbe92ce10cf3.zip
Use underline suggestions for purely 'additive' replacements
Diffstat (limited to 'compiler/rustc_errors/src/lib.rs')
-rw-r--r--compiler/rustc_errors/src/lib.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 9af17db9a6e..4a186565a7b 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -230,6 +230,17 @@ impl SubstitutionPart {
         !self.snippet.is_empty() && self.replaces_meaningful_content(sm)
     }
 
+    /// Whether this is a replacement that overwrites source with a snippet
+    /// in a way that isn't a superset of the original string. For example,
+    /// replacing "abc" with "abcde" is not destructive, but replacing it
+    /// it with "abx" is, since the "c" character is lost.
+    pub fn is_destructive_replacement(&self, sm: &SourceMap) -> bool {
+        self.is_replacement(sm)
+            && !sm
+                .span_to_snippet(self.span)
+                .is_ok_and(|snippet| self.snippet.trim_start().starts_with(snippet.trim_start()))
+    }
+
     fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
         sm.span_to_snippet(self.span)
             .map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())