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
Closed

Diagnostic information does not include item attributes #54400

jrobsonchase opened this issue Sep 20, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2018 Area: The 2018 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@jrobsonchase
Copy link

Original rustfix issue: rust-lang/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.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 21, 2018
@zackmdavis
Copy link
Member

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.

zackmdavis added a commit to zackmdavis/rust that referenced this issue Sep 22, 2018
kennytm added a commit to kennytm/rust that referenced this issue Oct 1, 2018
…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
@fmease fmease added A-edition-2018 Area: The 2018 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-edition-2018-lints labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2018 Area: The 2018 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

5 participants