Skip to content

for_loops_over_fallibles lint with macros give invalid suggestion #148114

Description

@theemathas

Code

src/main.rs

fn main() {
    let _name = Some(1);
    dep::do_loop!(_name);
}

dep/src/lib.rs

#[macro_export]
macro_rules! identity {
    ($x:ident) => {
        $x
    };
}

#[macro_export]
macro_rules! do_loop {
    ($x:ident) => {
        for $crate::identity!($x) in $x {}
    };
}

Zip of all files needed for reproduction, for your convenience: repro.zip

Current output

warning: for loop over an `Option`. This is more readably written as an `if let` statement
 --> src\main.rs:3:5
  |
3 |     dep::do_loop!(_name);
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(for_loops_over_fallibles)]` on by default
help: to check pattern in a loop use `while let`
 --> C:\Users\theem\Downloads\repro\dep\src\lib.rs:4:9
  |
4 ~         while let Some(for $crate::identity!($x) in $x {}
5 ~         ) =
  |
help: consider using `if let` to clear intent
 --> C:\Users\theem\Downloads\repro\dep\src\lib.rs:4:9
  |
4 ~         if let Some(for $crate::identity!($x) in $x {}
5 ~         ) =
  |

warning: `repro` (bin "repro") generated 1 warning

Desired output

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-pc-windows-msvc
release: 1.90.0
LLVM version: 20.1.8

Anything else?

Discovered in #147973 as part of a crater run, in which rust gave the invalid suggestion in 1.90.0, but ICE'ed in beta.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-for_loops_over_falliblesLint: for_loops_over_falliblesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions