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

unused_extern_crates: does not handle cfg attributes #57420

Closed
ehuss opened this issue Jan 7, 2019 · 2 comments
Closed

unused_extern_crates: does not handle cfg attributes #57420

ehuss opened this issue Jan 7, 2019 · 2 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`

Comments

@ehuss
Copy link
Contributor

ehuss commented Jan 7, 2019

If an extern crate has a cfg_attr that is not enabled, then it incorrectly suggests removing the extern crate.

#![warn(unused_extern_crates)]

#[cfg_attr(test, macro_use)]
extern crate serde_json;

#[cfg(test)]
pub fn f() {
    json!({})
}

Run cargo fix --lib on the above, and it will remove the extern crate, leaving the attribute floating in limbo. Then, cargo check --lib --profile=test will demonstrate that the code is now broken.

The unused_extern_crates lint tries to avoid firing if there are attributes (see check_unused) but I'm guessing the cfg_attr strips the attribute (or doesn't add it in the first place), so the lint doesn't know there are any attributes.

A similar issue, the unused_extern_crates suggests use when it doesn't work. Given the following:

#![warn(unused_extern_crates)]

#[cfg_attr(test, macro_use)]
extern crate serde_derive as sd;

#[derive(sd::Serialize)]
pub struct Z;

#[cfg(test)]
mod m {
    #[derive(Deserialize)]
    struct S;
}

cargo fix --edition-idioms --lib will convert the extern crate to a use statement, keeping the macro_use attribute which won't work. cargo check --profile=test --lib will the fail to compile.

@alexcrichton alexcrichton added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Jan 7, 2019
@ExpHP
Copy link
Contributor

ExpHP commented Jan 8, 2019

I think this is the same as #56328

@ehuss
Copy link
Contributor Author

ehuss commented Jan 8, 2019

Indeed, closing in favor of that.

@ehuss ehuss closed this as completed Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`
Projects
None yet
Development

No branches or pull requests

3 participants