about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-09-09 00:28:20 +0200
committerGitHub <noreply@github.com>2023-09-09 00:28:20 +0200
commitb33ac52106c78cf7ad499dbe8ba7d0f54dafdadf (patch)
tree903308fcb3838ae28ecebd04829e185291d3e856
parentaa78b4c36806c1e020607989c6ae9a48cff1b9b1 (diff)
parent03bee1f53dbf28c582d52ced9b165812514303f9 (diff)
downloadrust-b33ac52106c78cf7ad499dbe8ba7d0f54dafdadf.tar.gz
rust-b33ac52106c78cf7ad499dbe8ba7d0f54dafdadf.zip
Rollup merge of #115630 - compiler-errors:dont-suggest-use-btw-use-and-attr, r=wesleywiser
Dont suggest use between `use` and cfg attr

Fixes #115618
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs8
-rw-r--r--tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed16
-rw-r--r--tests/ui/resolve/suggest-import-without-clobbering-attrs.rs15
-rw-r--r--tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr14
4 files changed, 52 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index d99fc07a7cd..e9edfb53b44 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -2753,7 +2753,13 @@ fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
     for item in items {
         if let ItemKind::Use(..) = item.kind {
             if is_span_suitable_for_use_injection(item.span) {
-                return Some(item.span.shrink_to_lo());
+                let mut lo = item.span.lo();
+                for attr in &item.attrs {
+                    if attr.span.eq_ctxt(item.span) {
+                        lo = std::cmp::min(lo, attr.span.lo());
+                    }
+                }
+                return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent()));
             }
         }
     }
diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed
new file mode 100644
index 00000000000..fc68884fe9c
--- /dev/null
+++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed
@@ -0,0 +1,16 @@
+// run-rustfix
+// compile-flags: --cfg=whatever -Aunused
+
+use y::z;
+#[cfg(whatever)]
+use y::Whatever;
+
+mod y {
+    pub(crate) fn z() {}
+    pub(crate) struct Whatever;
+}
+
+fn main() {
+    z();
+    //~^ ERROR cannot find function `z` in this scope
+}
diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs b/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs
new file mode 100644
index 00000000000..38a1095703b
--- /dev/null
+++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs
@@ -0,0 +1,15 @@
+// run-rustfix
+// compile-flags: --cfg=whatever -Aunused
+
+#[cfg(whatever)]
+use y::Whatever;
+
+mod y {
+    pub(crate) fn z() {}
+    pub(crate) struct Whatever;
+}
+
+fn main() {
+    z();
+    //~^ ERROR cannot find function `z` in this scope
+}
diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr b/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr
new file mode 100644
index 00000000000..d3574851d5c
--- /dev/null
+++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr
@@ -0,0 +1,14 @@
+error[E0425]: cannot find function `z` in this scope
+  --> $DIR/suggest-import-without-clobbering-attrs.rs:13:5
+   |
+LL |     z();
+   |     ^ not found in this scope
+   |
+help: consider importing this function
+   |
+LL + use y::z;
+   |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0425`.