rustc_mir_transform/
remove_unneeded_drops.rsuse rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use tracing::{debug, trace};
use super::simplify::simplify_cfg;
pub(super) struct RemoveUnneededDrops;
impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("Running RemoveUnneededDrops on {:?}", body.source);
let typing_env = body.typing_env(tcx);
let mut should_simplify = false;
for block in body.basic_blocks.as_mut() {
let terminator = block.terminator_mut();
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
let ty = place.ty(&body.local_decls, tcx);
if ty.ty.needs_drop(tcx, typing_env) {
continue;
}
if !tcx.consider_optimizing(|| {
format!("RemoveUnneededDrops {:?}", body.source.def_id())
}) {
continue;
}
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
should_simplify = true;
}
}
if should_simplify {
simplify_cfg(body);
}
}
}