Skip to content

Commit 0897e3e

Browse files
hir::-ify internal lints
1 parent 75530e9 commit 0897e3e

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

compiler/rustc_lint/src/internal.rs

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
22
//! Clippy.
33
4-
use rustc_ast as ast;
4+
use rustc_hir::HirId;
55
use rustc_hir::def::Res;
66
use rustc_hir::def_id::DefId;
7-
use rustc_hir::{
8-
AmbigArg, BinOp, BinOpKind, Expr, ExprKind, GenericArg, HirId, Impl, Item, ItemKind, Node, Pat,
9-
PatExpr, PatExprKind, PatKind, Path, PathSegment, QPath, Ty, TyKind,
10-
};
117
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy};
128
use rustc_session::{declare_lint_pass, declare_tool_lint};
139
use rustc_span::hygiene::{ExpnKind, MacroKind};
1410
use rustc_span::{Span, sym};
1511
use tracing::debug;
12+
use {rustc_ast as ast, rustc_hir as hir};
1613

1714
use crate::lints::{
1815
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
@@ -37,9 +34,12 @@ declare_tool_lint! {
3734
declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
3835

3936
impl LateLintPass<'_> for DefaultHashTypes {
40-
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) {
37+
fn check_path(&mut self, cx: &LateContext<'_>, path: &hir::Path<'_>, hir_id: HirId) {
4138
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
42-
if matches!(cx.tcx.hir_node(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
39+
if matches!(
40+
cx.tcx.hir_node(hir_id),
41+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Use(..), .. })
42+
) {
4343
// Don't lint imports, only actual usages.
4444
return;
4545
}
@@ -60,10 +60,10 @@ impl LateLintPass<'_> for DefaultHashTypes {
6060
/// get the `DefId` and `GenericArgsRef` of the function.
6161
fn typeck_results_of_method_fn<'tcx>(
6262
cx: &LateContext<'tcx>,
63-
expr: &Expr<'_>,
63+
expr: &hir::Expr<'_>,
6464
) -> Option<(Span, DefId, ty::GenericArgsRef<'tcx>)> {
6565
match expr.kind {
66-
ExprKind::MethodCall(segment, ..)
66+
hir::ExprKind::MethodCall(segment, ..)
6767
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
6868
{
6969
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id)))
@@ -102,7 +102,7 @@ declare_tool_lint! {
102102
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]);
103103

104104
impl LateLintPass<'_> for QueryStability {
105-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
105+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
106106
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
107107
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args)
108108
{
@@ -164,21 +164,25 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
164164
}
165165
}
166166

