Auto merge of #13288 - kyoto7250:fix-13184, r=y21

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
This commit is contained in:
bors 2024-08-19 20:03:46 +00:00
commit e7d9fcf8b2
4 changed files with 35 additions and 2 deletions

View File

@ -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 }

View File

@ -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;
@ -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) {
@ -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)
@ -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));

View File

@ -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() {}
}

View File

@ -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() {}
}