about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-05 17:32:26 +0000
committerbors <bors@rust-lang.org>2023-02-05 17:32:26 +0000
commita67649675014546ce454d65bc8fe3ebd18e6a319 (patch)
tree0cdc1ec062b132a4e718de8b3fe12e55a46cff76
parent319b88c463fe6f51bb6badbbd3bb97252a60f3a5 (diff)
parented58c01959bc69b42943292d7d5a8e86a821747e (diff)
downloadrust-a67649675014546ce454d65bc8fe3ebd18e6a319.tar.gz
rust-a67649675014546ce454d65bc8fe3ebd18e6a319.zip
Auto merge of #107663 - matthiaskrgr:107423-point-at-EOF-code, r=compiler-errors
don't point at nonexisting code beyond EOF when warning about delims

Previously we would show this:
```
warning: unnecessary braces around block return value
 --> /tmp/bad.rs:1:8
  |
1 | fn a(){{{
  |        ^  ^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
1 - fn a(){{{
1 + fn a(){{
  |
```

which is now hidden in this case.
We would create a span spanning between the pair of redundant {}s but there is only EOF instead of the `}` so we would previously point at nothing. This would cause the debug assertion ice to trigger. I would have loved to just only point at the second delim and say "you can remove that" but I'm not sure how to do that without refactoring the entire diagnostic which seems tricky. :( But given that this does not seem to regress any other tests we have, I think this edge-casey enough be acceptable.

Fixes https://github.com/rust-lang/rust/issues/107423

r? `@compiler-errors`
-rw-r--r--compiler/rustc_lint/src/unused.rs4
-rw-r--r--tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.rs7
-rw-r--r--tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.stderr32
3 files changed, 43 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index d829ca43328..ecaf1c44354 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -663,6 +663,10 @@ trait UnusedDelimLint {
         keep_space: (bool, bool),
     ) {
         let primary_span = if let Some((lo, hi)) = spans {
+            if hi.is_empty() {
+                // do not point at delims that do not exist
+                return;
+            }
             MultiSpan::from(vec![lo, hi])
         } else {
             MultiSpan::from(value_span)
diff --git a/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.rs b/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.rs
new file mode 100644
index 00000000000..4003ee37ca1
--- /dev/null
+++ b/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.rs
@@ -0,0 +1,7 @@
+// check that we don't generate a span that points beyond EOF
+
+// error-pattern: unclosed delimiter
+// error-pattern: unclosed delimiter
+// error-pattern: unclosed delimiter
+
+fn a(){{{
diff --git a/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.stderr b/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.stderr
new file mode 100644
index 00000000000..0479035171e
--- /dev/null
+++ b/tests/ui/malformed/issue-107423-unused-delim-only-one-no-pair.stderr
@@ -0,0 +1,32 @@
+error: this file contains an unclosed delimiter
+  --> $DIR/issue-107423-unused-delim-only-one-no-pair.rs:7:11
+   |
+LL | fn a(){{{
+   |       --- ^
+   |       |||
+   |       ||unclosed delimiter
+   |       |unclosed delimiter
+   |       unclosed delimiter
+
+error: this file contains an unclosed delimiter
+  --> $DIR/issue-107423-unused-delim-only-one-no-pair.rs:7:11
+   |
+LL | fn a(){{{
+   |       --- ^
+   |       |||
+   |       ||unclosed delimiter
+   |       |unclosed delimiter
+   |       unclosed delimiter
+
+error: this file contains an unclosed delimiter
+  --> $DIR/issue-107423-unused-delim-only-one-no-pair.rs:7:11
+   |
+LL | fn a(){{{
+   |       --- ^
+   |       |||
+   |       ||unclosed delimiter
+   |       |unclosed delimiter
+   |       unclosed delimiter
+
+error: aborting due to 3 previous errors
+