167-
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx, AmbigArg>) {
167+
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
168168
match &ty.kind {
169-
TyKind::Path(QPath::Resolved(_, path)) => {
169+
hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
170170
if lint_ty_kind_usage(cx, &path.res) {
171171
let span = match cx.tcx.parent_hir_node(ty.hir_id) {
172-
Node::PatExpr(PatExpr { kind: PatExprKind::Path(qpath), .. })
173-
| Node::Pat(Pat {
174-
kind: PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..),
172+
hir::Node::PatExpr(hir::PatExpr {
173+
kind: hir::PatExprKind::Path(qpath),
174+
..
175+
})
176+
| hir::Node::Pat(hir::Pat {
177+
kind:
178+
hir::PatKind::TupleStruct(qpath, ..) | hir::PatKind::Struct(qpath, ..),
175179
..
176180
})
177-
| Node::Expr(
178-
Expr { kind: ExprKind::Path(qpath), .. }
179-
| &Expr { kind: ExprKind::Struct(qpath, ..), .. },
181+
| hir::Node::Expr(
182+
hir::Expr { kind: hir::ExprKind::Path(qpath), .. }
183+
| &hir::Expr { kind: hir::ExprKind::Struct(qpath, ..), .. },
180184
) => {
181-
if let QPath::TypeRelative(qpath_ty, ..) = qpath
185+
if let hir::QPath::TypeRelative(qpath_ty, ..) = qpath
182186
&& qpath_ty.hir_id == ty.hir_id
183187
{
184188
Some(path.span)
@@ -223,7 +227,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_>, res: &Res) -> bool {
223227
}
224228
}
225229

226-
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
230+
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &hir::Path<'_>) -> Option<String> {
227231
match &path.res {
228232
Res::Def(_, def_id) => {
229233
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(*def_id) {
@@ -244,13 +248,17 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
244248
None
245249
}
246250

247-
fn gen_args(segment: &PathSegment<'_>) -> String {
251+
fn gen_args(segment: &hir::PathSegment<'_>) -> String {
248252
if let Some(args) = &segment.args {
249253
let lifetimes = args
250254
.args
251255
.iter()
252256
.filter_map(|arg| {
253-
if let GenericArg::Lifetime(lt) = arg { Some(lt.ident.to_string()) } else { None }
257+
if let hir::GenericArg::Lifetime(lt) = arg {
258+
Some(lt.ident.to_string())
259+
} else {
260+
None
261+
}
254262
})
255263
.collect::<Vec<_>>();
256264

@@ -284,7 +292,7 @@ declare_tool_lint! {
284292
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]);
285293

286294
impl<'tcx> LateLintPass<'tcx> for TypeIr {
287-
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
295+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
288296
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
289297

290298
let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id);
@@ -394,23 +402,23 @@ declare_tool_lint! {
394402
declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL]);
395403

396404
impl LateLintPass<'_> for Diagnostics {
397-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
398-
let collect_args_tys_and_spans = |args: &[Expr<'_>], reserve_one_extra: bool| {
405+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
406+
let collect_args_tys_and_spans = |args: &[hir::Expr<'_>], reserve_one_extra: bool| {
399407
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra));
400408
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span)));
401409
result
402410
};
403411
// Only check function calls and method calls.
404412
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind {
405-
ExprKind::Call(callee, args) => {
413+
hir::ExprKind::Call(callee, args) => {
406414
match cx.typeck_results().node_type(callee.hir_id).kind() {
407415
&ty::FnDef(def_id, fn_gen_args) => {
408416
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false))
409417
}
410418
_ => return, // occurs for fns passed as args
411419
}
412420
}
413-
ExprKind::MethodCall(_segment, _recv, args, _span) => {
421+
hir::ExprKind::MethodCall(_segment, _recv, args, _span) => {
414422
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr)
415423
else {
416424
return;
@@ -514,8 +522,8 @@ impl Diagnostics {
514522
let mut is_inside_appropriate_impl = false;
515523
for (_hir_id, parent) in cx.tcx.hir_parent_iter(current_id) {
516524
debug!(?parent);
517-
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent
518-
&& let Impl { of_trait: Some(of_trait), .. } = impl_
525+
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) = parent
526+
&& let hir::Impl { of_trait: Some(of_trait), .. } = impl_
519527
&& let Some(def_id) = of_trait.trait_def_id()
520528
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
521529
&& matches!(name, sym::Diagnostic | sym::Subdiagnostic | sym::LintDiagnostic)
@@ -543,8 +551,8 @@ declare_tool_lint! {
543551
declare_lint_pass!(BadOptAccess => [BAD_OPT_ACCESS]);
544552

545553
impl LateLintPass<'_> for BadOptAccess {
546-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
547-
let ExprKind::Field(base, target) = expr.kind else { return };
554+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
555+
let hir::ExprKind::Field(base, target) = expr.kind else { return };
548556
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
549557
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
550558
// avoided.
@@ -581,9 +589,12 @@ declare_tool_lint! {
581589
declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
582590

583591
impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
584-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
585-
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq | BinOpKind::Ne, .. }, lhs, rhs) =
586-
expr.kind
592+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
593+
if let hir::ExprKind::Binary(
594+
hir::BinOp { node: hir::BinOpKind::Eq | hir::BinOpKind::Ne, .. },
595+
lhs,
596+
rhs,
597+
) = expr.kind
587598
{
588599
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
589600
cx.emit_span_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
@@ -592,9 +603,9 @@ impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
592603
}
593604
}
594605

595-
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
606+
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
596607
match &expr.kind {
597-
ExprKind::MethodCall(..) => cx
608+
hir::ExprKind::MethodCall(..) => cx
598609
.typeck_results()
599610
.type_dependent_def_id(expr.hir_id)
600611
.is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
@@ -617,11 +628,11 @@ declare_lint_pass!(SymbolInternStringLiteral => [SYMBOL_INTERN_STRING_LITERAL]);
617628

618629
impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
619630
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
620-
if let ExprKind::Call(path, [arg]) = expr.kind
621-
&& let ExprKind::Path(ref qpath) = path.kind
631+
if let hir::ExprKind::Call(path, [arg]) = expr.kind
632+
&& let hir::ExprKind::Path(ref qpath) = path.kind
622633
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
623634
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
624-
&& let ExprKind::Lit(kind) = arg.kind
635+
&& let hir::ExprKind::Lit(kind) = arg.kind
625636
&& let rustc_ast::LitKind::Str(_, _) = kind.node
626637
{
627638
cx.emit_span_lint(

0 commit comments

Comments
 (0)