-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Restrict sysroot crate imports to those defined in this repo. #143548
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,10 @@ use tracing::debug; | |
use {rustc_ast as ast, rustc_hir as hir}; | ||
|
||
use crate::lints::{ | ||
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, | ||
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag, | ||
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrDirectUse, | ||
TypeIrInherentUsage, TypeIrTraitUsage, UntranslatableDiag, | ||
BadOptAccessDiag, DangerousExternCrateDiag, DefaultHashTypesDiag, DiagOutOfImpl, | ||
LintPassByHand, NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, | ||
SpanUseEqCtxtDiag, SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, | ||
TypeIrDirectUse, TypeIrInherentUsage, TypeIrTraitUsage, UntranslatableDiag, | ||
}; | ||
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; | ||
|
||
|
@@ -703,3 +703,37 @@ impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral { | |
} | ||
} | ||
} | ||
|
||
declare_tool_lint! { | ||
/// The `dangerous_extern_crate` detects use of `extern crate` to import non-whitelisted crates | ||
/// from the sysroot, which is dangerous because these crates are not guaranteed to exist | ||
/// exactly once, and so may be missing entirely or appear multiple times resulting in ambiguity. | ||
pub rustc::DANGEROUS_EXTERN_CRATE, | ||
Allow, | ||
"Forbid uses of non-whitelisted crates in `extern crate`", | ||
report_in_external_macro: true | ||
} | ||
|
||
declare_lint_pass!(DangerousExternCrate => [DANGEROUS_EXTERN_CRATE]); | ||
|
||
impl EarlyLintPass for DangerousExternCrate { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { | ||
fn is_whitelisted(crate_name: &str) -> bool { | ||
// Whitelist of allowed crates. | ||
crate_name.starts_with("rustc_") | ||
|| matches!(crate_name, "test" | "self" | "core" | "alloc" | "std" | "proc_macro") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is. You can for example do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah |
||
} | ||
|
||
if let ast::ItemKind::ExternCrate(original_name, imported_name) = &item.kind { | ||
let name = original_name.as_ref().unwrap_or(&imported_name.name).as_str(); | ||
let externs = &cx.builder.sess().opts.externs; | ||
if externs.get(name).is_none() && !is_whitelisted(name) { | ||
cx.emit_span_lint( | ||
DANGEROUS_EXTERN_CRATE, | ||
item.span, | ||
DangerousExternCrateDiag { name }, | ||
); | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.