-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Lint for removing extern crate
has false positive when crate is used
#50672
Comments
cc @Manishearth |
I and @nikomatsakis discussed this a bunch when implementing this and decided not to fix this problem. An earlier commit of mine there had an attempt which would try and ask you to directly import crates when used as |
The fix is in Manishearth@c904d98 It's not perfect for a bunch of reasons (also might not do precisely what I said in the commit above, but close) |
While I can't say for sure I'd be pretty certain that this pattern shows up in the wild (especially |
Okay, so paging this in a bit more, while Manishearth@c904d98 is a pretty decent lint, we can't write suggestions for it. The problem is that use trees get split out in HIR, which means that the suggestions here can't work to well in the presence of these. It's hard to even detect that there's a use tree involved (in some cases) Also, doing the span-splicing to emit the correct suggestion is tricky as well, we don't store spans for path segments. |
So basically this is doable as a rustfixable lint, but it will be buggy and/or very imperfect. |
I have a few questions. My memory is that, when an However, if the This approach breaks if the code uses |
One answer to my question would be |
I am reminded that I initially wanted to do a kind of global pass to see where and how the |
OK, so, it seems like the lint that @Manishearth pointed us at (Manishearth@c904d98) does the following:
As @Manishearth said, writing suggestions for this case is tricky, at least in its full generality. But we could certainly do something like the following:
That means we'd suppress the "not actionable" error but issue lints that guide you towards making the extern crate into something we can remove. However, there is still the downside that the paths must be manually changed. I suspect we can overcome that in practice a lot of the time — I mean there are tricky cases, but we can look for the plain vanilla case and just issue the suggestion in that case (for example, by checking that the |
I discussed with @nikomatsakis a bit on discord and agree that perhaps the best thing to do here is to avoid linting anything at all, but rather wait till the lint is higher accuracy to turn it on by default. |
Pending fix in #51015 |
…diom, r=alexcrichton merge unused-extern-crate and unnecessary-extern-crate lints Extend the `unused_extern_crates` lint to offer a suggestion to remove the extern crate and remove the `unnecessary_extern_crate` lint. Still a few minor issues to fix: - [x] this *does* now leave a blank line... (defer to #51176) - idea: extend the span to be replaced by 1 character if the next character is a `\n` - [x] what about macros? do we need to watch out for that? (defer to #48704) - [x] also it doesn't work for `extern crate foo; fn main() { foo::bar(); }` - this is subtle: the `foo` might be shadowing a glob import too, can't always remove - defer to #51177 - [x] we also don't do the `pub use` rewrite thang (#51013) Spun off from #51010 Fixes #50672 r? @alexcrichton
The following code
generates an error:
but if the
extern crate libc;
is removed it no longer compiles!This situation has come up on two occasions so far:
use libc;
and rustfix rewrote it touse crate::libc;
. This worked in the 2015 edition becauseextern crate libc;
was declared at the top. Later thisunnecessary_extern_crate
lint ended up showing the error above.crate
lint for single-item paths #50665 theuse *;
statement is entirely disallowed in the 2018 edition, so we instead recommend the replacementuse crate::*;
. This, however, will also break if one of the identifiers used is anextern crate
as the lint currently suggests removingextern crate
The text was updated successfully, but these errors were encountered: