Skip to content

Incorrect macro handling in various loop lints #14630

Closed
@samueltardieu

Description

@samueltardieu

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions