Skip to content

Commit 78fd719

Browse files
committed
Add idiom lint for bare extern crate
1 parent 707b696 commit 78fd719

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/librustc_lint/builtin.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,3 +1509,39 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedBrokenConst {
15091509
}
15101510
}
15111511
}
1512+
1513+
declare_lint! {
1514+
pub UNNECESSARY_EXTERN_CRATE,
1515+
Allow,
1516+
"suggest removing `extern crate` for the 2018 edition"
1517+
}
1518+
1519+
pub struct ExternCrate;
1520+
1521+
impl LintPass for ExternCrate {
1522+
fn get_lints(&self) -> LintArray {
1523+
lint_array!(UNNECESSARY_EXTERN_CRATE)
1524+
}
1525+
}
1526+
1527+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExternCrate {
1528+
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
1529+
if let hir::ItemExternCrate(ref orig) = it.node {
1530+
if it.attrs.iter().any(|a| a.check_name("macro_use")) {
1531+
return
1532+
}
1533+
let mut err = cx.struct_span_lint(UNNECESSARY_EXTERN_CRATE,
1534+
it.span, "`extern crate` is unnecessary in the new edition");
1535+
if it.vis == hir::Visibility::Public {
1536+
if let Some(orig) = orig {
1537+
err.span_suggestion(it.span, "use `pub use`",
1538+
format!("pub use {} as {}", orig, it.name));
1539+
} else {
1540+
err.span_suggestion(it.span, "use `pub use`",
1541+
format!("pub use {}", it.name));
1542+
}
1543+
err.emit();
1544+
}
1545+
}
1546+
}
1547+
}

src/librustc_lint/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
137137
UnreachablePub,
138138
TypeAliasBounds,
139139
UnusedBrokenConst,
140+
ExternCrate,
140141
);
141142

142143
add_builtin_with_new!(sess,
@@ -180,7 +181,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
180181
add_lint_group!(sess,
181182
"rust_2018_idioms",
182183
BARE_TRAIT_OBJECT,
183-
UNREACHABLE_PUB);
184+
UNREACHABLE_PUB,
185+
UNNECESSARY_EXTERN_CRATE);
184186

185187
// Guidelines for creating a future incompatibility lint:
186188
//

0 commit comments

Comments
 (0)