2021-03-12 14:30:50 +00:00
|
|
|
use super::EXPLICIT_INTO_ITER_LOOP;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-07-29 10:16:06 +00:00
|
|
|
use clippy_utils::is_trait_method;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-03-12 14:30:50 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
2021-07-29 10:16:06 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2021-03-12 14:30:50 +00:00
|
|
|
|
2022-01-13 12:18:19 +00:00
|
|
|
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<'_>) {
|
2021-04-22 09:31:13 +00:00
|
|
|
let self_ty = cx.typeck_results().expr_ty(self_arg);
|
|
|
|
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
|
2022-01-25 07:42:52 +00:00
|
|
|
if !(self_ty == self_ty_adjusted && is_trait_method(cx, call_expr, sym::IntoIterator)) {
|
2021-03-12 14:30:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2021-04-22 09:31:13 +00:00
|
|
|
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
|
2021-03-12 14:30:50 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EXPLICIT_INTO_ITER_LOOP,
|
2021-04-22 09:31:13 +00:00
|
|
|
call_expr.span,
|
2021-03-12 14:30:50 +00:00
|
|
|
"it is more concise to loop over containers instead of using explicit \
|
|
|
|
iteration methods",
|
|
|
|
"to write this more concisely, try",
|
|
|
|
object.to_string(),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|