Skip to content

Commit e7d9fcf

Browse files
committed
Auto merge of rust-lang#13288 - kyoto7250:fix-13184, r=y21
fix false positive in explicit_iter_loop with msrv close rust-lang#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
2 parents ba6bc81 + 1958e1a commit e7d9fcf

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

clippy_config/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ macro_rules! msrv_aliases {
1818
// names may refer to stabilized feature flags or library items
1919
msrv_aliases! {
2020
1,81,0 { LINT_REASONS_STABILIZATION }
21+
1,80,0 { BOX_INTO_ITER}
2122
1,77,0 { C_STR_LITERALS }
2223
1,76,0 { PTR_FROM_REF, OPTION_RESULT_INSPECT }
2324
1,71,0 { TUPLE_ARRAY_CONVERSIONS, BUILD_HASHER_HASH_ONE }

clippy_lints/src/loops/explicit_iter_loop.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_config::msrvs::{self, Msrv};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::{
6-
implements_trait, implements_trait_with_env, is_copy, make_normalized_projection,
6+
implements_trait, implements_trait_with_env, is_copy, is_type_lang_item, make_normalized_projection,
77
make_normalized_projection_with_regions, normalize_with_regions,
88
};
99
use rustc_errors::Applicability;
@@ -20,9 +20,10 @@ pub(super) fn check(
2020
msrv: &Msrv,
2121
enforce_iter_loop_reborrow: bool,
2222
) {
23-
let Some((adjust, ty)) = is_ref_iterable(cx, self_arg, call_expr, enforce_iter_loop_reborrow) else {
23+
let Some((adjust, ty)) = is_ref_iterable(cx, self_arg, call_expr, enforce_iter_loop_reborrow, msrv) else {
2424
return;
2525
};
26+
2627
if let ty::Array(_, count) = *ty.peel_refs().kind() {
2728
if !ty.is_ref() {
2829
if !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) {
@@ -109,6 +110,7 @@ fn is_ref_iterable<'tcx>(
109110
self_arg: &Expr<'_>,
110111
call_expr: &Expr<'_>,
111112
enforce_iter_loop_reborrow: bool,
113+
msrv: &Msrv,
112114
) -> Option<(AdjustKind, Ty<'tcx>)> {
113115
let typeck = cx.typeck_results();
114116
if let Some(trait_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator)
@@ -128,6 +130,12 @@ fn is_ref_iterable<'tcx>(
128130
let self_ty = typeck.expr_ty(self_arg);
129131
let self_is_copy = is_copy(cx, self_ty);
130132

133+
if !msrv.meets(msrvs::BOX_INTO_ITER)
134+
&& is_type_lang_item(cx, self_ty.peel_refs(), rustc_hir::LangItem::OwnedBox)
135+
{
136+
return None;
137+
}
138+
131139
if adjustments.is_empty() && self_is_copy {
132140
// Exact type match, already checked earlier
133141
return Some((AdjustKind::None, self_ty));

tests/ui/explicit_iter_loop.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,15 @@ fn main() {
153153
let r = &x;
154154
for _ in r {}
155155
}
156+
157+
#[clippy::msrv = "1.79"]
158+
pub fn issue_13184() {
159+
// https://github.com/rust-lang/rust-clippy/issues/13184
160+
// No need to fix, as IntoIterator for Box is valid starting from 1.80
161+
let mut values: Box<[u32]> = Box::new([1, 2]);
162+
for _ in values.iter() {}
163+
for _ in values.iter_mut() {}
164+
165+
let rvalues = &values;
166+
for _ in rvalues.iter() {}
167+
}

tests/ui/explicit_iter_loop.rs

+12
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,15 @@ fn main() {
153153
let r = &x;
154154
for _ in r.iter() {}
155155
}
156+
157+
#[clippy::msrv = "1.79"]
158+
pub fn issue_13184() {
159+
// https://github.com/rust-lang/rust-clippy/issues/13184
160+
// No need to fix, as IntoIterator for Box is valid starting from 1.80
161+
let mut values: Box<[u32]> = Box::new([1, 2]);
162+
for _ in values.iter() {}
163+
for _ in values.iter_mut() {}
164+
165+
let rvalues = &values;
166+
for _ in rvalues.iter() {}
167+
}

0 commit comments

Comments
 (0)