Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostic information does not include item attributes #54400

Closed
jrobsonchase opened this Issue Sep 20, 2018 · 1 comment

Comments

Projects
None yet
4 participants
@jrobsonchase
Copy link

jrobsonchase commented Sep 20, 2018

Original rustfix issue: rust-lang-nursery/rustfix#147

When rustc outputs warnings like "you don't need this extern crate line anymore, delete it," it doesn't include accompanying attributes. This leads rustfix to apply its suggestion to delete the extern crate line, but leave the attribute, which then gets applied to the next item.

For example:

#[cfg(unix)]
extern crate nix;

mod foo;

becomes

#[cfg(unix)]

mod foo;

which applies the #[cfg(unix)] attribute to mod foo; which is definitely not what was desired.

@zackmdavis

This comment has been minimized.

Copy link
Member

zackmdavis commented Sep 21, 2018

Here's where we issue the diagnostic:

if extern_crate.warn_if_unused {
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
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";
tcx.struct_span_lint_node(lint, id, span, msg)
.span_suggestion_short_with_applicability(
span,
"remove it",
String::new(),
Applicability::MachineApplicable)
.emit();
continue;
}
}

Assuming the cfg attribute is actually preserved by earlier passes, then correcting for this case should be easy (look for it in tcx.get_attrs and extend the span correspondingly), but I don't remember offhand whether that's true and don't have time to investigate right now. If it isn't, then this is similar-in-spirit to #45216 in that we "just" want the span of an attribute, but would need to do some nontrivial rearchitecting work to make it available.

kennytm added a commit to kennytm/rust that referenced this issue Oct 1, 2018

Rollup merge of rust-lang#54488 - zackmdavis:and_the_case_of_the_unus…
…ed_crate, r=estebank

in which we include attributes in unused `extern crate` suggestion spans

![unused_extern](https://user-images.githubusercontent.com/1076988/45921698-50243e80-be6f-11e8-930a-7b2a33b4935c.png)

Resolves rust-lang#54400.

r? @estebank

@bors bors closed this in #54488 Oct 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.