Make LateCtxt be a type info delegate for EUV for clippy

This commit is contained in:
Michael Goulet 2024-05-08 14:08:18 -04:00
parent dfc9c9132b
commit db193c1c9d
9 changed files with 21 additions and 60 deletions

View File

@ -1,7 +1,6 @@
use clippy_utils::diagnostics::span_lint_hir;
use rustc_hir::{intravisit, AssocItemKind, Body, FnDecl, HirId, HirIdSet, Impl, ItemKind, Node, Pat, PatKind};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::layout::LayoutOf;
@ -105,8 +104,7 @@ fn check_fn(
too_large_for_stack: self.too_large_for_stack,
};
let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);
ExprUseVisitor::for_clippy(cx, fn_def_id, &mut v).consume_body(body);
for node in v.set {
span_lint_hir(

View File

@ -4,7 +4,6 @@
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{BindingMode, Expr, ExprKind, HirId, Node, PatKind};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext;
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty;
@ -61,13 +60,10 @@ fn check_for_mutation(
span_low: None,
span_high: None,
};
let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(
&mut delegate,
&infcx,
ExprUseVisitor::for_clippy(
cx,
body.hir_id.owner.def_id,
cx.param_env,
cx.typeck_results(),
&mut delegate,
)
.walk_expr(body);

View File

@ -69,14 +69,11 @@ pub(super) fn check<'tcx>(
let mut delegate = MoveDelegate {
used_move: HirIdSet::default(),
};
let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(
ExprUseVisitor::for_clippy(
cx,
closure.def_id,
&mut delegate,
&infcx,
closure.body.hir_id.owner.def_id,
cx.param_env,
cx.typeck_results(),
)
.consume_body(body);

View File

@ -11,7 +11,6 @@
PatKind,
};
use rustc_hir_typeck::expr_use_visitor as euv;
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::{self, Ty, TyCtxt, UpvarId, UpvarPath};
@ -102,7 +101,6 @@ fn should_skip<'tcx>(
fn check_closures<'tcx>(
ctx: &mut MutablyUsedVariablesCtxt<'tcx>,
cx: &LateContext<'tcx>,
infcx: &InferCtxt<'tcx>,
checked_closures: &mut FxHashSet<LocalDefId>,
closures: FxHashSet<LocalDefId>,
) {
@ -119,7 +117,7 @@ fn check_closures<'tcx>(
.associated_body()
.map(|(_, body_id)| hir.body(body_id))
{
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);
euv::ExprUseVisitor::for_clippy(cx, closure, &mut *ctx).consume_body(body);
}
}
}
@ -196,8 +194,7 @@ fn check_fn(
async_closures: FxHashSet::default(),
tcx: cx.tcx,
};
let infcx = cx.tcx.infer_ctxt().build();
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);
euv::ExprUseVisitor::for_clippy(cx, fn_def_id, &mut ctx).consume_body(body);
let mut checked_closures = FxHashSet::default();
@ -210,13 +207,13 @@ fn check_fn(
}
ControlFlow::<()>::Continue(())
});
check_closures(&mut ctx, cx, &infcx, &mut checked_closures, closures);
check_closures(&mut ctx, cx, &mut checked_closures, closures);
if is_async {
while !ctx.async_closures.is_empty() {
let async_closures = ctx.async_closures.clone();
ctx.async_closures.clear();
check_closures(&mut ctx, cx, &infcx, &mut checked_closures, async_closures);
check_closures(&mut ctx, cx, &mut checked_closures, async_closures);
}
}
ctx.generate_mutably_used_ids_from_aliases()

View File

@ -13,7 +13,6 @@
TyKind,
};
use rustc_hir_typeck::expr_use_visitor as euv;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@ -134,8 +133,7 @@ fn check_fn(
// function body.
let MovedVariablesCtxt { moved_vars } = {
let mut ctx = MovedVariablesCtxt::default();
let infcx = cx.tcx.infer_ctxt().build();
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);
euv::ExprUseVisitor::for_clippy(cx, fn_def_id, &mut ctx).consume_body(body);
ctx
};

View File

@ -11,7 +11,6 @@
use rustc_lint::LateContext;
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::BorrowKind;
use rustc_trait_selection::infer::TyCtxtInferExt;
use super::ASSIGN_OP_PATTERN;
@ -119,14 +118,7 @@ fn copy(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
}
let mut s = S(HirIdSet::default());
let infcx = cx.tcx.infer_ctxt().build();
let v = ExprUseVisitor::new(
&mut s,
&infcx,
cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap()),
cx.param_env,
cx.typeck_results(),
);
let v = ExprUseVisitor::for_clippy(cx, e.hir_id.owner.def_id, &mut s);
v.consume_expr(e);
s.0
}
@ -151,14 +143,7 @@ fn copy(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
}
let mut s = S(HirIdSet::default());
let infcx = cx.tcx.infer_ctxt().build();
let v = ExprUseVisitor::new(
&mut s,
&infcx,
cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap()),
cx.param_env,
cx.typeck_results(),
);
let v = ExprUseVisitor::for_clippy(cx, e.hir_id.owner.def_id, &mut s);
v.consume_expr(e);
s.0
}

View File

@ -6,7 +6,6 @@
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Node, PathSegment, UnOp};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceWithHirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
use rustc_middle::lint::in_external_macro;
@ -252,13 +251,10 @@ fn visit_branch(
local_id: unwrap_info.local_id,
};
let infcx = self.cx.tcx.infer_ctxt().build();
let vis = ExprUseVisitor::new(
&mut delegate,
&infcx,
let vis = ExprUseVisitor::for_clippy(
self.cx,
cond.hir_id.owner.def_id,
self.cx.param_env,
self.cx.typeck_results(),
&mut delegate,
);
vis.walk_expr(cond);
vis.walk_expr(branch);

View File

@ -11,7 +11,6 @@
use rustc_hir as hir;
use rustc_hir::{Closure, ExprKind, HirId, MutTy, TyKind};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{EarlyContext, LateContext, LintContext};
use rustc_middle::hir::place::ProjectionKind;
use rustc_middle::mir::{FakeReadCause, Mutability};
@ -831,8 +830,7 @@ pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Opti
applicability: Applicability::MachineApplicable,
};
let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(&mut visitor, &infcx, def_id, cx.param_env, cx.typeck_results()).consume_body(closure_body);
ExprUseVisitor::for_clippy(cx, def_id, &mut visitor).consume_body(closure_body);
if !visitor.suggestion_start.is_empty() {
return Some(DerefClosure {

View File

@ -5,7 +5,6 @@
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{self as hir, Expr, ExprKind, HirId, HirIdSet};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, Place, PlaceBase, PlaceWithHirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext;
use rustc_middle::hir::nested_filter;
use rustc_middle::mir::FakeReadCause;
@ -17,13 +16,10 @@ pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) ->
used_mutably: HirIdSet::default(),
skip: false,
};
let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(
&mut delegate,
&infcx,
ExprUseVisitor::for_clippy(
cx,
expr.hir_id.owner.def_id,
cx.param_env,
cx.typeck_results(),
&mut delegate,
)
.walk_expr(expr);