2020-01-09 07:52:01 +01:00
|
|
|
use crate::{LateContext, LateLintPass, LintContext};
|
2022-06-27 13:35:31 +01:00
|
|
|
use rustc_errors::{fluent, Applicability};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
|
2021-06-15 17:16:21 +02:00
|
|
|
use rustc_session::lint::FutureIncompatibilityReason;
|
|
|
|
use rustc_span::edition::Edition;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2021-05-25 19:15:27 +02:00
|
|
|
use rustc_span::Span;
|
2019-11-01 12:04:18 +01:00
|
|
|
|
|
|
|
declare_lint! {
|
2020-09-08 15:09:57 -07:00
|
|
|
/// The `array_into_iter` lint detects calling `into_iter` on arrays.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
2021-09-20 08:46:26 -04:00
|
|
|
/// ```rust,edition2018
|
2020-09-08 15:09:57 -07:00
|
|
|
/// # #![allow(unused)]
|
|
|
|
/// [1, 2, 3].into_iter().for_each(|n| { *n; });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
2021-05-25 17:12:45 +02:00
|
|
|
/// Since Rust 1.53, arrays implement `IntoIterator`. However, to avoid
|
|
|
|
/// breakage, `array.into_iter()` in Rust 2015 and 2018 code will still
|
|
|
|
/// behave as `(&array).into_iter()`, returning an iterator over
|
|
|
|
/// references, just like in Rust 1.52 and earlier.
|
|
|
|
/// This only applies to the method call syntax `array.into_iter()`, not to
|
|
|
|
/// any other syntax such as `for _ in array` or `IntoIterator::into_iter(array)`.
|
2019-11-01 12:04:18 +01:00
|
|
|
pub ARRAY_INTO_ITER,
|
|
|
|
Warn,
|
2021-05-25 17:12:45 +02:00
|
|
|
"detects calling `into_iter` on arrays in Rust 2015 and 2018",
|
2019-11-01 12:04:18 +01:00
|
|
|
@future_incompatible = FutureIncompatibleInfo {
|
2021-08-09 17:45:01 +02:00
|
|
|
reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>",
|
2021-06-15 17:16:21 +02:00
|
|
|
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2021),
|
2019-11-01 12:04:18 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 19:15:27 +02:00
|
|
|
#[derive(Copy, Clone, Default)]
|
|
|
|
pub struct ArrayIntoIter {
|
|
|
|
for_expr_span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(ArrayIntoIter => [ARRAY_INTO_ITER]);
|
2019-11-01 12:04:18 +01:00
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
|
2021-05-25 19:15:27 +02:00
|
|
|
// Save the span of expressions in `for _ in expr` syntax,
|
|
|
|
// so we can give a better suggestion for those later.
|
|
|
|
if let hir::ExprKind::Match(arg, [_], hir::MatchSource::ForLoopDesugar) = &expr.kind {
|
|
|
|
if let hir::ExprKind::Call(path, [arg]) = &arg.kind {
|
|
|
|
if let hir::ExprKind::Path(hir::QPath::LangItem(
|
|
|
|
hir::LangItem::IntoIterIntoIter,
|
2021-11-16 20:07:23 +00:00
|
|
|
..,
|
2021-05-25 19:15:27 +02:00
|
|
|
)) = &path.kind
|
|
|
|
{
|
|
|
|
self.for_expr_span = arg.span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:04:18 +01:00
|
|
|
// We only care about method call expressions.
|
2021-12-01 11:17:50 -06:00
|
|
|
if let hir::ExprKind::MethodCall(call, args, _) = &expr.kind {
|
2019-11-01 12:04:18 +01:00
|
|
|
if call.ident.name != sym::into_iter {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the method call actually calls the libcore
|
|
|
|
// `IntoIterator::into_iter`.
|
2020-07-17 08:47:04 +00:00
|
|
|
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
|
2019-11-01 12:04:18 +01:00
|
|
|
match cx.tcx.trait_of_item(def_id) {
|
|
|
|
Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2021-08-30 23:41:48 +02:00
|
|
|
// As this is a method call expression, we have at least one argument.
|
2019-11-01 12:04:18 +01:00
|
|
|
let receiver_arg = &args[0];
|
2021-08-30 23:41:48 +02:00
|
|
|
let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
|
|
|
|
let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);
|
2019-11-01 12:04:18 +01:00
|
|
|
|
2021-12-03 03:25:11 +01:00
|
|
|
let Some(Adjustment { kind: Adjust::Borrow(_), target }) = adjustments.last() else {
|
|
|
|
return
|
2021-08-30 23:41:48 +02:00
|
|
|
};
|
2019-12-22 14:26:28 +01:00
|
|
|
|
2021-08-30 23:41:48 +02:00
|
|
|
let types =
|
|
|
|
std::iter::once(receiver_ty).chain(adjustments.iter().map(|adj| adj.target));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-11-01 12:04:18 +01:00
|
|
|
}
|
|
|
|
|
2021-08-30 23:41:48 +02:00
|
|
|
if !found_array {
|
|
|
|
return;
|
2019-11-01 12:04:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit lint diagnostic.
|
2021-08-30 23:41:48 +02:00
|
|
|
let target = match *target.kind() {
|
2020-08-03 00:49:11 +02:00
|
|
|
ty::Ref(_, inner_ty, _) if inner_ty.is_array() => "[T; N]",
|
|
|
|
ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Slice(..)) => "[T]",
|
2019-11-01 12:04:18 +01:00
|
|
|
// We know the original first argument type is an array type,
|
|
|
|
// we know that the first adjustment was an autoref coercion
|
|
|
|
// and we know that `IntoIterator` is the trait involved. The
|
|
|
|
// array cannot be coerced to something other than a reference
|
|
|
|
// to an array or to a slice.
|
|
|
|
_ => bug!("array type coerced to something other than array or slice"),
|
|
|
|
};
|
2021-12-01 11:17:50 -06:00
|
|
|
cx.struct_span_lint(ARRAY_INTO_ITER, call.ident.span, |lint| {
|
2022-06-27 13:35:31 +01:00
|
|
|
let mut diag = lint.build(fluent::lint::array_into_iter);
|
|
|
|
diag.set_arg("target", target);
|
2021-05-25 19:15:27 +02:00
|
|
|
diag.span_suggestion(
|
2019-11-01 12:04:18 +01:00
|
|
|
call.ident.span,
|
2022-06-27 13:35:31 +01:00
|
|
|
fluent::lint::use_iter_suggestion,
|
2022-04-26 06:17:33 +01:00
|
|
|
"iter",
|
2019-11-01 12:04:18 +01:00
|
|
|
Applicability::MachineApplicable,
|
2021-05-25 19:15:27 +02:00
|
|
|
);
|
|
|
|
if self.for_expr_span == expr.span {
|
|
|
|
diag.span_suggestion(
|
2021-10-14 16:19:39 -05:00
|
|
|
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
|
2022-06-27 13:35:31 +01:00
|
|
|
fluent::lint::remove_into_iter_suggestion,
|
2022-06-13 15:48:40 +09:00
|
|
|
"",
|
2021-05-25 19:15:27 +02:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2021-08-30 23:42:13 +02:00
|
|
|
} else if receiver_ty.is_array() {
|
2021-05-25 19:15:27 +02:00
|
|
|
diag.multipart_suggestion(
|
2022-06-27 13:35:31 +01:00
|
|
|
fluent::lint::use_explicit_into_iter_suggestion,
|
2021-05-25 19:15:27 +02:00
|
|
|
vec![
|
|
|
|
(expr.span.shrink_to_lo(), "IntoIterator::into_iter(".into()),
|
2022-06-27 13:35:31 +01:00
|
|
|
(
|
|
|
|
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
|
|
|
|
")".into(),
|
|
|
|
),
|
2021-05-25 19:15:27 +02:00
|
|
|
],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
diag.emit();
|
2020-01-31 22:24:57 +10:00
|
|
|
})
|
2019-11-01 12:04:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|