Skip to content
Snippets Groups Projects
Verified Commit 09d02c93 authored by Vincent Lafeychine's avatar Vincent Lafeychine
Browse files

clippy: Fix manual_let_else

parent df66db26
No related branches found
No related tags found
1 merge request!44Fix most of `clippy::pedantic`
......@@ -32,7 +32,6 @@ rustflags = [
"-Aclippy::string_lit_as_bytes",
# clippy::pedantic
"-Aclippy::manual_let_else",
"-Aclippy::manual_string_new",
"-Aclippy::map_unwrap_or",
"-Aclippy::match_same_arms",
......
......@@ -30,9 +30,8 @@ pub trait SplitAggregateAssignment<'tcx> {
impl<'tcx> SplitAggregateAssignment<'tcx> for mir::Statement<'tcx> {
fn split_assignment(self, tcx: ty::TyCtxt<'tcx>, mir: &mir::Body<'tcx>) -> Vec<mir::Statement<'tcx>> {
let (lhs, rhs) = match self.kind {
mir::StatementKind::Assign(box (lhs, rhs)) => (lhs, rhs),
_ => return vec![self],
let mir::StatementKind::Assign(box (lhs, rhs)) = self.kind else {
return vec![self];
};
let atomic_assignments = match rhs {
......
......@@ -350,59 +350,58 @@ fn get_borrowed_places<'a, 'tcx: 'a>(
loan_position: &HashMap<facts::Loan, mir::Location>,
loan: facts::Loan,
) -> Result<Vec<&'a mir::Place<'tcx>>, PoloniusInfoError> {
let location = if let Some(location) = loan_position.get(&loan) {
location
} else {
let Some(location) = loan_position.get(&loan) else {
// FIXME (Vytautas): This is likely to be wrong.
debug!("Not found: {:?}", loan);
return Ok(Vec::new());
};
let mir::BasicBlockData { ref statements, .. } = mir[location.block];
if statements.len() == location.statement_index {
Ok(Vec::new())
} else {
let statement = &statements[location.statement_index];
match statement.kind {
mir::StatementKind::Assign(box (ref _lhs, ref rhs)) => match *rhs {
mir::Rvalue::Use(mir::Operand::Copy(ref place) | mir::Operand::Move(ref place))
| mir::Rvalue::Ref(_, _, ref place)
| mir::Rvalue::Discriminant(ref place) => Ok(vec![place]),
mir::Rvalue::Use(mir::Operand::Constant(_)) => Ok(Vec::new()),
mir::Rvalue::Aggregate(_, ref operands) => Ok(operands
.iter()
.filter_map(|operand| match *operand {
mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => Some(place),
mir::Operand::Constant(_) => None,
})
.collect()),
// slice creation involves an unsize pointer cast like [i32; 3] -> &[i32]
mir::Rvalue::Cast(
mir::CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
ref operand,
ref ty,
) if ty.is_slice() && !ty.is_unsafe_ptr() => {
trace!("slice: operand={:?}, ty={:?}", operand, ty);
Ok(match *operand {
mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => vec![place],
mir::Operand::Constant(_) => vec![],
})
},
return Ok(Vec::new());
}
mir::Rvalue::Cast(..) => {
// all other loan-casts are unsupported
Err(PoloniusInfoError::LoanInUnsupportedStatement(
"cast statements that create loans are not supported".to_string(),
*location,
))
},
let statement = &statements[location.statement_index];
match statement.kind {
mir::StatementKind::Assign(box (ref _lhs, ref rhs)) => match *rhs {
mir::Rvalue::Use(mir::Operand::Copy(ref place) | mir::Operand::Move(ref place))
| mir::Rvalue::Ref(_, _, ref place)
| mir::Rvalue::Discriminant(ref place) => Ok(vec![place]),
mir::Rvalue::Use(mir::Operand::Constant(_)) => Ok(Vec::new()),
mir::Rvalue::Aggregate(_, ref operands) => Ok(operands
.iter()
.filter_map(|operand| match *operand {
mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => Some(place),
mir::Operand::Constant(_) => None,
})
.collect()),
// slice creation involves an unsize pointer cast like [i32; 3] -> &[i32]
mir::Rvalue::Cast(
mir::CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
ref operand,
ref ty,
) if ty.is_slice() && !ty.is_unsafe_ptr() => {
trace!("slice: operand={:?}, ty={:?}", operand, ty);
Ok(match *operand {
mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => vec![place],
mir::Operand::Constant(_) => vec![],
})
},
ref x => unreachable!("{:?}", x),
mir::Rvalue::Cast(..) => {
// all other loan-casts are unsupported
Err(PoloniusInfoError::LoanInUnsupportedStatement(
"cast statements that create loans are not supported".to_string(),
*location,
))
},
ref x => unreachable!("{:?}", x),
}
},
ref x => unreachable!("{:?}", x),
}
}
......@@ -1105,14 +1104,14 @@ impl<'a, 'tcx: 'a> PoloniusInfo<'a, 'tcx> {
// get_assignment_for_loan(L1) would be _3.1 = move _5. Using these atomic assignments, we
// can simply read off the loan places as before.
let assignment = if let Some(loan_assignment) = self.get_assignment_for_loan(*loan)? {
loan_assignment
} else {
let Some(assignment) = self.get_assignment_for_loan(*loan)? else {
return Ok(None);
};
let (dest, source) = assignment.as_assign().unwrap();
let source = source.clone();
let location = self.loan_position[loan];
Ok(Some(LoanPlaces {
dest,
source,
......@@ -1126,17 +1125,15 @@ impl<'a, 'tcx: 'a> PoloniusInfo<'a, 'tcx> {
&self,
loan: facts::Loan,
) -> Result<Option<mir::Statement<'tcx>>, PlaceRegionsError> {
let &location = if let Some(loc) = self.loan_position.get(&loan) {
loc
} else {
let Some(&location) = self.loan_position.get(&loan) else {
return Ok(None);
};
let stmt = if let Some(s) = self.mir.statement_at(location) {
s.clone()
} else {
let Some(stmt) = self.mir.statement_at(location) else {
return Ok(None);
};
let mut assignments: Vec<_> = stmt.split_assignment(self.tcx, self.mir);
let mut assignments: Vec<_> = stmt.clone().split_assignment(self.tcx, self.mir);
// TODO: This is a workaround. It seems like some local variables don't have a local
// variable declaration in the MIR. One example of this can be observed in the
......
......@@ -95,40 +95,35 @@ pub enum FlatType {
impl FlatType {
/// Try to convert a flat type to a type.
pub fn to_type<'tcx>(&self, tcx: ty::TyCtxt<'tcx>) -> Option<ty::Ty<'tcx>> {
match self {
Self::Adt(path_with_args) => {
let (did, flat_args) = path_with_args.to_item(tcx)?;
let ty: ty::EarlyBinder<ty::Ty<'tcx>> = tcx.type_of(did);
let Self::Adt(path_with_args) = self;
let (did, flat_args) = path_with_args.to_item(tcx)?;
let args = if let ty::TyKind::Adt(_, adt_args) = ty.skip_binder().kind() {
adt_args
} else {
return None;
};
// build substitution
let mut substs = Vec::new();
for (ty_arg, flat_arg) in args.iter().zip(flat_args.into_iter()) {
match ty_arg.unpack() {
ty::GenericArgKind::Type(_ty) => {
if let Some(flat_arg) = flat_arg {
substs.push(flat_arg);
}
},
_ => {
substs.push(ty_arg);
},
let ty: ty::EarlyBinder<ty::Ty<'tcx>> = tcx.type_of(did);
let ty::TyKind::Adt(_, args) = ty.skip_binder().kind() else {
return None;
};
// build substitution
let mut substs = Vec::new();
for (ty_arg, flat_arg) in args.iter().zip(flat_args.into_iter()) {
match ty_arg.unpack() {
ty::GenericArgKind::Type(_) => {
if let Some(flat_arg) = flat_arg {
substs.push(flat_arg);
}
}
},
_ => {
substs.push(ty_arg);
},
}
}
// substitute
info!("substituting {:?} with {:?}", ty, substs);
let subst_ty =
if substs.is_empty() { ty.instantiate_identity() } else { ty.instantiate(tcx, &substs) };
// substitute
info!("substituting {:?} with {:?}", ty, substs);
let subst_ty =
if substs.is_empty() { ty.instantiate_identity() } else { ty.instantiate(tcx, &substs) };
Some(subst_ty)
},
}
Some(subst_ty)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment