1
Fork 0

Fix clippy::needless_borrow in the compiler

`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`.

Then I had to remove a few unnecessary parens and muts that were exposed
now.
This commit is contained in:
Nilstrieb 2023-11-21 20:07:32 +01:00
parent 0ff8610964
commit 21a870515b
304 changed files with 1101 additions and 1174 deletions

View file

@ -13,7 +13,7 @@ pub struct CheckConstItemMutation;
impl<'tcx> MirLint<'tcx> for CheckConstItemMutation {
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = ConstMutationChecker { body, tcx, target_local: None };
checker.visit_body(&body);
checker.visit_body(body);
}
}
@ -98,7 +98,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
if !lhs.projection.is_empty() {
if let Some(def_id) = self.is_const_item_without_destructor(lhs.local)
&& let Some((lint_root, span, item)) =
self.should_lint_const_item_usage(&lhs, def_id, loc)
self.should_lint_const_item_usage(lhs, def_id, loc)
{
self.tcx.emit_spanned_lint(
CONST_ITEM_MUTATION,
@ -132,12 +132,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
// the `self` parameter of a method call (as the terminator of our current
// BasicBlock). If so, we emit a more specific lint.
let method_did = self.target_local.and_then(|target_local| {
rustc_middle::util::find_self_call(
self.tcx,
&self.body,
target_local,
loc.block,
)
rustc_middle::util::find_self_call(self.tcx, self.body, target_local, loc.block)
});
let lint_loc =
if method_did.is_some() { self.body.terminator_loc(loc.block) } else { loc };

View file

@ -12,7 +12,7 @@ impl<'tcx> MirLint<'tcx> for CheckPackedRef {
let param_env = tcx.param_env(body.source.def_id());
let source_info = SourceInfo::outermost(body.span);
let mut checker = PackedRefChecker { body, tcx, param_env, source_info };
checker.visit_body(&body);
checker.visit_body(body);
}
}

View file

@ -494,7 +494,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResu
let param_env = tcx.param_env(def);
let mut checker = UnsafetyChecker::new(body, def, tcx, param_env);
checker.visit_body(&body);
checker.visit_body(body);
let unused_unsafes = (!tcx.is_typeck_child(def.to_def_id()))
.then(|| check_unused_unsafe(tcx, def, &checker.used_unsafe_blocks));

View file

@ -606,7 +606,7 @@ impl CanConstProp {
for arg in body.args_iter() {
cpv.found_assignment.insert(arg);
}
cpv.visit_body(&body);
cpv.visit_body(body);
cpv.can_const_prop
}
}

View file

@ -453,7 +453,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
cond: &Operand<'tcx>,
location: Location,
) -> Option<!> {
let value = &self.eval_operand(&cond, location)?;
let value = &self.eval_operand(cond, location)?;
trace!("assertion on {:?} should be {:?}", value, expected);
let expected = Scalar::from_bool(expected);
@ -626,7 +626,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
self.check_assertion(*expected, msg, cond, location);
}
TerminatorKind::SwitchInt { ref discr, ref targets } => {
if let Some(ref value) = self.eval_operand(&discr, location)
if let Some(ref value) = self.eval_operand(discr, location)
&& let Some(value_const) =
self.use_ecx(location, |this| this.ecx.read_scalar(value))
&& let Ok(constant) = value_const.try_to_int()

View file

@ -50,7 +50,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Replacer {
tcx,
copy_classes: &ssa.copy_classes(),
copy_classes: ssa.copy_classes(),
fully_moved,
borrowed_locals,
storage_to_remove,
@ -124,7 +124,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
}
fn visit_place(&mut self, place: &mut Place<'tcx>, ctxt: PlaceContext, loc: Location) {
if let Some(new_projection) = self.process_projection(&place.projection, loc) {
if let Some(new_projection) = self.process_projection(place.projection, loc) {
place.projection = self.tcx().mk_place_elems(&new_projection);
}

View file

@ -651,7 +651,7 @@ fn locals_live_across_suspend_points<'tcx>(
always_live_locals: &BitSet<Local>,
movable: bool,
) -> LivenessInfo {
let body_ref: &Body<'_> = &body;
let body_ref: &Body<'_> = body;
// Calculate when MIR locals have live storage. This gives us an upper bound of their
// lifetimes.
@ -742,7 +742,7 @@ fn locals_live_across_suspend_points<'tcx>(
// saving.
let live_locals_at_suspension_points = live_locals_at_suspension_points
.iter()
.map(|live_here| saved_locals.renumber_bitset(&live_here))
.map(|live_here| saved_locals.renumber_bitset(live_here))
.collect();
let storage_conflicts = compute_storage_conflicts(
@ -778,7 +778,7 @@ impl CoroutineSavedLocals {
/// Transforms a `BitSet<Local>` that contains only locals saved across yield points to the
/// equivalent `BitSet<CoroutineSavedLocal>`.
fn renumber_bitset(&self, input: &BitSet<Local>) -> BitSet<CoroutineSavedLocal> {
assert!(self.superset(&input), "{:?} not a superset of {:?}", self.0, input);
assert!(self.superset(input), "{:?} not a superset of {:?}", self.0, input);
let mut out = BitSet::new_empty(self.count());
for (saved_local, local) in self.iter_enumerated() {
if input.contains(local) {
@ -829,7 +829,7 @@ fn compute_storage_conflicts<'mir, 'tcx>(
// Compute the storage conflicts for all eligible locals.
let mut visitor = StorageConflictVisitor {
body,
saved_locals: &saved_locals,
saved_locals: saved_locals,
local_conflicts: BitMatrix::from_row_n(&ineligible_locals, body.local_decls.len()),
};
@ -1128,7 +1128,7 @@ fn create_coroutine_drop_shim<'tcx>(
// The returned state and the poisoned state fall through to the default
// case which is just to return
insert_switch(&mut body, cases, &transform, TerminatorKind::Return);
insert_switch(&mut body, cases, transform, TerminatorKind::Return);
for block in body.basic_blocks_mut() {
let kind = &mut block.terminator_mut().kind;
@ -1465,7 +1465,7 @@ pub(crate) fn mir_coroutine_witnesses<'tcx>(
// The witness simply contains all locals live across suspend points.
let always_live_locals = always_storage_live_locals(&body);
let always_live_locals = always_storage_live_locals(body);
let liveness_info = locals_live_across_suspend_points(tcx, body, &always_live_locals, movable);
// Extract locals which are live across suspension point into `layout`
@ -1473,7 +1473,7 @@ pub(crate) fn mir_coroutine_witnesses<'tcx>(
// `storage_liveness` tells us which locals have live storage at suspension points
let (_, coroutine_layout, _) = compute_layout(liveness_info, body);
check_suspend_tys(tcx, &coroutine_layout, &body);
check_suspend_tys(tcx, &coroutine_layout, body);
Some(coroutine_layout)
}
@ -1564,7 +1564,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
},
);
let always_live_locals = always_storage_live_locals(&body);
let always_live_locals = always_storage_live_locals(body);
let liveness_info =
locals_live_across_suspend_points(tcx, body, &always_live_locals, movable);

View file

@ -222,7 +222,7 @@ impl<'a> MakeBcbCounters<'a> {
// all `BasicCoverageBlock` nodes in the loop are visited before visiting any node outside
// the loop. The `traversal` state includes a `context_stack`, providing a way to know if
// the current BCB is in one or more nested loops or not.
let mut traversal = TraverseCoverageGraphWithLoops::new(&self.basic_coverage_blocks);
let mut traversal = TraverseCoverageGraphWithLoops::new(self.basic_coverage_blocks);
while let Some(bcb) = traversal.next() {
if bcb_has_coverage_spans(bcb) {
debug!("{:?} has at least one coverage span. Get or make its counter", bcb);
@ -425,7 +425,7 @@ impl<'a> MakeBcbCounters<'a> {
traversal: &TraverseCoverageGraphWithLoops<'_>,
branches: &[BcbBranch],
) -> BcbBranch {
let good_reloop_branch = self.find_good_reloop_branch(traversal, &branches);
let good_reloop_branch = self.find_good_reloop_branch(traversal, branches);
if let Some(reloop_branch) = good_reloop_branch {
assert!(self.branch_has_no_counter(&reloop_branch));
debug!("Selecting reloop branch {reloop_branch:?} to get an expression");
@ -508,7 +508,7 @@ impl<'a> MakeBcbCounters<'a> {
fn bcb_branches(&self, from_bcb: BasicCoverageBlock) -> Vec<BcbBranch> {
self.bcb_successors(from_bcb)
.iter()
.map(|&to_bcb| BcbBranch::from_to(from_bcb, to_bcb, &self.basic_coverage_blocks))
.map(|&to_bcb| BcbBranch::from_to(from_bcb, to_bcb, self.basic_coverage_blocks))
.collect::<Vec<_>>()
}

View file

@ -38,7 +38,7 @@ impl CoverageGraph {
}
let bcb_data = &bcbs[bcb];
let mut bcb_successors = Vec::new();
for successor in bcb_filtered_successors(&mir_body, bcb_data.last_bb())
for successor in bcb_filtered_successors(mir_body, bcb_data.last_bb())
.filter_map(|successor_bb| bb_to_bcb[successor_bb])
{
if !seen[successor] {

View file

@ -142,7 +142,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
////////////////////////////////////////////////////
// Compute coverage spans from the `CoverageGraph`.
let coverage_spans = CoverageSpans::generate_coverage_spans(
&self.mir_body,
self.mir_body,
fn_sig_span,
body_span,
&self.basic_coverage_blocks,
@ -240,7 +240,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
);
// Inject a counter into the newly-created BB.
inject_statement(self.mir_body, self.make_mir_coverage_kind(&counter_kind), new_bb);
inject_statement(self.mir_body, self.make_mir_coverage_kind(counter_kind), new_bb);
}
mappings

View file

@ -20,7 +20,7 @@ impl<'tcx> MirPass<'tcx> for CtfeLimit {
.filter_map(|(node, node_data)| {
if matches!(node_data.terminator().kind, TerminatorKind::Call { .. })
// Back edges in a CFG indicate loops
|| has_back_edge(&doms, node, &node_data)
|| has_back_edge(doms, node, node_data)
{
Some(node)
} else {

View file

@ -362,7 +362,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
&& let Ok(rhs_layout) = self.tcx.layout_of(self.param_env.and(rhs_ty))
{
let op = ImmTy::from_scalar(pointer, rhs_layout).into();
self.assign_constant(state, place, op, &rhs.projection);
self.assign_constant(state, place, op, rhs.projection);
}
}
Operand::Constant(box constant) => {

View file

@ -57,7 +57,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
// For types that do not need dropping, the behaviour is trivial. So we only need to track
// init/uninit for types that do need dropping.
let move_data =
MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
MoveData::gather_moves(body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
let elaborate_patch = {
let env = MoveDataParamEnv { move_data, param_env };
@ -67,7 +67,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
.pass_name("elaborate_drops")
.iterate_to_fixpoint()
.into_results_cursor(body);
let dead_unwinds = compute_dead_unwinds(&body, &mut inits);
let dead_unwinds = compute_dead_unwinds(body, &mut inits);
let uninits = MaybeUninitializedPlaces::new(tcx, body, &env)
.mark_inactive_variants_as_uninit()

View file

@ -14,7 +14,7 @@ pub struct FunctionItemReferences;
impl<'tcx> MirLint<'tcx> for FunctionItemReferences {
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = FunctionItemRefChecker { tcx, body };
checker.visit_body(&body);
checker.visit_body(body);
}
}
@ -47,12 +47,12 @@ impl<'tcx> Visitor<'tcx> for FunctionItemRefChecker<'_, 'tcx> {
for inner_ty in arg_ty.walk().filter_map(|arg| arg.as_type()) {
if let Some((fn_id, fn_args)) = FunctionItemRefChecker::is_fn_ref(inner_ty)
{
let span = self.nth_arg_span(&args, 0);
let span = self.nth_arg_span(args, 0);
self.emit_lint(fn_id, fn_args, source_info, span);
}
}
} else {
self.check_bound_args(def_id, args_ref, &args, source_info);
self.check_bound_args(def_id, args_ref, args, source_info);
}
}
}

View file

@ -280,7 +280,7 @@ impl<'tcx> Inliner<'tcx> {
}
let old_blocks = caller_body.basic_blocks.next_index();
self.inline_call(caller_body, &callsite, callee_body);
self.inline_call(caller_body, callsite, callee_body);
let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
Ok(new_blocks)

View file

@ -35,12 +35,9 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
}
}
ctx.simplify_primitive_clone(
&mut block.terminator.as_mut().unwrap(),
&mut block.statements,
);
ctx.simplify_primitive_clone(block.terminator.as_mut().unwrap(), &mut block.statements);
ctx.simplify_intrinsic_assert(
&mut block.terminator.as_mut().unwrap(),
block.terminator.as_mut().unwrap(),
&mut block.statements,
);
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());

View file

@ -95,7 +95,7 @@ impl<'tcx> MirPass<'tcx> for JumpThreading {
let cost = CostChecker::new(tcx, param_env, None, body);
let mut state = State::new(ConditionSet::default(), &finder.map);
let mut state = State::new(ConditionSet::default(), finder.map);
let conds = if let Some((value, then, else_)) = targets.as_static_if() {
let Some(value) = ScalarInt::try_from_uint(value, discr_layout.size) else {
@ -112,7 +112,7 @@ impl<'tcx> MirPass<'tcx> for JumpThreading {
}))
};
let conds = ConditionSet(conds);
state.insert_value_idx(discr, conds, &finder.map);
state.insert_value_idx(discr, conds, finder.map);
finder.find_opportunity(bb, state, cost, 0);
}

View file

@ -481,14 +481,14 @@ pub fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
assert!(body.phase == MirPhase::Analysis(AnalysisPhase::PostCleanup));
// Do a little drop elaboration before const-checking if `const_precise_live_drops` is enabled.
if check_consts::post_drop_elaboration::checking_enabled(&ConstCx::new(tcx, &body)) {
if check_consts::post_drop_elaboration::checking_enabled(&ConstCx::new(tcx, body)) {
pm::run_passes(
tcx,
body,
&[&remove_uninit_drops::RemoveUninitDrops, &simplify::SimplifyCfg::RemoveFalseEdges],
None,
);
check_consts::post_drop_elaboration::check_live_drops(tcx, &body); // FIXME: make this a MIR lint
check_consts::post_drop_elaboration::check_live_drops(tcx, body); // FIXME: make this a MIR lint
}
debug!("runtime_mir_lowering({:?})", did);

View file

@ -99,7 +99,7 @@ where
);
*polarity
});
overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess))
overridden.unwrap_or_else(|| pass.is_enabled(tcx.sess))
}
fn run_passes_inner<'tcx>(
@ -126,7 +126,7 @@ fn run_passes_inner<'tcx>(
let dump_enabled = pass.is_mir_dump_enabled();
if dump_enabled {
dump_mir_for_pass(tcx, body, &name, false);
dump_mir_for_pass(tcx, body, name, false);
}
if validate {
validate_body(tcx, body, format!("before pass {name}"));
@ -142,7 +142,7 @@ fn run_passes_inner<'tcx>(
}
if dump_enabled {
dump_mir_for_pass(tcx, body, &name, true);
dump_mir_for_pass(tcx, body, name, true);
}
if validate {
validate_body(tcx, body, format!("after pass {name}"));

View file

@ -25,7 +25,7 @@ impl<'tcx> MirPass<'tcx> for RemoveUninitDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env(body.source.def_id());
let move_data =
MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
MoveData::gather_moves(body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
let mdpe = MoveDataParamEnv { move_data, param_env };
let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe)

View file

@ -74,7 +74,7 @@ pub fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
impl<'tcx> MirPass<'tcx> for SimplifyCfg {
fn name(&self) -> &'static str {
&self.name()
self.name()
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

View file

@ -167,7 +167,7 @@ impl<'tcx> ReplacementMap<'tcx> {
};
let fields = self.fragments[place.local].as_ref()?;
let (_, new_local) = fields[f]?;
Some(Place { local: new_local, projection: tcx.mk_place_elems(&rest) })
Some(Place { local: new_local, projection: tcx.mk_place_elems(rest) })
}
fn place_fragments(

View file

@ -86,7 +86,7 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
continue;
}
let Some(discriminant_ty) = get_switched_on_type(&bb_data, tcx, body) else { continue };
let Some(discriminant_ty) = get_switched_on_type(bb_data, tcx, body) else { continue };
let layout = tcx.layout_of(
tcx.param_env_reveal_all_normalized(body.source.def_id()).and(discriminant_ty),