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

edition idioms: Incorrect span in extern crate removal #56328

Open
Tracked by #54910
alexcrichton opened this issue Nov 28, 2018 · 3 comments
Open
Tracked by #54910

edition idioms: Incorrect span in extern crate removal #56328

alexcrichton opened this issue Nov 28, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2018-lints Area: lints supporting the 2018 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

First reported upstream this code:

#![warn(rust_2018_idioms)]

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

use itertools::Itertools;

fn main() {
    println!("{:?}", (0..1).collect_vec());
}

when compiled yields:

warning: unused extern crate
 --> src/main.rs:4:1
  |
4 | extern crate itertools;
  | ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![warn(rust_2018_idioms)]
  |         ^^^^^^^^^^^^^^^^
  = note: #[warn(unused_extern_crates)] implied by #[warn(rust_2018_idioms)]

but the suggestion is incorrect! Both the extern crate and the attribute on the item should be removed.

@ExpHP
Copy link
Contributor

ExpHP commented Nov 28, 2018

Both the extern crate and the attribute on the item should be removed.

That works for the example above, but not if a #[test] uses macros. My original report would have required something like

#[cfg(test)] #[macro_use] extern crate itertools;

to fix it.

@estebank
Copy link
Contributor

We could suggest both removal (with the correct span) or alternatively leaving it behind a cfg flag, using test as an example.

I think full removal suggestions that span multiple lines don't work as well, we should show probably one or two lines before and one or two lines after the removed code to make it clear of where the removal took place.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 29, 2018
@alexcrichton alexcrichton added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. label Jan 7, 2019
@ehuss
Copy link
Contributor

ehuss commented Jan 8, 2019

See also #57420 for some more examples where cfg attributes cause problems with unused_extern_crates. In those cases, the lint probably shouldn't fire at all.

@crlf0710 crlf0710 added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 11, 2020
@estebank estebank added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Mar 15, 2023
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-lints Area: lints supporting the 2018 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants