Auto merge of #10416 - Jarcho:explicit_iter_loop_ext, r=Manishearth

Extend `explicit_iter_loop` and `explicit_into_iter_loop`

fixes #1518

Some included cleanups
* Split `for_loop` test into different files for each lint (partially).
* Move handling of some `into_iter` cases from `explicit_into_iter`.

---

changelog: Enhancement: [`explicit_iter_loop`]: Now also handles types that implement `IntoIterator`.
[#10416](https://github.com/rust-lang/rust-clippy/pull/10416)

changelog: Sugg: [`explicit_into_iter_loop`]: The suggestion now works on mutable references.
[#10416](https://github.com/rust-lang/rust-clippy/pull/10416)
<!-- changelog_checked -->
This commit is contained in:
bors 2023-06-12 05:30:32 +00:00
commit 841f2199e0
41 changed files with 1168 additions and 864 deletions

View file

@ -808,7 +808,7 @@ fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msr
}
fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
for item in items.iter() {
for item in items {
if let NestedMetaItem::MetaItem(meta) = item {
if !meta.has_name(sym::any) && !meta.has_name(sym::all) {
continue;
@ -842,7 +842,7 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
}
fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
for item in items.iter() {
for item in items {
if let NestedMetaItem::MetaItem(meta) = item {
if meta.has_name(sym!(features)) && let Some(val) = meta.value_str() {
span_lint_and_sugg(

View file

@ -161,7 +161,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
let fields_def = &variant.fields;
// Push field type then visit each field expr.
for field in fields.iter() {
for field in *fields {
let bound =
fields_def
.iter()

View file

@ -680,7 +680,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
});
store.register_late_pass(|_| Box::<shadow::Shadow>::default());
store.register_late_pass(|_| Box::new(unit_types::UnitTypes));
store.register_late_pass(|_| Box::new(loops::Loops));
store.register_late_pass(move |_| Box::new(loops::Loops::new(msrv())));
store.register_late_pass(|_| Box::<main_recursion::MainRecursion>::default());
store.register_late_pass(|_| Box::new(lifetimes::Lifetimes));
store.register_late_pass(|_| Box::new(entry::HashMapPass));

View file

@ -562,7 +562,7 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_
// if the bounds define new lifetimes, they are fine to occur
let allowed_lts = allowed_lts_from(pred.bound_generic_params);
// now walk the bounds
for bound in pred.bounds.iter() {
for bound in pred.bounds {
walk_param_bound(&mut visitor, bound);
}
// and check that all lifetimes are allowed

View file

@ -5,15 +5,76 @@ use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
use rustc_span::symbol::sym;
#[derive(Clone, Copy)]
enum AdjustKind {
None,
Borrow,
BorrowMut,
Reborrow,
ReborrowMut,
}
impl AdjustKind {
fn borrow(mutbl: AutoBorrowMutability) -> Self {
match mutbl {
AutoBorrowMutability::Not => Self::Borrow,
AutoBorrowMutability::Mut { .. } => Self::BorrowMut,
}
}
fn reborrow(mutbl: AutoBorrowMutability) -> Self {
match mutbl {
AutoBorrowMutability::Not => Self::Reborrow,
AutoBorrowMutability::Mut { .. } => Self::ReborrowMut,
}
}
fn display(self) -> &'static str {
match self {
Self::None => "",
Self::Borrow => "&",
Self::BorrowMut => "&mut ",
Self::Reborrow => "&*",
Self::ReborrowMut => "&mut *",
}
}
}
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<'_>) {
let self_ty = cx.typeck_results().expr_ty(self_arg);
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
if !(self_ty == self_ty_adjusted && is_trait_method(cx, call_expr, sym::IntoIterator)) {
if !is_trait_method(cx, call_expr, sym::IntoIterator) {
return;
}
let typeck = cx.typeck_results();
let self_ty = typeck.expr_ty(self_arg);
let adjust = match typeck.expr_adjustments(self_arg) {
[] => AdjustKind::None,
&[
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
..
},
] => AdjustKind::borrow(mutbl),
&[
Adjustment {
kind: Adjust::Deref(_), ..
},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
target,
},
] => {
if self_ty == target && matches!(mutbl, AutoBorrowMutability::Not) {
AdjustKind::None
} else {
AdjustKind::reborrow(mutbl)
}
},
_ => return,
};
let mut applicability = Applicability::MachineApplicable;
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
span_lint_and_sugg(
@ -23,7 +84,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
"it is more concise to loop over containers instead of using explicit \
iteration methods",
"to write this more concisely, try",
object.to_string(),
format!("{}{object}", adjust.display()),
applicability,
);
}

View file

@ -1,75 +1,235 @@
use super::EXPLICIT_ITER_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_trait_method;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::ty::{
implements_trait, implements_trait_with_env, is_copy, make_normalized_projection,
make_normalized_projection_with_regions, normalize_with_regions,
};
use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
use rustc_middle::ty::{self, EarlyBinder, Ty, TypeAndMut};
use rustc_span::sym;
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, arg: &Expr<'_>, method_name: &str) {
let should_lint = match method_name {
"iter" | "iter_mut" => is_ref_iterable_type(cx, self_arg),
"into_iter" if is_trait_method(cx, arg, sym::IntoIterator) => {
let receiver_ty = cx.typeck_results().expr_ty(self_arg);
let receiver_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
let ref_receiver_ty = cx.tcx.mk_ref(
cx.tcx.lifetimes.re_erased,
ty::TypeAndMut {
ty: receiver_ty,
mutbl: Mutability::Not,
},
);
receiver_ty_adjusted == ref_receiver_ty
},
_ => false,
};
if !should_lint {
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<'_>, msrv: &Msrv) {
let Some((adjust, ty)) = is_ref_iterable(cx, self_arg, call_expr) else {
return;
};
if let ty::Array(_, count) = *ty.peel_refs().kind() {
if !ty.is_ref() {
if !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) {
return;
}
} else if count
.try_eval_target_usize(cx.tcx, cx.param_env)
.map_or(true, |x| x > 32)
&& !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN)
{
return;
}
}
let mut applicability = Applicability::MachineApplicable;
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
let muta = if method_name == "iter_mut" { "mut " } else { "" };
span_lint_and_sugg(
cx,
EXPLICIT_ITER_LOOP,
arg.span,
call_expr.span,
"it is more concise to loop over references to containers instead of using explicit \
iteration methods",
"to write this more concisely, try",
format!("&{muta}{object}"),
format!("{}{object}", adjust.display()),
applicability,
);
}
/// Returns `true` if the type of expr is one that provides `IntoIterator` impls
/// for `&T` and `&mut T`, such as `Vec`.
#[rustfmt::skip]
fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
// no walk_ptrs_ty: calling iter() on a reference can make sense because it
// will allow further borrows afterwards
let ty = cx.typeck_results().expr_ty(e);
is_iterable_array(ty, cx) ||
is_type_diagnostic_item(cx, ty, sym::Vec) ||
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
is_type_diagnostic_item(cx, ty, sym::HashMap) ||
is_type_diagnostic_item(cx, ty, sym::HashSet) ||
is_type_diagnostic_item(cx, ty, sym::VecDeque) ||
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||
is_type_diagnostic_item(cx, ty, sym::BTreeMap) ||
is_type_diagnostic_item(cx, ty, sym::BTreeSet)
#[derive(Clone, Copy)]
enum AdjustKind {
None,
Borrow,
BorrowMut,
Deref,
Reborrow,
ReborrowMut,
}
impl AdjustKind {
fn borrow(mutbl: Mutability) -> Self {
match mutbl {
Mutability::Not => Self::Borrow,
Mutability::Mut => Self::BorrowMut,
}
}
fn is_iterable_array<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
// IntoIterator is currently only implemented for array sizes <= 32 in rustc
match ty.kind() {
ty::Array(_, n) => n
.try_eval_target_usize(cx.tcx, cx.param_env)
.map_or(false, |val| (0..=32).contains(&val)),
_ => false,
fn auto_borrow(mutbl: AutoBorrowMutability) -> Self {
match mutbl {
AutoBorrowMutability::Not => Self::Borrow,
AutoBorrowMutability::Mut { .. } => Self::BorrowMut,
}
}
fn reborrow(mutbl: Mutability) -> Self {
match mutbl {
Mutability::Not => Self::Reborrow,
Mutability::Mut => Self::ReborrowMut,
}
}
fn auto_reborrow(mutbl: AutoBorrowMutability) -> Self {
match mutbl {
AutoBorrowMutability::Not => Self::Reborrow,
AutoBorrowMutability::Mut { .. } => Self::ReborrowMut,
}
}
fn display(self) -> &'static str {
match self {
Self::None => "",
Self::Borrow => "&",
Self::BorrowMut => "&mut ",
Self::Deref => "*",
Self::Reborrow => "&*",
Self::ReborrowMut => "&mut *",
}
}
}
/// Checks if an `iter` or `iter_mut` call returns `IntoIterator::IntoIter`. Returns how the
/// argument needs to be adjusted.
#[expect(clippy::too_many_lines)]
fn is_ref_iterable<'tcx>(
cx: &LateContext<'tcx>,
self_arg: &Expr<'_>,
call_expr: &Expr<'_>,
) -> Option<(AdjustKind, Ty<'tcx>)> {
let typeck = cx.typeck_results();
if let Some(trait_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator)
&& let Some(fn_id) = typeck.type_dependent_def_id(call_expr.hir_id)
&& let sig = cx.tcx.liberate_late_bound_regions(fn_id, cx.tcx.fn_sig(fn_id).skip_binder())
&& let &[req_self_ty, req_res_ty] = &**sig.inputs_and_output
&& let param_env = cx.tcx.param_env(fn_id)
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, [])
&& let Some(into_iter_ty) =
make_normalized_projection_with_regions(cx.tcx, param_env, trait_id, sym!(IntoIter), [req_self_ty])
&& let req_res_ty = normalize_with_regions(cx.tcx, param_env, req_res_ty)
&& into_iter_ty == req_res_ty
{
let adjustments = typeck.expr_adjustments(self_arg);
let self_ty = typeck.expr_ty(self_arg);
let self_is_copy = is_copy(cx, self_ty);
if adjustments.is_empty() && self_is_copy {
// Exact type match, already checked earlier
return Some((AdjustKind::None, self_ty));
}
let res_ty = cx.tcx.erase_regions(EarlyBinder::bind(req_res_ty)
.subst(cx.tcx, typeck.node_substs(call_expr.hir_id)));
let mutbl = if let ty::Ref(_, _, mutbl) = *req_self_ty.kind() {
Some(mutbl)
} else {
None
};
if !adjustments.is_empty() {
if self_is_copy {
// Using by value won't consume anything
if implements_trait(cx, self_ty, trait_id, &[])
&& let Some(ty) =
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
&& ty == res_ty
{
return Some((AdjustKind::None, self_ty));
}
} else if let ty::Ref(region, ty, Mutability::Mut) = *self_ty.kind()
&& let Some(mutbl) = mutbl
{
// Attempt to reborrow the mutable reference
let self_ty = if mutbl.is_mut() {
self_ty
} else {
cx.tcx.mk_ref(region, TypeAndMut { ty, mutbl })
};
if implements_trait(cx, self_ty, trait_id, &[])
&& let Some(ty) =
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
&& ty == res_ty
{
return Some((AdjustKind::reborrow(mutbl), self_ty));
}
}
}
if let Some(mutbl) = mutbl
&& !self_ty.is_ref()
{
// Attempt to borrow
let self_ty = cx.tcx.mk_ref(cx.tcx.lifetimes.re_erased, TypeAndMut {
ty: self_ty,
mutbl,
});
if implements_trait(cx, self_ty, trait_id, &[])
&& let Some(ty) = make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
&& ty == res_ty
{
return Some((AdjustKind::borrow(mutbl), self_ty));
}
}
match adjustments {
[] => Some((AdjustKind::None, self_ty)),
&[
Adjustment { kind: Adjust::Deref(_), ..},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
target,
},
..
] => {
if target != self_ty
&& implements_trait(cx, target, trait_id, &[])
&& let Some(ty) =
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
&& ty == res_ty
{
Some((AdjustKind::auto_reborrow(mutbl), target))
} else {
None
}
}
&[Adjustment { kind: Adjust::Deref(_), target }, ..] => {
if is_copy(cx, target)
&& implements_trait(cx, target, trait_id, &[])
&& let Some(ty) =
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
&& ty == res_ty
{
Some((AdjustKind::Deref, target))
} else {
None
}
}
&[
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
target,
},
..
] => {
if self_ty.is_ref()
&& implements_trait(cx, target, trait_id, &[])
&& let Some(ty) =
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
&& ty == res_ty
{
Some((AdjustKind::auto_borrow(mutbl), target))
} else {
None
}
}
_ => None,
}
} else {
None
}
}

View file

@ -20,9 +20,10 @@ mod while_let_loop;
mod while_let_on_iterator;
use clippy_utils::higher;
use clippy_utils::msrvs::Msrv;
use rustc_hir::{Expr, ExprKind, LoopSource, Pat};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span;
use utils::{make_iterator_snippet, IncrementVisitor, InitializeVisitor};
@ -606,7 +607,15 @@ declare_clippy_lint! {
"checking for emptiness of a `Vec` in the loop condition and popping an element in the body"
}
declare_lint_pass!(Loops => [
pub struct Loops {
msrv: Msrv,
}
impl Loops {
pub fn new(msrv: Msrv) -> Self {
Self { msrv }
}
}
impl_lint_pass!(Loops => [
MANUAL_MEMCPY,
MANUAL_FLATTEN,
NEEDLESS_RANGE_LOOP,
@ -645,7 +654,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
if body.span.from_expansion() {
return;
}
check_for_loop(cx, pat, arg, body, expr, span);
self.check_for_loop(cx, pat, arg, body, expr, span);
if let ExprKind::Block(block, _) = body.kind {
never_loop::check(cx, block, loop_id, span, for_loop.as_ref());
}
@ -678,40 +687,41 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
manual_while_let_some::check(cx, condition, body, span);
}
}
extract_msrv_attr!(LateContext);
}
fn check_for_loop<'tcx>(
impl Loops {
fn check_for_loop<'tcx>(
&self,
cx: &LateContext<'tcx>,
pat: &'tcx Pat<'_>,
arg: &'tcx Expr<'_>,
body: &'tcx Expr<'_>,
expr: &'tcx Expr<'_>,
span: Span,
) {
) {
let is_manual_memcpy_triggered = manual_memcpy::check(cx, pat, arg, body, expr);
if !is_manual_memcpy_triggered {
needless_range_loop::check(cx, pat, arg, body, expr);
explicit_counter_loop::check(cx, pat, arg, body, expr);
}
check_for_loop_arg(cx, pat, arg);
self.check_for_loop_arg(cx, pat, arg);
for_kv_map::check(cx, pat, arg, body);
mut_range_bound::check(cx, arg, body);
single_element_loop::check(cx, pat, arg, body, expr);
same_item_push::check(cx, pat, arg, body, expr);
manual_flatten::check(cx, pat, arg, body, span);
manual_find::check(cx, pat, arg, body, span, expr);
}
}
fn check_for_loop_arg(cx: &LateContext<'_>, _: &Pat<'_>, arg: &Expr<'_>) {
fn check_for_loop_arg(&self, cx: &LateContext<'_>, _: &Pat<'_>, arg: &Expr<'_>) {
if let ExprKind::MethodCall(method, self_arg, [], _) = arg.kind {
let method_name = method.ident.as_str();
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
match method_name {
match method.ident.as_str() {
"iter" | "iter_mut" => {
explicit_iter_loop::check(cx, self_arg, arg, method_name);
explicit_iter_loop::check(cx, self_arg, arg, &self.msrv);
},
"into_iter" => {
explicit_iter_loop::check(cx, self_arg, arg, method_name);
explicit_into_iter_loop::check(cx, self_arg, arg);
},
"next" => {
@ -720,4 +730,5 @@ fn check_for_loop_arg(cx: &LateContext<'_>, _: &Pat<'_>, arg: &Expr<'_>) {
_ => {},
}
}
}
}

View file

@ -148,7 +148,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SameItemPushVisitor<'a, 'tcx> {
}
fn visit_block(&mut self, b: &'tcx Block<'_>) {
for stmt in b.stmts.iter() {
for stmt in b.stmts {
self.visit_stmt(stmt);
}
}

View file

@ -101,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
});
if !id.is_local();
then {
for kid in cx.tcx.module_children(id).iter() {
for kid in cx.tcx.module_children(id) {
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
let span = mac_attr.span;
let def_path = cx.tcx.def_path_str(mac_id);

View file

@ -25,7 +25,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'
let mut ident_bind_name = kw::Underscore;
if !matching_wild {
// Looking for unused bindings (i.e.: `_e`)
for pat in inner.iter() {
for pat in inner {
if let PatKind::Binding(_, id, ident, None) = pat.kind {
if ident.as_str().starts_with('_') && !is_local_used(cx, arm.body, id) {
ident_bind_name = ident.name;

View file

@ -140,7 +140,7 @@ impl<'a, 'tcx> SigDropChecker<'a, 'tcx> {
}
}
for generic_arg in b.iter() {
for generic_arg in *b {
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
if self.has_sig_drop_attr(cx, ty) {
return true;

View file

@ -81,7 +81,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
/// Finds function return type by examining return expressions in match arms.
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
for arm in arms.iter() {
for arm in *arms {
if let ExprKind::Ret(Some(ret)) = arm.body.kind {
return Some(cx.typeck_results().expr_ty(ret));
}

View file

@ -13,7 +13,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, suffix: &str, lit_sni
return;
}
let mut seen = (false, false);
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
for ch in &lit_snip.as_bytes()[2..=maybe_last_sep_idx] {
match ch {
b'a'..=b'f' => seen.0 = true,
b'A'..=b'F' => seen.1 = true,

View file

@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeParamMismatch {
then {
// get the name and span of the generic parameters in the Impl
let mut impl_params = Vec::new();
for p in generic_args.args.iter() {
for p in generic_args.args {
match p {
GenericArg::Type(Ty {kind: TyKind::Path(QPath::Resolved(_, path)), ..}) =>
impl_params.push((path.segments[0].ident.to_string(), path.span)),

View file

@ -292,7 +292,7 @@ fn check_final_expr<'tcx>(
// (except for unit type functions) so we don't match it
ExprKind::Match(_, arms, MatchSource::Normal) => {
let match_ty = cx.typeck_results().expr_ty(peeled_drop_expr);
for arm in arms.iter() {
for arm in *arms {
check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit, Some(match_ty));
}
},

View file

@ -333,7 +333,7 @@ impl<'cx, 'sdt, 'tcx> SigDropChecker<'cx, 'sdt, 'tcx> {
return true;
}
}
for generic_arg in b.iter() {
for generic_arg in *b {
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
if self.has_sig_drop_attr(ty) {
return true;

View file

@ -139,7 +139,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
) = cx.tcx.hir().get_if_local(*def_id);
then {
if self_bounds_map.is_empty() {
for bound in self_bounds.iter() {
for bound in *self_bounds {
let Some((self_res, self_segments, _)) = get_trait_info_from_bound(bound) else { continue };
self_bounds_map.insert(self_res, self_segments);
}
@ -184,7 +184,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
// Iterate the bounds and add them to our seen hash
// If we haven't yet seen it, add it to the fixed traits
for bound in bounds.iter() {
for bound in bounds {
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
let new_trait = seen_def_ids.insert(def_id);

View file

@ -75,7 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
for def_id in def_path_def_ids(cx, module) {
for item in cx.tcx.module_children(def_id).iter() {
for item in cx.tcx.module_children(def_id) {
if_chain! {
if let Res::Def(DefKind::Const, item_def_id) = item.res;
let ty = cx.tcx.type_of(item_def_id).subst_identity();

View file

@ -28,7 +28,7 @@ msrv_aliases! {
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS }
1,50,0 { BOOL_THEN, CLAMP }
1,47,0 { TAU, IS_ASCII_DIGIT_CONST }
1,47,0 { TAU, IS_ASCII_DIGIT_CONST, ARRAY_IMPL_ANY_LEN }
1,46,0 { CONST_IF_MATCH }
1,45,0 { STR_STRIP_PREFIX }
1,43,0 { LOG2_10, LOG10_2 }

View file

@ -61,7 +61,7 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
body.local_decls.iter().next().unwrap().source_info.span,
)?;
for bb in body.basic_blocks.iter() {
for bb in &*body.basic_blocks {
check_terminator(tcx, body, bb.terminator(), msrv)?;
for stmt in &bb.statements {
check_statement(tcx, body, def_id, stmt)?;
@ -89,7 +89,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
return Err((span, "function pointers in const fn are unstable".into()));
},
ty::Dynamic(preds, _, _) => {
for pred in preds.iter() {
for pred in *preds {
match pred.skip_binder() {
ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
return Err((

View file

@ -277,7 +277,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
false
},
ty::Dynamic(binder, _, _) => {
for predicate in binder.iter() {
for predicate in *binder {
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
if cx.tcx.has_attr(trait_ref.def_id, sym::must_use) {
return true;
@ -1180,3 +1180,51 @@ pub fn is_interior_mut_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
_ => false,
}
}
pub fn make_normalized_projection_with_regions<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
container_id: DefId,
assoc_ty: Symbol,
substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
) -> Option<Ty<'tcx>> {
fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: AliasTy<'tcx>) -> Option<Ty<'tcx>> {
#[cfg(debug_assertions)]
if let Some((i, subst)) = ty
.substs
.iter()
.enumerate()
.find(|(_, subst)| subst.has_late_bound_regions())
{
debug_assert!(
false,
"substs contain late-bound region at index `{i}` which can't be normalized.\n\
use `TyCtxt::erase_late_bound_regions`\n\
note: subst is `{subst:#?}`",
);
return None;
}
let cause = rustc_middle::traits::ObligationCause::dummy();
match tcx
.infer_ctxt()
.build()
.at(&cause, param_env)
.query_normalize(tcx.mk_projection(ty.def_id, ty.substs))
{
Ok(ty) => Some(ty.value),
Err(e) => {
debug_assert!(false, "failed to normalize type `{ty}`: {e:#?}");
None
},
}
}
helper(tcx, param_env, make_projection(tcx, container_id, assoc_ty, substs)?)
}
pub fn normalize_with_regions<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
let cause = rustc_middle::traits::ObligationCause::dummy();
match tcx.infer_ctxt().build().at(&cause, param_env).query_normalize(ty) {
Ok(ty) => ty.value,
Err(_) => ty,
}
}

View file

@ -82,7 +82,7 @@ pub struct ParamBindingIdCollector {
impl<'tcx> ParamBindingIdCollector {
fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
let mut hir_ids: Vec<hir::HirId> = Vec::new();
for param in body.params.iter() {
for param in body.params {
let mut finder = ParamBindingIdCollector {
binding_hir_ids: Vec::new(),
};

View file

@ -474,7 +474,7 @@ fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
});
} else if let Some(ref versions) = tk.versions {
// if we have multiple versions, save each one
for ver in versions.iter() {
for ver in versions {
crate_sources.push(CrateSource::CratesIo {
name: tk.name.clone(),
version: ver.to_string(),

View file

@ -23,6 +23,54 @@ fn main() {
for _v in vec {
_index += 1;
}
let vec = [1, 2, 3, 4];
// Potential false positives
let mut _index = 0;
_index = 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
_index += 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index = 1;
_index += 1
}
let mut _index = 0;
for _v in &vec {
let mut _index = 0;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index += 1;
_index = 0;
}
let mut _index = 0;
if true {
_index = 1
};
for _v in &vec {
_index += 1
}
let mut _index = 1;
if false {
_index = 0
};
for _v in &vec {
_index += 1
}
}
mod issue_1219 {

View file

@ -25,31 +25,31 @@ LL | for _v in vec {
| ^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()`
error: the variable `count` is used as a loop counter
--> $DIR/explicit_counter_loop.rs:62:9
--> $DIR/explicit_counter_loop.rs:110:9
|
LL | for ch in text.chars() {
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
error: the variable `count` is used as a loop counter
--> $DIR/explicit_counter_loop.rs:73:9
--> $DIR/explicit_counter_loop.rs:121:9
|
LL | for ch in text.chars() {
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
error: the variable `count` is used as a loop counter
--> $DIR/explicit_counter_loop.rs:131:9
--> $DIR/explicit_counter_loop.rs:179:9
|
LL | for _i in 3..10 {
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`
error: the variable `idx_usize` is used as a loop counter
--> $DIR/explicit_counter_loop.rs:171:9
--> $DIR/explicit_counter_loop.rs:219:9
|
LL | for _item in slice {
| ^^^^^^^^^^^^^^^^^^ help: consider using: `for (idx_usize, _item) in slice.iter().enumerate()`
error: the variable `idx_u32` is used as a loop counter
--> $DIR/explicit_counter_loop.rs:183:9
--> $DIR/explicit_counter_loop.rs:231:9
|
LL | for _item in slice {
| ^^^^^^^^^^^^^^^^^^ help: consider using: `for (idx_u32, _item) in (0_u32..).zip(slice.iter())`

View file

@ -0,0 +1,69 @@
//@run-rustfix
#![warn(clippy::explicit_into_iter_loop)]
fn main() {
// Issue #4958
fn _takes_iterator<T>(iterator: &T)
where
for<'a> &'a T: IntoIterator<Item = &'a String>,
{
for _ in iterator {}
}
struct T;
impl IntoIterator for &T {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
let mut t = T;
for _ in &t {}
let r = &t;
for _ in r {}
// No suggestion for this.
// We'd have to suggest `for _ in *rr {}` which is less clear.
let rr = &&t;
for _ in rr.into_iter() {}
let mr = &mut t;
for _ in &*mr {}
struct U;
impl IntoIterator for &mut U {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
let mut u = U;
for _ in &mut u {}
let mr = &mut u;
for _ in &mut *mr {}
// Issue #6900
struct S;
impl S {
#[allow(clippy::should_implement_trait)]
pub fn into_iter<T>(self) -> I<T> {
unimplemented!()
}
}
struct I<T>(T);
impl<T> Iterator for I<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
for _ in S.into_iter::<u32>() {}
}

View file

@ -0,0 +1,69 @@
//@run-rustfix
#![warn(clippy::explicit_into_iter_loop)]
fn main() {
// Issue #4958
fn _takes_iterator<T>(iterator: &T)
where
for<'a> &'a T: IntoIterator<Item = &'a String>,
{
for _ in iterator.into_iter() {}
}
struct T;
impl IntoIterator for &T {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
let mut t = T;
for _ in t.into_iter() {}
let r = &t;
for _ in r.into_iter() {}
// No suggestion for this.
// We'd have to suggest `for _ in *rr {}` which is less clear.
let rr = &&t;
for _ in rr.into_iter() {}
let mr = &mut t;
for _ in mr.into_iter() {}
struct U;
impl IntoIterator for &mut U {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
let mut u = U;
for _ in u.into_iter() {}
let mr = &mut u;
for _ in mr.into_iter() {}
// Issue #6900
struct S;
impl S {
#[allow(clippy::should_implement_trait)]
pub fn into_iter<T>(self) -> I<T> {
unimplemented!()
}
}
struct I<T>(T);
impl<T> Iterator for I<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
for _ in S.into_iter::<u32>() {}
}

View file

@ -0,0 +1,40 @@
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:10:18
|
LL | for _ in iterator.into_iter() {}
| ^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `iterator`
|
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:23:14
|
LL | for _ in t.into_iter() {}
| ^^^^^^^^^^^^^ help: to write this more concisely, try: `&t`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:26:14
|
LL | for _ in r.into_iter() {}
| ^^^^^^^^^^^^^ help: to write this more concisely, try: `r`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:34:14
|
LL | for _ in mr.into_iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&*mr`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:46:14
|
LL | for _ in u.into_iter() {}
| ^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut u`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/explicit_into_iter_loop.rs:49:14
|
LL | for _ in mr.into_iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut *mr`
error: aborting due to 6 previous errors

View file

@ -0,0 +1,154 @@
//@run-rustfix
#![deny(clippy::explicit_iter_loop)]
#![allow(
clippy::linkedlist,
clippy::similar_names,
clippy::needless_borrow,
clippy::deref_addrof,
dead_code
)]
use core::slice;
use std::collections::*;
fn main() {
let mut vec = vec![1, 2, 3, 4];
for _ in &vec {}
for _ in &mut vec {}
let rvec = &vec;
for _ in rvec {}
let rmvec = &mut vec;
for _ in &*rmvec {}
for _ in &mut *rmvec {}
for _ in &vec {} // these are fine
for _ in &mut vec {} // these are fine
for _ in &[1, 2, 3] {}
for _ in &*(&mut [1, 2, 3]) {}
for _ in &[0; 32] {}
for _ in &[0; 33] {}
let ll: LinkedList<()> = LinkedList::new();
for _ in &ll {}
let rll = &ll;
for _ in rll {}
let vd: VecDeque<()> = VecDeque::new();
for _ in &vd {}
let rvd = &vd;
for _ in rvd {}
let bh: BinaryHeap<()> = BinaryHeap::new();
for _ in &bh {}
let hm: HashMap<(), ()> = HashMap::new();
for _ in &hm {}
let bt: BTreeMap<(), ()> = BTreeMap::new();
for _ in &bt {}
let hs: HashSet<()> = HashSet::new();
for _ in &hs {}
let bs: BTreeSet<()> = BTreeSet::new();
for _ in &bs {}
struct NoIntoIter();
impl NoIntoIter {
fn iter(&self) -> slice::Iter<u8> {
unimplemented!()
}
fn iter_mut(&mut self) -> slice::IterMut<u8> {
unimplemented!()
}
}
let mut x = NoIntoIter();
for _ in x.iter() {} // no error
for _ in x.iter_mut() {} // no error
struct IntoIterDiffTy;
impl IntoIterator for &'_ IntoIterDiffTy {
type Item = &'static ();
type IntoIter = core::slice::Iter<'static, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl IntoIterDiffTy {
fn iter(&self) -> core::slice::Iter<'static, i32> {
unimplemented!()
}
}
let x = IntoIterDiffTy;
for _ in x.iter() {}
struct IntoIterDiffSig;
impl IntoIterator for &'_ IntoIterDiffSig {
type Item = &'static ();
type IntoIter = core::slice::Iter<'static, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl IntoIterDiffSig {
fn iter(&self, _: u32) -> core::slice::Iter<'static, ()> {
unimplemented!()
}
}
let x = IntoIterDiffSig;
for _ in x.iter(0) {}
struct IntoIterDiffLt<'a>(&'a ());
impl<'a> IntoIterator for &'a IntoIterDiffLt<'_> {
type Item = &'a ();
type IntoIter = core::slice::Iter<'a, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl<'a> IntoIterDiffLt<'a> {
fn iter(&self) -> core::slice::Iter<'a, ()> {
unimplemented!()
}
}
let x = IntoIterDiffLt(&());
for _ in x.iter() {}
struct CustomType;
impl<'a> IntoIterator for &'a CustomType {
type Item = &'a u32;
type IntoIter = core::slice::Iter<'a, u32>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl<'a> IntoIterator for &'a mut CustomType {
type Item = &'a mut u32;
type IntoIter = core::slice::IterMut<'a, u32>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl CustomType {
fn iter(&self) -> <&'_ Self as IntoIterator>::IntoIter {
panic!()
}
fn iter_mut(&mut self) -> core::slice::IterMut<'_, u32> {
panic!()
}
}
let mut x = CustomType;
for _ in &x {}
for _ in &mut x {}
let r = &x;
for _ in r {}
}

View file

@ -0,0 +1,154 @@
//@run-rustfix
#![deny(clippy::explicit_iter_loop)]
#![allow(
clippy::linkedlist,
clippy::similar_names,
clippy::needless_borrow,
clippy::deref_addrof,
dead_code
)]
use core::slice;
use std::collections::*;
fn main() {
let mut vec = vec![1, 2, 3, 4];
for _ in vec.iter() {}
for _ in vec.iter_mut() {}
let rvec = &vec;
for _ in rvec.iter() {}
let rmvec = &mut vec;
for _ in rmvec.iter() {}
for _ in rmvec.iter_mut() {}
for _ in &vec {} // these are fine
for _ in &mut vec {} // these are fine
for _ in [1, 2, 3].iter() {}
for _ in (&mut [1, 2, 3]).iter() {}
for _ in [0; 32].iter() {}
for _ in [0; 33].iter() {}
let ll: LinkedList<()> = LinkedList::new();
for _ in ll.iter() {}
let rll = &ll;
for _ in rll.iter() {}
let vd: VecDeque<()> = VecDeque::new();
for _ in vd.iter() {}
let rvd = &vd;
for _ in rvd.iter() {}
let bh: BinaryHeap<()> = BinaryHeap::new();
for _ in bh.iter() {}
let hm: HashMap<(), ()> = HashMap::new();
for _ in hm.iter() {}
let bt: BTreeMap<(), ()> = BTreeMap::new();
for _ in bt.iter() {}
let hs: HashSet<()> = HashSet::new();
for _ in hs.iter() {}
let bs: BTreeSet<()> = BTreeSet::new();
for _ in bs.iter() {}
struct NoIntoIter();
impl NoIntoIter {
fn iter(&self) -> slice::Iter<u8> {
unimplemented!()
}
fn iter_mut(&mut self) -> slice::IterMut<u8> {
unimplemented!()
}
}
let mut x = NoIntoIter();
for _ in x.iter() {} // no error
for _ in x.iter_mut() {} // no error
struct IntoIterDiffTy;
impl IntoIterator for &'_ IntoIterDiffTy {
type Item = &'static ();
type IntoIter = core::slice::Iter<'static, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl IntoIterDiffTy {
fn iter(&self) -> core::slice::Iter<'static, i32> {
unimplemented!()
}
}
let x = IntoIterDiffTy;
for _ in x.iter() {}
struct IntoIterDiffSig;
impl IntoIterator for &'_ IntoIterDiffSig {
type Item = &'static ();
type IntoIter = core::slice::Iter<'static, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl IntoIterDiffSig {
fn iter(&self, _: u32) -> core::slice::Iter<'static, ()> {
unimplemented!()
}
}
let x = IntoIterDiffSig;
for _ in x.iter(0) {}
struct IntoIterDiffLt<'a>(&'a ());
impl<'a> IntoIterator for &'a IntoIterDiffLt<'_> {
type Item = &'a ();
type IntoIter = core::slice::Iter<'a, ()>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl<'a> IntoIterDiffLt<'a> {
fn iter(&self) -> core::slice::Iter<'a, ()> {
unimplemented!()
}
}
let x = IntoIterDiffLt(&());
for _ in x.iter() {}
struct CustomType;
impl<'a> IntoIterator for &'a CustomType {
type Item = &'a u32;
type IntoIter = core::slice::Iter<'a, u32>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl<'a> IntoIterator for &'a mut CustomType {
type Item = &'a mut u32;
type IntoIter = core::slice::IterMut<'a, u32>;
fn into_iter(self) -> Self::IntoIter {
unimplemented!()
}
}
impl CustomType {
fn iter(&self) -> <&'_ Self as IntoIterator>::IntoIter {
panic!()
}
fn iter_mut(&mut self) -> core::slice::IterMut<'_, u32> {
panic!()
}
}
let mut x = CustomType;
for _ in x.iter() {}
for _ in x.iter_mut() {}
let r = &x;
for _ in r.iter() {}
}

View file

@ -0,0 +1,142 @@
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:17:14
|
LL | for _ in vec.iter() {}
| ^^^^^^^^^^ help: to write this more concisely, try: `&vec`
|
note: the lint level is defined here
--> $DIR/explicit_iter_loop.rs:2:9
|
LL | #![deny(clippy::explicit_iter_loop)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:18:14
|
LL | for _ in vec.iter_mut() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:21:14
|
LL | for _ in rvec.iter() {}
| ^^^^^^^^^^^ help: to write this more concisely, try: `rvec`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:24:14
|
LL | for _ in rmvec.iter() {}
| ^^^^^^^^^^^^ help: to write this more concisely, try: `&*rmvec`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:25:14
|
LL | for _ in rmvec.iter_mut() {}
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut *rmvec`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:30:14
|
LL | for _ in [1, 2, 3].iter() {}
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:32:14
|
LL | for _ in (&mut [1, 2, 3]).iter() {}
| ^^^^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&*(&mut [1, 2, 3])`
error: the method `iter` doesn't need a mutable reference
--> $DIR/explicit_iter_loop.rs:32:14
|
LL | for _ in (&mut [1, 2, 3]).iter() {}
| ^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:34:14
|
LL | for _ in [0; 32].iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:35:14
|
LL | for _ in [0; 33].iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 33]`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:38:14
|
LL | for _ in ll.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:40:14
|
LL | for _ in rll.iter() {}
| ^^^^^^^^^^ help: to write this more concisely, try: `rll`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:43:14
|
LL | for _ in vd.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:45:14
|
LL | for _ in rvd.iter() {}
| ^^^^^^^^^^ help: to write this more concisely, try: `rvd`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:48:14
|
LL | for _ in bh.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:51:14
|
LL | for _ in hm.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:54:14
|
LL | for _ in bt.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:57:14
|
LL | for _ in hs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:60:14
|
LL | for _ in bs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:149:14
|
LL | for _ in x.iter() {}
| ^^^^^^^^ help: to write this more concisely, try: `&x`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:150:14
|
LL | for _ in x.iter_mut() {}
| ^^^^^^^^^^^^ help: to write this more concisely, try: `&mut x`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/explicit_iter_loop.rs:153:14
|
LL | for _ in r.iter() {}
| ^^^^^^^^ help: to write this more concisely, try: `r`
error: aborting due to 22 previous errors

View file

@ -1,309 +0,0 @@
//@run-rustfix
#![allow(dead_code, unused)]
#![allow(clippy::uninlined_format_args, clippy::useless_vec)]
use std::collections::*;
#[warn(clippy::all)]
struct Unrelated(Vec<u8>);
impl Unrelated {
fn next(&self) -> std::slice::Iter<u8> {
self.0.iter()
}
fn iter(&self) -> std::slice::Iter<u8> {
self.0.iter()
}
}
#[warn(
clippy::needless_range_loop,
clippy::explicit_iter_loop,
clippy::explicit_into_iter_loop,
clippy::iter_next_loop,
clippy::for_kv_map
)]
#[allow(
clippy::linkedlist,
clippy::unnecessary_mut_passed,
clippy::similar_names,
clippy::needless_borrow
)]
#[allow(unused_variables)]
fn main() {
let mut vec = vec![1, 2, 3, 4];
// See #601
for i in 0..10 {
// no error, id_col does not exist outside the loop
let mut id_col = vec![0f64; 10];
id_col[i] = 1f64;
}
for _v in &vec {}
for _v in &mut vec {}
let out_vec = vec![1, 2, 3];
for _v in out_vec {}
for _v in &vec {} // these are fine
for _v in &mut vec {} // these are fine
for _v in &[1, 2, 3] {}
for _v in (&mut [1, 2, 3]).iter() {} // no error
for _v in &[0; 32] {}
for _v in [0; 33].iter() {} // no error
let ll: LinkedList<()> = LinkedList::new();
for _v in &ll {}
let vd: VecDeque<()> = VecDeque::new();
for _v in &vd {}
let bh: BinaryHeap<()> = BinaryHeap::new();
for _v in &bh {}
let hm: HashMap<(), ()> = HashMap::new();
for _v in &hm {}
let bt: BTreeMap<(), ()> = BTreeMap::new();
for _v in &bt {}
let hs: HashSet<()> = HashSet::new();
for _v in &hs {}
let bs: BTreeSet<()> = BTreeSet::new();
for _v in &bs {}
let u = Unrelated(vec![]);
for _v in u.next() {} // no error
for _v in u.iter() {} // no error
let mut out = vec![];
vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
// Loop with explicit counter variable
// Potential false positives
let mut _index = 0;
_index = 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
_index += 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
if true {
_index = 1
}
for _v in &vec {
_index += 1
}
let mut _index = 0;
let mut _index = 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index += 1;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index *= 2;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index = 1;
_index += 1
}
let mut _index = 0;
for _v in &vec {
let mut _index = 0;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index += 1;
_index = 0;
}
let mut _index = 0;
for _v in &vec {
for _x in 0..1 {
_index += 1;
}
_index += 1
}
let mut _index = 0;
for x in &vec {
if *x == 1 {
_index += 1
}
}
let mut _index = 0;
if true {
_index = 1
};
for _v in &vec {
_index += 1
}
let mut _index = 1;
if false {
_index = 0
};
for _v in &vec {
_index += 1
}
let mut index = 0;
{
let mut _x = &mut index;
}
for _v in &vec {
_index += 1
}
let mut index = 0;
for _v in &vec {
index += 1
}
println!("index: {}", index);
fn f<T>(_: &T, _: &T) -> bool {
unimplemented!()
}
fn g<T>(_: &mut [T], _: usize, _: usize) {
unimplemented!()
}
for i in 1..vec.len() {
if f(&vec[i - 1], &vec[i]) {
g(&mut vec, i - 1, i);
}
}
for mid in 1..vec.len() {
let (_, _) = vec.split_at(mid);
}
}
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
let pivot = v.len() - 1;
let mut i = 0;
for j in 0..pivot {
if v[j] <= v[pivot] {
v.swap(i, j);
i += 1;
}
}
v.swap(i, pivot);
i
}
#[warn(clippy::needless_range_loop)]
pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
// Same source and destination - don't trigger lint
for i in 0..dst.len() {
dst[d + i] = dst[s + i];
}
}
mod issue_2496 {
pub trait Handle {
fn new_for_index(index: usize) -> Self;
fn index(&self) -> usize;
}
pub fn test<H: Handle>() -> H {
for x in 0..5 {
let next_handle = H::new_for_index(x);
println!("{}", next_handle.index());
}
unimplemented!()
}
}
// explicit_into_iter_loop bad suggestions
#[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
mod issue_4958 {
fn takes_iterator<T>(iterator: &T)
where
for<'a> &'a T: IntoIterator<Item = &'a String>,
{
for i in iterator {
println!("{}", i);
}
}
struct T;
impl IntoIterator for &T {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
vec![].into_iter()
}
}
fn more_tests() {
let t = T;
let r = &t;
let rr = &&t;
// This case is handled by `explicit_iter_loop`. No idea why.
for _ in &t {}
for _ in r {}
// No suggestion for this.
// We'd have to suggest `for _ in *rr {}` which is less clear.
for _ in rr.into_iter() {}
}
}
// explicit_into_iter_loop
#[warn(clippy::explicit_into_iter_loop)]
mod issue_6900 {
struct S;
impl S {
#[allow(clippy::should_implement_trait)]
pub fn into_iter<T>(self) -> I<T> {
unimplemented!()
}
}
struct I<T>(T);
impl<T> Iterator for I<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
fn f() {
for _ in S.into_iter::<u32>() {
unimplemented!()
}
}
}

View file

@ -1,309 +0,0 @@
//@run-rustfix
#![allow(dead_code, unused)]
#![allow(clippy::uninlined_format_args, clippy::useless_vec)]
use std::collections::*;
#[warn(clippy::all)]
struct Unrelated(Vec<u8>);
impl Unrelated {
fn next(&self) -> std::slice::Iter<u8> {
self.0.iter()
}
fn iter(&self) -> std::slice::Iter<u8> {
self.0.iter()
}
}
#[warn(
clippy::needless_range_loop,
clippy::explicit_iter_loop,
clippy::explicit_into_iter_loop,
clippy::iter_next_loop,
clippy::for_kv_map
)]
#[allow(
clippy::linkedlist,
clippy::unnecessary_mut_passed,
clippy::similar_names,
clippy::needless_borrow
)]
#[allow(unused_variables)]
fn main() {
let mut vec = vec![1, 2, 3, 4];
// See #601
for i in 0..10 {
// no error, id_col does not exist outside the loop
let mut id_col = vec![0f64; 10];
id_col[i] = 1f64;
}
for _v in vec.iter() {}
for _v in vec.iter_mut() {}
let out_vec = vec![1, 2, 3];
for _v in out_vec.into_iter() {}
for _v in &vec {} // these are fine
for _v in &mut vec {} // these are fine
for _v in [1, 2, 3].iter() {}
for _v in (&mut [1, 2, 3]).iter() {} // no error
for _v in [0; 32].iter() {}
for _v in [0; 33].iter() {} // no error
let ll: LinkedList<()> = LinkedList::new();
for _v in ll.iter() {}
let vd: VecDeque<()> = VecDeque::new();
for _v in vd.iter() {}
let bh: BinaryHeap<()> = BinaryHeap::new();
for _v in bh.iter() {}
let hm: HashMap<(), ()> = HashMap::new();
for _v in hm.iter() {}
let bt: BTreeMap<(), ()> = BTreeMap::new();
for _v in bt.iter() {}
let hs: HashSet<()> = HashSet::new();
for _v in hs.iter() {}
let bs: BTreeSet<()> = BTreeSet::new();
for _v in bs.iter() {}
let u = Unrelated(vec![]);
for _v in u.next() {} // no error
for _v in u.iter() {} // no error
let mut out = vec![];
vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
// Loop with explicit counter variable
// Potential false positives
let mut _index = 0;
_index = 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
_index += 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
if true {
_index = 1
}
for _v in &vec {
_index += 1
}
let mut _index = 0;
let mut _index = 1;
for _v in &vec {
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index += 1;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index *= 2;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index = 1;
_index += 1
}
let mut _index = 0;
for _v in &vec {
let mut _index = 0;
_index += 1
}
let mut _index = 0;
for _v in &vec {
_index += 1;
_index = 0;
}
let mut _index = 0;
for _v in &vec {
for _x in 0..1 {
_index += 1;
}
_index += 1
}
let mut _index = 0;
for x in &vec {
if *x == 1 {
_index += 1
}
}
let mut _index = 0;
if true {
_index = 1
};
for _v in &vec {
_index += 1
}
let mut _index = 1;
if false {
_index = 0
};
for _v in &vec {
_index += 1
}
let mut index = 0;
{
let mut _x = &mut index;
}
for _v in &vec {
_index += 1
}
let mut index = 0;
for _v in &vec {
index += 1
}
println!("index: {}", index);
fn f<T>(_: &T, _: &T) -> bool {
unimplemented!()
}
fn g<T>(_: &mut [T], _: usize, _: usize) {
unimplemented!()
}
for i in 1..vec.len() {
if f(&vec[i - 1], &vec[i]) {
g(&mut vec, i - 1, i);
}
}
for mid in 1..vec.len() {
let (_, _) = vec.split_at(mid);
}
}
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
let pivot = v.len() - 1;
let mut i = 0;
for j in 0..pivot {
if v[j] <= v[pivot] {
v.swap(i, j);
i += 1;
}
}
v.swap(i, pivot);
i
}
#[warn(clippy::needless_range_loop)]
pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
// Same source and destination - don't trigger lint
for i in 0..dst.len() {
dst[d + i] = dst[s + i];
}
}
mod issue_2496 {
pub trait Handle {
fn new_for_index(index: usize) -> Self;
fn index(&self) -> usize;
}
pub fn test<H: Handle>() -> H {
for x in 0..5 {
let next_handle = H::new_for_index(x);
println!("{}", next_handle.index());
}
unimplemented!()
}
}
// explicit_into_iter_loop bad suggestions
#[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
mod issue_4958 {
fn takes_iterator<T>(iterator: &T)
where
for<'a> &'a T: IntoIterator<Item = &'a String>,
{
for i in iterator.into_iter() {
println!("{}", i);
}
}
struct T;
impl IntoIterator for &T {
type Item = ();
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
vec![].into_iter()
}
}
fn more_tests() {
let t = T;
let r = &t;
let rr = &&t;
// This case is handled by `explicit_iter_loop`. No idea why.
for _ in t.into_iter() {}
for _ in r.into_iter() {}
// No suggestion for this.
// We'd have to suggest `for _ in *rr {}` which is less clear.
for _ in rr.into_iter() {}
}
}
// explicit_into_iter_loop
#[warn(clippy::explicit_into_iter_loop)]
mod issue_6900 {
struct S;
impl S {
#[allow(clippy::should_implement_trait)]
pub fn into_iter<T>(self) -> I<T> {
unimplemented!()
}
}
struct I<T>(T);
impl<T> Iterator for I<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
fn f() {
for _ in S.into_iter::<u32>() {
unimplemented!()
}
}
}

View file

@ -1,96 +0,0 @@
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:43:15
|
LL | for _v in vec.iter() {}
| ^^^^^^^^^^ help: to write this more concisely, try: `&vec`
|
= note: `-D clippy::explicit-iter-loop` implied by `-D warnings`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:45:15
|
LL | for _v in vec.iter_mut() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:48:15
|
LL | for _v in out_vec.into_iter() {}
| ^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `out_vec`
|
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:53:15
|
LL | for _v in [1, 2, 3].iter() {}
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:57:15
|
LL | for _v in [0; 32].iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:62:15
|
LL | for _v in ll.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:65:15
|
LL | for _v in vd.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:68:15
|
LL | for _v in bh.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:71:15
|
LL | for _v in hm.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:74:15
|
LL | for _v in bt.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:77:15
|
LL | for _v in hs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:80:15
|
LL | for _v in bs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:255:18
|
LL | for i in iterator.into_iter() {
| ^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `iterator`
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:275:18
|
LL | for _ in t.into_iter() {}
| ^^^^^^^^^^^^^ help: to write this more concisely, try: `&t`
error: it is more concise to loop over containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:277:18
|
LL | for _ in r.into_iter() {}
| ^^^^^^^^^^^^^ help: to write this more concisely, try: `r`
error: aborting due to 15 previous errors

View file

@ -1,21 +0,0 @@
// Tests from for_loop.rs that don't have suggestions
#[warn(
clippy::needless_range_loop,
clippy::explicit_iter_loop,
clippy::explicit_into_iter_loop,
clippy::iter_next_loop,
clippy::for_kv_map
)]
#[allow(
clippy::linkedlist,
clippy::unnecessary_mut_passed,
clippy::similar_names,
clippy::useless_vec
)]
#[allow(for_loops_over_fallibles)]
fn main() {
let vec = vec![1, 2, 3, 4];
for _v in vec.iter().next() {}
}

View file

@ -1,10 +0,0 @@
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
--> $DIR/for_loop_unfixable.rs:20:15
|
LL | for _v in vec.iter().next() {}
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::iter-next-loop` implied by `-D warnings`
error: aborting due to previous error

View file

@ -0,0 +1,16 @@
#![allow(dead_code, unused, for_loops_over_fallibles)]
#![warn(clippy::iter_next_loop)]
fn main() {
let x = [1, 2, 3, 4];
for _ in vec.iter().next() {}
struct Unrelated(&'static [u8]);
impl Unrelated {
fn next(&self) -> std::slice::Iter<u8> {
self.0.iter()
}
}
let u = Unrelated(&[0]);
for _v in u.next() {} // no error
}

View file

@ -0,0 +1,9 @@
error[E0423]: expected value, found macro `vec`
--> $DIR/iter_next_loop.rs:6:14
|
LL | for _ in vec.iter().next() {}
| ^^^ not a value
error: aborting due to previous error
For more information about this error, try `rustc --explain E0423`.

View file

@ -70,6 +70,11 @@ fn main() {
bar((0..10).collect::<Vec<_>>(), (0..10));
baz((0..10), (), ('a'..='z'))
}
let values = [1, 2, 3, 4];
let mut out = vec![];
values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
let _y = values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
}
fn foo(_: impl IntoIterator<Item = usize>) {}

View file

@ -70,6 +70,11 @@ fn main() {
bar((0..10).collect::<Vec<_>>(), (0..10).collect::<Vec<_>>());
baz((0..10), (), ('a'..='z').collect::<Vec<_>>())
}
let values = [1, 2, 3, 4];
let mut out = vec![];
values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
let _y = values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
}
fn foo(_: impl IntoIterator<Item = usize>) {}

View file

@ -82,6 +82,29 @@ fn main() {
for i in 0..2 {
println!("{}", test[i]);
}
// See #601
for i in 0..10 {
// no error, id_col does not exist outside the loop
let mut id_col = [0f64; 10];
id_col[i] = 1f64;
}
fn f<T>(_: &T, _: &T) -> bool {
unimplemented!()
}
fn g<T>(_: &mut [T], _: usize, _: usize) {
unimplemented!()
}
for i in 1..vec.len() {
if f(&vec[i - 1], &vec[i]) {
g(&mut vec, i - 1, i);
}
}
for mid in 1..vec.len() {
let (_, _) = vec.split_at(mid);
}
}
struct Test {
@ -94,3 +117,38 @@ impl std::ops::Index<usize> for Test {
&self.inner[index]
}
}
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
let pivot = v.len() - 1;
let mut i = 0;
for j in 0..pivot {
if v[j] <= v[pivot] {
v.swap(i, j);
i += 1;
}
}
v.swap(i, pivot);
i
}
pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
// Same source and destination - don't trigger lint
for i in 0..dst.len() {
dst[d + i] = dst[s + i];
}
}
mod issue_2496 {
pub trait Handle {
fn new_for_index(index: usize) -> Self;
fn index(&self) -> usize;
}
pub fn test<H: Handle>() -> H {
for x in 0..5 {
let next_handle = H::new_for_index(x);
println!("{}", next_handle.index());
}
unimplemented!()
}
}