-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add internal lint for detecting non-glob imports of rustc_type_ir::inherent
#127854
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 |
---|---|---|
|
@@ -3,7 +3,8 @@ | |
|
||
use crate::lints::{ | ||
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword, | ||
QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, TykindKind, UntranslatableDiag, | ||
NonGlobImportTypeIrInherent, QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, | ||
TykindKind, UntranslatableDiag, | ||
}; | ||
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; | ||
use rustc_ast as ast; | ||
|
@@ -263,6 +264,49 @@ fn gen_args(segment: &PathSegment<'_>) -> String { | |
String::new() | ||
} | ||
|
||
declare_tool_lint! { | ||
/// The `non_glob_import_of_type_ir_inherent_item` lint detects | ||
/// non-glob imports of module `rustc_type_ir::inherent`. | ||
pub rustc::NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, | ||
Allow, | ||
"non-glob import of `rustc_type_ir::inherent`", | ||
report_in_external_macro: true | ||
} | ||
|
||
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for TypeIr { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { | ||
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return }; | ||
|
||
let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id); | ||
let (lo, hi, snippet) = match path.segments { | ||
[.., penultimate, segment] | ||
if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) => | ||
{ | ||
(segment.ident.span, item.ident.span, "*") | ||
} | ||
[.., segment] | ||
if path.res.iter().flat_map(Res::opt_def_id).any(is_mod_inherent) | ||
&& let rustc_hir::UseKind::Single = kind => | ||
{ | ||
let (lo, snippet) = | ||
match cx.tcx.sess.source_map().span_to_snippet(path.span).as_deref() { | ||
Ok("self") => (path.span, "*"), | ||
Comment on lines
+294
to
+295
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. Super gross but I couldn't think of anything better w/o tweaking AST lowering (e.g., to use In HIR, there's no way to tell whether |
||
_ => (segment.ident.span.shrink_to_hi(), "::*"), | ||
}; | ||
(lo, if segment.ident == item.ident { lo } else { item.ident.span }, snippet) | ||
} | ||
_ => return, | ||
}; | ||
cx.emit_span_lint( | ||
NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, | ||
path.span, | ||
NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet }, | ||
); | ||
} | ||
} | ||
|
||
declare_tool_lint! { | ||
/// The `lint_pass_impl_without_macro` detects manual implementations of a lint | ||
/// pass, without using [`declare_lint_pass`] or [`impl_lint_pass`]. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::inherent::{AdtDef, IntoKind, Ty}; | ||
use crate::inherent::*; | ||
use crate::lang_items::TraitSolverLangItem::{EffectsMaybe, EffectsNoRuntime, EffectsRuntime}; | ||
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. omfg |
||
use crate::Interner; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//@ compile-flags: -Z unstable-options | ||
//@ ignore-stage1 (can be removed after beta bump, #[cfg(bootstrap)]) | ||
#![feature(rustc_private)] | ||
#![deny(rustc::non_glob_import_of_type_ir_inherent)] | ||
|
||
extern crate rustc_type_ir; | ||
|
||
mod ok { | ||
use rustc_type_ir::inherent::*; // OK | ||
use rustc_type_ir::inherent::{}; // OK | ||
use rustc_type_ir::inherent::{*}; // OK | ||
|
||
fn usage<T: rustc_type_ir::inherent::SliceLike>() {} // OK | ||
} | ||
|
||
mod direct { | ||
use rustc_type_ir::inherent::Predicate; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
use rustc_type_ir::inherent::{AdtDef, Ty}; | ||
//~^ ERROR non-glob import of `rustc_type_ir::inherent` | ||
//~| ERROR non-glob import of `rustc_type_ir::inherent` | ||
use rustc_type_ir::inherent::ParamEnv as _; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
} | ||
|
||
mod indirect0 { | ||
use rustc_type_ir::inherent; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
use rustc_type_ir::inherent as inh; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
use rustc_type_ir::{inherent as _}; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
|
||
fn usage0<T: inherent::SliceLike>() {} | ||
fn usage1<T: inh::SliceLike>() {} | ||
} | ||
|
||
mod indirect1 { | ||
use rustc_type_ir::inherent::{self}; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
use rustc_type_ir::inherent::{self as innate}; //~ ERROR non-glob import of `rustc_type_ir::inherent` | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:17:9 | ||
| | ||
LL | use rustc_type_ir::inherent::Predicate; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^--------- | ||
| | | ||
| help: try using a glob import instead: `*` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:4:9 | ||
| | ||
LL | #![deny(rustc::non_glob_import_of_type_ir_inherent)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:18:35 | ||
| | ||
LL | use rustc_type_ir::inherent::{AdtDef, Ty}; | ||
| ^^^^^^ help: try using a glob import instead: `*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:18:43 | ||
| | ||
LL | use rustc_type_ir::inherent::{AdtDef, Ty}; | ||
| ^^ help: try using a glob import instead: `*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:21:9 | ||
| | ||
LL | use rustc_type_ir::inherent::ParamEnv as _; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^------------- | ||
| | | ||
| help: try using a glob import instead: `*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:25:9 | ||
| | ||
LL | use rustc_type_ir::inherent; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^- help: try using a glob import instead: `::*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:26:9 | ||
| | ||
LL | use rustc_type_ir::inherent as inh; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^------- help: try using a glob import instead: `::*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:27:25 | ||
| | ||
LL | use rustc_type_ir::{inherent as _}; | ||
| ^^^^^^^^----- help: try using a glob import instead: `::*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:34:35 | ||
| | ||
LL | use rustc_type_ir::inherent::{self}; | ||
| ^^^^ help: try using a glob import instead: `*` | ||
|
||
error: non-glob import of `rustc_type_ir::inherent` | ||
--> $DIR/non_glob_import_of_type_ir_inherent.rs:35:35 | ||
| | ||
LL | use rustc_type_ir::inherent::{self as innate}; | ||
| ^^^^---------- | ||
| | | ||
| help: try using a glob import instead: `*` | ||
|
||
error: aborting due to 9 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to consolidate the lint passes
TypeIr
andTyTyKind
to reduce bloat. I'd just need to think of a good name 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"opinionated imports"?