Skip to content

Commit 7cbe060

Browse files
committed
in which we include attributes in unused extern crate suggestion spans
Resolves #54400.
1 parent af50e38 commit 7cbe060

4 files changed

+74
-1
lines changed

src/librustc_typeck/check_unused.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,15 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
140140
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
141141
let id = tcx.hir.hir_to_node_id(hir_id);
142142
let msg = "unused extern crate";
143+
144+
// Removal suggestion span needs to include attributes (Issue #54400)
145+
let span_with_attrs = tcx.get_attrs(extern_crate.def_id).iter()
146+
.map(|attr| attr.span)
147+
.fold(span, |acc, attr_span| acc.to(attr_span));
148+
143149
tcx.struct_span_lint_node(lint, id, span, msg)
144150
.span_suggestion_short_with_applicability(
145-
span,
151+
span_with_attrs,
146152
"remove it",
147153
String::new(),
148154
Applicability::MachineApplicable)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
// compile-flags:--extern edition_lint_paths --cfg blandiloquence
14+
// edition:2018
15+
16+
#![deny(rust_2018_idioms)]
17+
#![allow(dead_code)]
18+
19+
// The suggestion span should include the attribute.
20+
21+
22+
//~^ ERROR unused extern crate
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
// compile-flags:--extern edition_lint_paths --cfg blandiloquence
14+
// edition:2018
15+
16+
#![deny(rust_2018_idioms)]
17+
#![allow(dead_code)]
18+
19+
// The suggestion span should include the attribute.
20+
21+
#[cfg(blandiloquence)] //~ HELP remove it
22+
extern crate edition_lint_paths;
23+
//~^ ERROR unused extern crate
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unused extern crate
2+
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:22:1
3+
|
4+
LL | / #[cfg(blandiloquence)] //~ HELP remove it
5+
LL | | extern crate edition_lint_paths;
6+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
7+
| |________________________________|
8+
| help: remove it
9+
|
10+
note: lint level defined here
11+
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:16:9
12+
|
13+
LL | #![deny(rust_2018_idioms)]
14+
| ^^^^^^^^^^^^^^^^
15+
= note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)