hir::-ify internal lints
This commit is contained in:
parent
068609ce76
commit
ca6dad3eab
1 changed files with 50 additions and 39 deletions
|
@ -1,18 +1,15 @@
|
||||||
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
|
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
|
||||||
//! Clippy.
|
//! Clippy.
|
||||||
|
|
||||||
use rustc_ast as ast;
|
use rustc_hir::HirId;
|
||||||
use rustc_hir::def::Res;
|
use rustc_hir::def::Res;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_hir::{
|
|
||||||
AmbigArg, BinOp, BinOpKind, Expr, ExprKind, GenericArg, HirId, Impl, Item, ItemKind, Node, Pat,
|
|
||||||
PatExpr, PatExprKind, PatKind, Path, PathSegment, QPath, Ty, TyKind,
|
|
||||||
};
|
|
||||||
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy};
|
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||||
use rustc_span::{Span, sym};
|
use rustc_span::{Span, sym};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
use {rustc_ast as ast, rustc_hir as hir};
|
||||||
|
|
||||||
use crate::lints::{
|
use crate::lints::{
|
||||||
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
|
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
|
||||||
|
@ -37,9 +34,12 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
|
declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for DefaultHashTypes {
|
impl LateLintPass<'_> for DefaultHashTypes {
|
||||||
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) {
|
fn check_path(&mut self, cx: &LateContext<'_>, path: &hir::Path<'_>, hir_id: HirId) {
|
||||||
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
|
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
|
||||||
if matches!(cx.tcx.hir_node(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
|
if matches!(
|
||||||
|
cx.tcx.hir_node(hir_id),
|
||||||
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Use(..), .. })
|
||||||
|
) {
|
||||||
// Don't lint imports, only actual usages.
|
// Don't lint imports, only actual usages.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -60,10 +60,10 @@ impl LateLintPass<'_> for DefaultHashTypes {
|
||||||
/// get the `DefId` and `GenericArgsRef` of the function.
|
/// get the `DefId` and `GenericArgsRef` of the function.
|
||||||
fn typeck_results_of_method_fn<'tcx>(
|
fn typeck_results_of_method_fn<'tcx>(
|
||||||
cx: &LateContext<'tcx>,
|
cx: &LateContext<'tcx>,
|
||||||
expr: &Expr<'_>,
|
expr: &hir::Expr<'_>,
|
||||||
) -> Option<(Span, DefId, ty::GenericArgsRef<'tcx>)> {
|
) -> Option<(Span, DefId, ty::GenericArgsRef<'tcx>)> {
|
||||||
match expr.kind {
|
match expr.kind {
|
||||||
ExprKind::MethodCall(segment, ..)
|
hir::ExprKind::MethodCall(segment, ..)
|
||||||
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
|
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
|
||||||
{
|
{
|
||||||
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id)))
|
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id)))
|
||||||
|
@ -102,7 +102,7 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]);
|
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for QueryStability {
|
impl LateLintPass<'_> for QueryStability {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
||||||
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
|
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
|
||||||
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args)
|
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args)
|
||||||
{
|
{
|
||||||
|
@ -164,21 +164,25 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx, AmbigArg>) {
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
|
||||||
match &ty.kind {
|
match &ty.kind {
|
||||||
TyKind::Path(QPath::Resolved(_, path)) => {
|
hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
|
||||||
if lint_ty_kind_usage(cx, &path.res) {
|
if lint_ty_kind_usage(cx, &path.res) {
|
||||||
let span = match cx.tcx.parent_hir_node(ty.hir_id) {
|
let span = match cx.tcx.parent_hir_node(ty.hir_id) {
|
||||||
Node::PatExpr(PatExpr { kind: PatExprKind::Path(qpath), .. })
|
hir::Node::PatExpr(hir::PatExpr {
|
||||||
| Node::Pat(Pat {
|
kind: hir::PatExprKind::Path(qpath),
|
||||||
kind: PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..),
|
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
| Node::Expr(
|
| hir::Node::Pat(hir::Pat {
|
||||||
Expr { kind: ExprKind::Path(qpath), .. }
|
kind:
|
||||||
| &Expr { kind: ExprKind::Struct(qpath, ..), .. },
|
hir::PatKind::TupleStruct(qpath, ..) | hir::PatKind::Struct(qpath, ..),
|
||||||
|
..
|
||||||
|
})
|
||||||
|
| hir::Node::Expr(
|
||||||
|
hir::Expr { kind: hir::ExprKind::Path(qpath), .. }
|
||||||
|
| &hir::Expr { kind: hir::ExprKind::Struct(qpath, ..), .. },
|
||||||
) => {
|
) => {
|
||||||
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
if let hir::QPath::TypeRelative(qpath_ty, ..) = qpath
|
||||||
&& qpath_ty.hir_id == ty.hir_id
|
&& qpath_ty.hir_id == ty.hir_id
|
||||||
{
|
{
|
||||||
Some(path.span)
|
Some(path.span)
|
||||||
|
@ -223,7 +227,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_>, res: &Res) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
|
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &hir::Path<'_>) -> Option<String> {
|
||||||
match &path.res {
|
match &path.res {
|
||||||
Res::Def(_, def_id) => {
|
Res::Def(_, def_id) => {
|
||||||
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(*def_id) {
|
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> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_args(segment: &PathSegment<'_>) -> String {
|
fn gen_args(segment: &hir::PathSegment<'_>) -> String {
|
||||||
if let Some(args) = &segment.args {
|
if let Some(args) = &segment.args {
|
||||||
let lifetimes = args
|
let lifetimes = args
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|arg| {
|
.filter_map(|arg| {
|
||||||
if let GenericArg::Lifetime(lt) = arg { Some(lt.ident.to_string()) } else { None }
|
if let hir::GenericArg::Lifetime(lt) = arg {
|
||||||
|
Some(lt.ident.to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
@ -284,7 +292,7 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]);
|
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for TypeIr {
|
impl<'tcx> LateLintPass<'tcx> for TypeIr {
|
||||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
||||||
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
|
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 is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id);
|
||||||
|
@ -394,15 +402,15 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL]);
|
declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for Diagnostics {
|
impl LateLintPass<'_> for Diagnostics {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
||||||
let collect_args_tys_and_spans = |args: &[Expr<'_>], reserve_one_extra: bool| {
|
let collect_args_tys_and_spans = |args: &[hir::Expr<'_>], reserve_one_extra: bool| {
|
||||||
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra));
|
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra));
|
||||||
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span)));
|
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span)));
|
||||||
result
|
result
|
||||||
};
|
};
|
||||||
// Only check function calls and method calls.
|
// Only check function calls and method calls.
|
||||||
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind {
|
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind {
|
||||||
ExprKind::Call(callee, args) => {
|
hir::ExprKind::Call(callee, args) => {
|
||||||
match cx.typeck_results().node_type(callee.hir_id).kind() {
|
match cx.typeck_results().node_type(callee.hir_id).kind() {
|
||||||
&ty::FnDef(def_id, fn_gen_args) => {
|
&ty::FnDef(def_id, fn_gen_args) => {
|
||||||
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false))
|
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false))
|
||||||
|
@ -410,7 +418,7 @@ impl LateLintPass<'_> for Diagnostics {
|
||||||
_ => return, // occurs for fns passed as args
|
_ => return, // occurs for fns passed as args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprKind::MethodCall(_segment, _recv, args, _span) => {
|
hir::ExprKind::MethodCall(_segment, _recv, args, _span) => {
|
||||||
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr)
|
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr)
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
|
@ -514,8 +522,8 @@ impl Diagnostics {
|
||||||
let mut is_inside_appropriate_impl = false;
|
let mut is_inside_appropriate_impl = false;
|
||||||
for (_hir_id, parent) in cx.tcx.hir_parent_iter(current_id) {
|
for (_hir_id, parent) in cx.tcx.hir_parent_iter(current_id) {
|
||||||
debug!(?parent);
|
debug!(?parent);
|
||||||
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent
|
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) = parent
|
||||||
&& let Impl { of_trait: Some(of_trait), .. } = impl_
|
&& let hir::Impl { of_trait: Some(of_trait), .. } = impl_
|
||||||
&& let Some(def_id) = of_trait.trait_def_id()
|
&& let Some(def_id) = of_trait.trait_def_id()
|
||||||
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
|
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
|
||||||
&& matches!(name, sym::Diagnostic | sym::Subdiagnostic | sym::LintDiagnostic)
|
&& matches!(name, sym::Diagnostic | sym::Subdiagnostic | sym::LintDiagnostic)
|
||||||
|
@ -543,8 +551,8 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(BadOptAccess => [BAD_OPT_ACCESS]);
|
declare_lint_pass!(BadOptAccess => [BAD_OPT_ACCESS]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for BadOptAccess {
|
impl LateLintPass<'_> for BadOptAccess {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
||||||
let ExprKind::Field(base, target) = expr.kind else { return };
|
let hir::ExprKind::Field(base, target) = expr.kind else { return };
|
||||||
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
|
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
|
||||||
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
|
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
|
||||||
// avoided.
|
// avoided.
|
||||||
|
@ -581,9 +589,12 @@ declare_tool_lint! {
|
||||||
declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
|
declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
|
impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
|
||||||
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq | BinOpKind::Ne, .. }, lhs, rhs) =
|
if let hir::ExprKind::Binary(
|
||||||
expr.kind
|
hir::BinOp { node: hir::BinOpKind::Eq | hir::BinOpKind::Ne, .. },
|
||||||
|
lhs,
|
||||||
|
rhs,
|
||||||
|
) = expr.kind
|
||||||
{
|
{
|
||||||
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
|
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
|
||||||
cx.emit_span_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
|
cx.emit_span_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
|
||||||
|
@ -592,9 +603,9 @@ impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
|
||||||
match &expr.kind {
|
match &expr.kind {
|
||||||
ExprKind::MethodCall(..) => cx
|
hir::ExprKind::MethodCall(..) => cx
|
||||||
.typeck_results()
|
.typeck_results()
|
||||||
.type_dependent_def_id(expr.hir_id)
|
.type_dependent_def_id(expr.hir_id)
|
||||||
.is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
|
.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]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
|
impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
|
||||||
if let ExprKind::Call(path, [arg]) = expr.kind
|
if let hir::ExprKind::Call(path, [arg]) = expr.kind
|
||||||
&& let ExprKind::Path(ref qpath) = path.kind
|
&& let hir::ExprKind::Path(ref qpath) = path.kind
|
||||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||||
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
|
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
|
||||||
&& let ExprKind::Lit(kind) = arg.kind
|
&& let hir::ExprKind::Lit(kind) = arg.kind
|
||||||
&& let rustc_ast::LitKind::Str(_, _) = kind.node
|
&& let rustc_ast::LitKind::Str(_, _) = kind.node
|
||||||
{
|
{
|
||||||
cx.emit_span_lint(
|
cx.emit_span_lint(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue