Skip to content

Incorrect macro handling in various loop lints #14630

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

Closed
samueltardieu opened this issue Apr 16, 2025 · 0 comments · Fixed by #14631
Closed

Incorrect macro handling in various loop lints #14630

samueltardieu opened this issue Apr 16, 2025 · 0 comments · Fixed by #14631
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@samueltardieu
Copy link
Contributor

samueltardieu commented Apr 16, 2025

Summary

The explicit_into_iter_loop, explicit_iter_loop and iter_next_loop all try to lint expanded code, and also point into macro code even when they could trigger.

Reproducer

I tried this code:

#![deny(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop, clippy::iter_next_loop)]
#![allow(for_loops_over_fallibles)]

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

    for _ in dbg!([1, 2]).iter() {}
    for _ in dbg!([1, 2]).into_iter() {}

    for _ in mac!(iter [1, 2]) {}
    for _ in mac!(into_iter [1, 2]) {}
    for _ in mac!(next [1, 2]) {}
}

I expected to see this happen: proper suggestions

Instead, this happened:

error: it is more concise to loop over references to containers instead of using explicit iteration methods
  --> t.rs:11:14
   |
11 |     for _ in dbg!([1, 2]).iter() {}
   |              ^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
note: the lint level is defined here
  --> t.rs:1:42
   |
1  | #![deny(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop, clippy::iter_next_loop)]
   |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to write this more concisely, try
   |
11 ~     for _ in &match $val {
12 +             tmp => {
13 +                 $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
14 +                     $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
15 +                 tmp
16 +             }
17 ~         } {}
   |

error: it is more concise to loop over containers instead of using explicit iteration methods
  --> t.rs:12:14
   |
12 |     for _ in dbg!([1, 2]).into_iter() {}
   |              ^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_into_iter_loop
note: the lint level is defined here
  --> t.rs:1:9
   |
1  | #![deny(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop, clippy::iter_next_loop)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to write this more concisely, try
   |
12 ~     for _ in match $val {
13 +             tmp => {
14 +                 $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
15 +                     $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
16 +                 tmp
17 +             }
18 ~         } {}
   |

p over references to containers instead of using explicit iteration methods
  --> t.rs:6:29
   |
6  |         (iter $e:expr) => { $e.iter() };
   |                             ^^^^^^^^^ help: to write this more concisely, try: `&[1, 2]`
...
14 |     for _ in mac!(iter [1, 2]) {}
   |              ----------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
   = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: it is more concise to loop over containers instead of using explicit iteration methods
  --> t.rs:7:34
   |
7  |         (into_iter $e:expr) => { $e.into_iter() };
   |                                  ^^^^^^^^^^^^^^ help: to write this more concisely, try: `[1, 2]`
...
15 |     for _ in mac!(into_iter [1, 2]) {}
   |              ---------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_into_iter_loop
   = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
  --> t.rs:8:29
   |
8  |         (next $e:expr) => { $e.iter().next() };
   |                             ^^^^^^^^^^^^^^^^
...
16 |     for _ in mac!(next [1, 2]) {}
   |              ----------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
note: the lint level is defined here
  --> t.rs:1:70
   |
1  | #![deny(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop, clippy::iter_next_loop)]
   |                                                                      ^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 5 previous errors

Those suggestions are all inappropriate, or point into macro code.

Version


Additional Labels

@rustbot claim

@samueltardieu samueltardieu added the C-bug Category: Clippy is not doing the correct thing label Apr 16, 2025
github-merge-queue bot pushed a commit that referenced this issue May 20, 2025
The `explicit_into_iter_loop`, `explicit_iter_loop` and `iter_next_loop`
will now:

- trigger only when the triggering expression is not located into macro
code;
- properly expose code rewrite proposal with code coming from the root
context.

changelog: [`explicit_into_iter_loop`, `explicit_iter_loop`,
`iter_next_loop`]: behave in macro context

Fixes #14630
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant