2020-01-09 00:52:01 -06:00
|
|
|
use crate::{LateContext, LateLintPass, LintContext};
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
|
2021-06-15 10:16:21 -05:00
|
|
|
use rustc_session::lint::FutureIncompatibilityReason;
|
|
|
|
use rustc_span::edition::Edition;
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::sym;
|
2021-05-25 12:15:27 -05:00
|
|
|
use rustc_span::Span;
|
2019-11-01 06:04:18 -05:00
|
|
|
|
|
|
|
declare_lint! {
|
2020-09-08 17:09:57 -05:00
|
|
|
/// The `array_into_iter` lint detects calling `into_iter` on arrays.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #![allow(unused)]
|
|
|
|
/// [1, 2, 3].into_iter().for_each(|n| { *n; });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
2021-05-25 10:12:45 -05: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 06:04:18 -05:00
|
|
|
pub ARRAY_INTO_ITER,
|
|
|
|
Warn,
|
2021-05-25 10:12:45 -05:00
|
|
|
"detects calling `into_iter` on arrays in Rust 2015 and 2018",
|
2019-11-01 06:04:18 -05:00
|
|
|
@future_incompatible = FutureIncompatibleInfo {
|
|
|
|
reference: "issue #66145 <https://github.com/rust-lang/rust/issues/66145>",
|
2021-06-15 10:16:21 -05:00
|
|
|
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2021),
|
2019-11-01 06:04:18 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:15:27 -05:00
|
|
|
#[derive(Copy, Clone, Default)]
|
|
|
|
pub struct ArrayIntoIter {
|
|
|
|
for_expr_span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(ArrayIntoIter => [ARRAY_INTO_ITER]);
|
2019-11-01 06:04:18 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
|
2021-05-25 12:15:27 -05: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,
|
|
|
|
_,
|
|
|
|
)) = &path.kind
|
|
|
|
{
|
|
|
|
self.for_expr_span = arg.span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 06:04:18 -05:00
|
|
|
// We only care about method call expressions.
|
2020-06-09 14:34:23 -05:00
|
|
|
if let hir::ExprKind::MethodCall(call, span, args, _) = &expr.kind {
|
2019-11-01 06:04:18 -05:00
|
|
|
if call.ident.name != sym::into_iter {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the method call actually calls the libcore
|
|
|
|
// `IntoIterator::into_iter`.
|
2020-07-17 03:47:04 -05:00
|
|
|
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
|
2019-11-01 06:04:18 -05:00
|
|
|
match cx.tcx.trait_of_item(def_id) {
|
2019-12-22 16:42:04 -06:00
|
|
|
Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {}
|
2019-11-01 06:04:18 -05:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// As this is a method call expression, we have at least one
|
|
|
|
// argument.
|
|
|
|
let receiver_arg = &args[0];
|
|
|
|
|
2019-12-22 07:26:28 -06:00
|
|
|
// Peel all `Box<_>` layers. We have to special case `Box` here as
|
|
|
|
// `Box` is the only thing that values can be moved out of via
|
|
|
|
// method call. `Box::new([1]).into_iter()` should trigger this
|
|
|
|
// lint.
|
2020-07-17 03:47:04 -05:00
|
|
|
let mut recv_ty = cx.typeck_results().expr_ty(receiver_arg);
|
2019-12-22 07:26:28 -06:00
|
|
|
let mut num_box_derefs = 0;
|
|
|
|
while recv_ty.is_box() {
|
|
|
|
num_box_derefs += 1;
|
|
|
|
recv_ty = recv_ty.boxed_ty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we found an array after peeling the boxes.
|
2020-08-02 17:49:11 -05:00
|
|
|
if !matches!(recv_ty.kind(), ty::Array(..)) {
|
2019-12-22 07:26:28 -06:00
|
|
|
return;
|
2019-11-01 06:04:18 -05:00
|
|
|
}
|
|
|
|
|
2019-12-22 07:26:28 -06:00
|
|
|
// Make sure that there is an autoref coercion at the expected
|
|
|
|
// position. The first `num_box_derefs` adjustments are the derefs
|
|
|
|
// of the box.
|
2020-07-17 03:47:04 -05:00
|
|
|
match cx.typeck_results().expr_adjustments(receiver_arg).get(num_box_derefs) {
|
2019-11-01 06:04:18 -05:00
|
|
|
Some(Adjustment { kind: Adjust::Borrow(_), .. }) => {}
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit lint diagnostic.
|
2020-08-02 17:49:11 -05:00
|
|
|
let target = match *cx.typeck_results().expr_ty_adjusted(receiver_arg).kind() {
|
|
|
|
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 06:04:18 -05: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"),
|
|
|
|
};
|
2020-01-31 06:24:57 -06:00
|
|
|
cx.struct_span_lint(ARRAY_INTO_ITER, *span, |lint| {
|
2021-05-25 12:15:27 -05:00
|
|
|
let mut diag = lint.build(&format!(
|
2021-05-25 10:12:45 -05:00
|
|
|
"this method call resolves to `<&{} as IntoIterator>::into_iter` \
|
|
|
|
(due to backwards compatibility), \
|
|
|
|
but will resolve to <{} as IntoIterator>::into_iter in Rust 2021.",
|
|
|
|
target, target,
|
2021-05-25 12:15:27 -05:00
|
|
|
));
|
|
|
|
diag.span_suggestion(
|
2019-11-01 06:04:18 -05:00
|
|
|
call.ident.span,
|
|
|
|
"use `.iter()` instead of `.into_iter()` to avoid ambiguity",
|
|
|
|
"iter".into(),
|
|
|
|
Applicability::MachineApplicable,
|
2021-05-25 12:15:27 -05:00
|
|
|
);
|
|
|
|
if self.for_expr_span == expr.span {
|
|
|
|
let expr_span = expr.span.ctxt().outer_expn_data().call_site;
|
|
|
|
diag.span_suggestion(
|
|
|
|
receiver_arg.span.shrink_to_hi().to(expr_span.shrink_to_hi()),
|
|
|
|
"or remove `.into_iter()` to iterate by value",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
diag.multipart_suggestion(
|
|
|
|
"or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value",
|
|
|
|
vec![
|
|
|
|
(expr.span.shrink_to_lo(), "IntoIterator::into_iter(".into()),
|
|
|
|
(receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()), ")".into()),
|
|
|
|
],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
diag.emit();
|
2020-01-31 06:24:57 -06:00
|
|
|
})
|
2019-11-01 06:04:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|