Upgrade array_into_iter lint to include Deref-to-array types.
This commit is contained in:
parent
13edc17f65
commit
422ad3bec2
1 changed files with 29 additions and 23 deletions
|
@ -74,39 +74,45 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
// As this is a method call expression, we have at least one
|
// As this is a method call expression, we have at least one argument.
|
||||||
// argument.
|
|
||||||
let receiver_arg = &args[0];
|
let receiver_arg = &args[0];
|
||||||
|
let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
|
||||||
|
let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);
|
||||||
|
|
||||||
// Peel all `Box<_>` layers. We have to special case `Box` here as
|
let target = match adjustments.last() {
|
||||||
// `Box` is the only thing that values can be moved out of via
|
Some(Adjustment { kind: Adjust::Borrow(_), target }) => target,
|
||||||
// method call. `Box::new([1]).into_iter()` should trigger this
|
_ => return,
|
||||||
// lint.
|
};
|
||||||
let mut recv_ty = cx.typeck_results().expr_ty(receiver_arg);
|
|
||||||
let mut num_box_derefs = 0;
|
let types =
|
||||||
while recv_ty.is_box() {
|
std::iter::once(receiver_ty).chain(adjustments.iter().map(|adj| adj.target));
|
||||||
num_box_derefs += 1;
|
|
||||||
recv_ty = recv_ty.boxed_ty();
|
let mut found_array = false;
|
||||||
|
|
||||||
|
for ty in types {
|
||||||
|
match ty.kind() {
|
||||||
|
// If we run into a &[T; N] or &[T] first, there's nothing to warn about.
|
||||||
|
// It'll resolve to the reference version.
|
||||||
|
ty::Ref(_, inner_ty, _) if inner_ty.is_array() => return,
|
||||||
|
ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Slice(..)) => return,
|
||||||
|
// Found an actual array type without matching a &[T; N] first.
|
||||||
|
// This is the problematic case.
|
||||||
|
ty::Array(..) => {
|
||||||
|
found_array = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure we found an array after peeling the boxes.
|
if !found_array {
|
||||||
if !matches!(recv_ty.kind(), ty::Array(..)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that there is an autoref coercion at the expected
|
|
||||||
// position. The first `num_box_derefs` adjustments are the derefs
|
|
||||||
// of the box.
|
|
||||||
match cx.typeck_results().expr_adjustments(receiver_arg).get(num_box_derefs) {
|
|
||||||
Some(Adjustment { kind: Adjust::Borrow(_), .. }) => {}
|
|
||||||
_ => return,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emit lint diagnostic.
|
// Emit lint diagnostic.
|
||||||
let target = match *cx.typeck_results().expr_ty_adjusted(receiver_arg).kind() {
|
let target = match *target.kind() {
|
||||||
ty::Ref(_, inner_ty, _) if inner_ty.is_array() => "[T; N]",
|
ty::Ref(_, inner_ty, _) if inner_ty.is_array() => "[T; N]",
|
||||||
ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Slice(..)) => "[T]",
|
ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Slice(..)) => "[T]",
|
||||||
|
|
||||||
// We know the original first argument type is an array type,
|
// We know the original first argument type is an array type,
|
||||||
// we know that the first adjustment was an autoref coercion
|
// we know that the first adjustment was an autoref coercion
|
||||||
// and we know that `IntoIterator` is the trait involved. The
|
// and we know that `IntoIterator` is the trait involved. The
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue