Skip to content

Commit

Permalink
Auto merge of #13288 - kyoto7250:fix-13184, r=y21
Browse files Browse the repository at this point in the history
fix false positive in explicit_iter_loop with msrv

close #13184

This PR fix false positive in explicit_iter_loop when msrv < 1.80

changelog: fix false positive in explicit_iter_loop when msrv < 1.80
  • Loading branch information
bors committed Aug 19, 2024
2 parents ba6bc81 + 1958e1a commit e7d9fcf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions clippy_config/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! msrv_aliases {
// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,81,0 { LINT_REASONS_STABILIZATION }
1,80,0 { BOX_INTO_ITER}
1,77,0 { C_STR_LITERALS }
1,76,0 { PTR_FROM_REF, OPTION_RESULT_INSPECT }
1,71,0 { TUPLE_ARRAY_CONVERSIONS, BUILD_HASHER_HASH_ONE }
Expand Down
12 changes: 10 additions & 2 deletions clippy_lints/src/loops/explicit_iter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{
implements_trait, implements_trait_with_env, is_copy, make_normalized_projection,
implements_trait, implements_trait_with_env, is_copy, is_type_lang_item, make_normalized_projection,
make_normalized_projection_with_regions, normalize_with_regions,
};
use rustc_errors::Applicability;
Expand All @@ -20,9 +20,10 @@ pub(super) fn check(
msrv: &Msrv,
enforce_iter_loop_reborrow: bool,
) {
let Some((adjust, ty)) = is_ref_iterable(cx, self_arg, call_expr, enforce_iter_loop_reborrow) else {
let Some((adjust, ty)) = is_ref_iterable(cx, self_arg, call_expr, enforce_iter_loop_reborrow, msrv) else {
return;
};

if let ty::Array(_, count) = *ty.peel_refs().kind() {
if !ty.is_ref() {
if !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) {
Expand Down Expand Up @@ -109,6 +110,7 @@ fn is_ref_iterable<'tcx>(
self_arg: &Expr<'_>,
call_expr: &Expr<'_>,
enforce_iter_loop_reborrow: bool,
msrv: &Msrv,
) -> Option<(AdjustKind, Ty<'tcx>)> {
let typeck = cx.typeck_results();
if let Some(trait_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator)
Expand All @@ -128,6 +130,12 @@ fn is_ref_iterable<'tcx>(
let self_ty = typeck.expr_ty(self_arg);
let self_is_copy = is_copy(cx, self_ty);

if !msrv.meets(msrvs::BOX_INTO_ITER)
&& is_type_lang_item(cx, self_ty.peel_refs(), rustc_hir::LangItem::OwnedBox)
{
return None;
}

if adjustments.is_empty() && self_is_copy {
// Exact type match, already checked earlier
return Some((AdjustKind::None, self_ty));
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/explicit_iter_loop.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ fn main() {
let r = &x;
for _ in r {}
}

#[clippy::msrv = "1.79"]
pub fn issue_13184() {
// https://github.com/rust-lang/rust-clippy/issues/13184
// No need to fix, as IntoIterator for Box is valid starting from 1.80
let mut values: Box<[u32]> = Box::new([1, 2]);
for _ in values.iter() {}
for _ in values.iter_mut() {}

let rvalues = &values;
for _ in rvalues.iter() {}
}
12 changes: 12 additions & 0 deletions tests/ui/explicit_iter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ fn main() {
let r = &x;
for _ in r.iter() {}
}

#[clippy::msrv = "1.79"]
pub fn issue_13184() {
// https://github.com/rust-lang/rust-clippy/issues/13184
// No need to fix, as IntoIterator for Box is valid starting from 1.80
let mut values: Box<[u32]> = Box::new([1, 2]);
for _ in values.iter() {}
for _ in values.iter_mut() {}

let rvalues = &values;
for _ in rvalues.iter() {}
}

0 comments on commit e7d9fcf

Please sign in to comment.