about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZack M. Davis <code@zackmdavis.net>2018-09-22 13:51:29 -0700
committerZack M. Davis <code@zackmdavis.net>2018-09-22 13:51:29 -0700
commit7cbe0605fa6eacb13aa8d256b999c8424144a4e1 (patch)
treec1dfd71e3c8a01ebce65f016b336e1e58d7c64e5
parentaf50e3822c4ceda60445c4a2adbb3bfa480ebd39 (diff)
downloadrust-7cbe0605fa6eacb13aa8d256b999c8424144a4e1.tar.gz
rust-7cbe0605fa6eacb13aa8d256b999c8424144a4e1.zip
in which we include attributes in unused `extern crate` suggestion spans
Resolves #54400.
-rw-r--r--src/librustc_typeck/check_unused.rs8
-rw-r--r--src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed24
-rw-r--r--src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs25
-rw-r--r--src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr18
4 files changed, 74 insertions, 1 deletions
diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs
index 5967bd1ba3e..3dbd5f2017e 100644
--- a/src/librustc_typeck/check_unused.rs
+++ b/src/librustc_typeck/check_unused.rs
@@ -140,9 +140,15 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
                 let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
                 let id = tcx.hir.hir_to_node_id(hir_id);
                 let msg = "unused extern crate";
+
+                // Removal suggestion span needs to include attributes (Issue #54400)
+                let span_with_attrs = tcx.get_attrs(extern_crate.def_id).iter()
+                    .map(|attr| attr.span)
+                    .fold(span, |acc, attr_span| acc.to(attr_span));
+
                 tcx.struct_span_lint_node(lint, id, span, msg)
                     .span_suggestion_short_with_applicability(
-                        span,
+                        span_with_attrs,
                         "remove it",
                         String::new(),
                         Applicability::MachineApplicable)
diff --git a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed
new file mode 100644
index 00000000000..20ebe6bb543
--- /dev/null
+++ b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed
@@ -0,0 +1,24 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:edition-lint-paths.rs
+// run-rustfix
+// compile-flags:--extern edition_lint_paths --cfg blandiloquence
+// edition:2018
+
+#![deny(rust_2018_idioms)]
+#![allow(dead_code)]
+
+// The suggestion span should include the attribute.
+
+
+//~^ ERROR unused extern crate
+
+fn main() {}
diff --git a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs
new file mode 100644
index 00000000000..11febe5e87d
--- /dev/null
+++ b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs
@@ -0,0 +1,25 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:edition-lint-paths.rs
+// run-rustfix
+// compile-flags:--extern edition_lint_paths --cfg blandiloquence
+// edition:2018
+
+#![deny(rust_2018_idioms)]
+#![allow(dead_code)]
+
+// The suggestion span should include the attribute.
+
+#[cfg(blandiloquence)] //~ HELP remove it
+extern crate edition_lint_paths;
+//~^ ERROR unused extern crate
+
+fn main() {}
diff --git a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr
new file mode 100644
index 00000000000..cb945ba1c78
--- /dev/null
+++ b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr
@@ -0,0 +1,18 @@
+error: unused extern crate
+  --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:22:1
+   |
+LL | / #[cfg(blandiloquence)] //~ HELP remove it
+LL | | extern crate edition_lint_paths;
+   | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
+   | |________________________________|
+   |                                  help: remove it
+   |
+note: lint level defined here
+  --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:16:9
+   |
+LL | #![deny(rust_2018_idioms)]
+   |         ^^^^^^^^^^^^^^^^
+   = note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]
+
+error: aborting due to previous error
+