Skip to content

[WIP] Internal lint: warn on public cross-crate re-exports #77479

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,37 @@ fn gen_args(segment: &PathSegment<'_>) -> String {
String::new()
}

declare_tool_lint! {
pub rustc::PUB_CROSS_CRATE_REEXPORT,
Allow,
"re-exporting items across crates",
report_in_external_macro: true
}

declare_lint_pass!(PubReexportChecker => [PUB_CROSS_CRATE_REEXPORT]);

impl<'tcx> LateLintPass<'tcx> for PubReexportChecker {
fn check_item(&mut self, cx: &LateContext<'_>, item: &'tcx rustc_hir::Item<'tcx>) {
use rustc_hir::def_id::LOCAL_CRATE;

if item.vis.node.is_pub() {
if let rustc_hir::ItemKind::Use(path, _kind) = item.kind {
if path.res.def_id().krate != LOCAL_CRATE {
cx.struct_span_lint(
PUB_CROSS_CRATE_REEXPORT,
item.span,
|lint| {
lint.build("publicly re-exporting an item from a different crate")
.note("facade crates are discouraged; import from the original crate instead")
.emit();
},
);
}
}
}
}
}

declare_tool_lint! {
pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
Allow,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ fn register_internals(store: &mut LintStore) {
store.register_early_pass(|| box LintPassImpl);
store.register_lints(&TyTyKind::get_lints());
store.register_late_pass(|| box TyTyKind);
store.register_lints(&PubReexportChecker::get_lints());
store.register_late_pass(|| box PubReexportChecker);
store.register_group(
false,
"rustc::internal",
Expand All @@ -461,6 +463,7 @@ fn register_internals(store: &mut LintStore) {
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
LintId::of(TY_PASS_BY_REFERENCE),
LintId::of(USAGE_OF_QUALIFIED_TY),
LintId::of(PUB_CROSS_CRATE_REEXPORT),
],
);
}