Skip to content

Various macro fixes for loop lints #14631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/src/loops/explicit_into_iter_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::EXPLICIT_INTO_ITER_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_trait_method;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::snippet_with_context;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
Expand Down Expand Up @@ -76,7 +76,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
};

let mut applicability = Applicability::MachineApplicable;
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
let object = snippet_with_context(cx, self_arg.span, call_expr.span.ctxt(), "_", &mut applicability).0;
span_lint_and_sugg(
cx,
EXPLICIT_INTO_ITER_LOOP,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/loops/explicit_iter_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::EXPLICIT_ITER_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::snippet_with_context;
use clippy_utils::sym;
use clippy_utils::ty::{
implements_trait, implements_trait_with_env, is_copy, is_type_lang_item, make_normalized_projection,
Expand Down Expand Up @@ -36,7 +36,7 @@ pub(super) fn check(
}

let mut applicability = Applicability::MachineApplicable;
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
let object = snippet_with_context(cx, self_arg.span, call_expr.span.ctxt(), "_", &mut applicability).0;
span_lint_and_sugg(
cx,
EXPLICIT_ITER_LOOP,
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/loops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,9 @@ impl Loops {
}

fn check_for_loop_arg(&self, cx: &LateContext<'_>, _: &Pat<'_>, arg: &Expr<'_>) {
if let ExprKind::MethodCall(method, self_arg, [], _) = arg.kind {
if !arg.span.from_expansion()
&& let ExprKind::MethodCall(method, self_arg, [], _) = arg.kind
{
match method.ident.name {
sym::iter | sym::iter_mut => {
explicit_iter_loop::check(cx, self_arg, arg, self.msrv, self.enforce_iter_loop_reborrow);
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/explicit_into_iter_loop.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,16 @@ fn main() {

for _ in S.into_iter::<u32>() {}
}

fn issue14630() {
macro_rules! mac {
(into_iter $e:expr) => {
$e.into_iter()
};
}

for _ in dbg!([1, 2]) {}
//~^ explicit_into_iter_loop

for _ in mac!(into_iter [1, 2]) {}
}
13 changes: 13 additions & 0 deletions tests/ui/explicit_into_iter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,16 @@ fn main() {

for _ in S.into_iter::<u32>() {}
}

fn issue14630() {
macro_rules! mac {
(into_iter $e:expr) => {
$e.into_iter()
};
}

for _ in dbg!([1, 2]).into_iter() {}
//~^ explicit_into_iter_loop

for _ in mac!(into_iter [1, 2]) {}
}
8 changes: 7 additions & 1 deletion tests/ui/explicit_into_iter_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ error: it is more concise to loop over containers instead of using explicit iter
LL | for _ in mr.into_iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut *mr`

error: aborting due to 6 previous errors
error: it is more concise to loop over containers instead of using explicit iteration methods
--> tests/ui/explicit_into_iter_loop.rs:84:14
|
LL | for _ in dbg!([1, 2]).into_iter() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `dbg!([1, 2])`

error: aborting due to 7 previous errors

13 changes: 13 additions & 0 deletions tests/ui/explicit_iter_loop.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,16 @@ pub fn issue_13184() {
let rvalues = &values;
for _ in rvalues.iter() {}
}

fn issue14630() {
macro_rules! mac {
(iter $e:expr) => {
$e.into_iter()
};
}

for _ in &dbg!([1, 2]) {}
//~^ explicit_iter_loop

for _ in mac!(iter [1, 2]) {}
}
13 changes: 13 additions & 0 deletions tests/ui/explicit_iter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,16 @@ pub fn issue_13184() {
let rvalues = &values;
for _ in rvalues.iter() {}
}

fn issue14630() {
macro_rules! mac {
(iter $e:expr) => {
$e.into_iter()
};
}

for _ in dbg!([1, 2]).iter() {}
//~^ explicit_iter_loop

for _ in mac!(iter [1, 2]) {}
}
8 changes: 7 additions & 1 deletion tests/ui/explicit_iter_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,11 @@ error: it is more concise to loop over references to containers instead of using
LL | for _ in r.iter() {}
| ^^^^^^^^ help: to write this more concisely, try: `r`

error: aborting due to 18 previous errors
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> tests/ui/explicit_iter_loop.rs:194:14
|
LL | for _ in dbg!([1, 2]).iter() {}
| ^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&dbg!([1, 2])`

error: aborting due to 19 previous errors

13 changes: 13 additions & 0 deletions tests/ui/iter_next_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ fn main() {
let u = Unrelated(&[0]);
for _v in u.next() {} // no error
}

fn issue14630() {
macro_rules! mac {
(next $e:expr) => {
$e.iter().next()
};
}

for _ in dbg!([1, 2].iter()).next() {}
//~^ iter_next_loop

for _ in mac!(next [1, 2]) {}
}
8 changes: 7 additions & 1 deletion tests/ui/iter_next_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ LL | for _ in x.iter().next() {}
= note: `-D clippy::iter-next-loop` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::iter_next_loop)]`

error: aborting due to 1 previous error
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
--> tests/ui/iter_next_loop.rs:26:14
|
LL | for _ in dbg!([1, 2].iter()).next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors