Skip to content

Lint for removing extern crate has false positive when crate is used #50672

Closed
@alexcrichton

Description

@alexcrichton

The following code

#![feature(rust_2018_preview)]
#![deny(unnecessary_extern_crate)]
#![allow(unused_imports)]

extern crate libc;

mod foo {
    use crate::libc;
}

fn main() {}

generates an error:

error: `extern crate` is unnecessary in the new edition
 --> src/main.rs:5:1
  |
5 | extern crate libc;
  | ^^^^^^^^^^^^^^^^^^ help: remove it
  |
note: lint level defined here
 --> src/main.rs:2:9
  |
2 | #![deny(unnecessary_extern_crate)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `playground`.

but if the extern crate libc; is removed it no longer compiles!

This situation has come up on two occasions so far:

  1. In attempting to fix 2018 compatibility lint isn't firing for all imports that need to be rewritten #50660 the lint started firing for use libc; and rustfix rewrote it to use crate::libc;. This worked in the 2015 edition because extern crate libc; was declared at the top. Later this unnecessary_extern_crate lint ended up showing the error above.
  2. Discovered in rustc: Fix crate lint for single-item paths  #50665 the use *; statement is entirely disallowed in the 2018 edition, so we instead recommend the replacement use crate::*;. This, however, will also break if one of the identifiers used is an extern crate as the lint currently suggests removing extern crate

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions