Skip to content

Diagnostic information does not include item attributes #54400

@jrobsonchase

Description

@jrobsonchase

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.

Activity

zackmdavis

zackmdavis commented on Sep 21, 2018

@zackmdavis
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.

added a commit that references this issue on Sep 22, 2018
7cbe060
added a commit that references this issue on Oct 1, 2018
b188212
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
and removed on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2018Area: The 2018 editionA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alexcrichton@zackmdavis@jrobsonchase@estebank@fmease

        Issue actions

          Diagnostic information does not include item attributes · Issue #54400 · rust-lang/rust