Skip to content

Incorrect span in for_loops_over_fallibles with macros #140747

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
bvanjoi opened this issue May 7, 2025 · 1 comment · Fixed by #140801
Closed

Incorrect span in for_loops_over_fallibles with macros #140747

bvanjoi opened this issue May 7, 2025 · 1 comment · Fixed by #140801
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bvanjoi
Copy link
Contributor

bvanjoi commented May 7, 2025

Code

fn main() {
    macro_rules! x {
        () => {
            None::<i32>
        };
    }
    for _ in x! {} {}
}

Current output

warning: for loop over an `Option`. This is more readably written as an `if let` statement
 --> crates/a/src/main.rs:4:13
  |
4 |             None::<i32>
  |             ^^^^^^^^^^^
...
7 |     for _ in x! {} {}
  |              ----- in this macro invocation
  |
  = note: `#[warn(for_loops_over_fallibles)]` on by default
  = note: this warning originates in the macro `x` (in Nightly builds, run with -Z macro-backtrace for more info)
help: to check pattern in a loop use `while let`
  |
4 ~             ) = 
5 |         };
6 |     }
7 ~     while let Some(_ in x! {} {}
  |
help: consider using `if let` to clear intent
  |
4 ~             ) = 
5 |         };
6 |     }
7 ~     if let Some(_ in x! {} {}
  |

Desired output

Ensure spans are either hidden or accurately displayed.

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (27d6200a7 2025-05-06)
binary: rustc
commit-hash: 27d6200a70601f6fcf419bf2f9e37989f3624ca4
commit-date: 2025-05-06
host: aarch64-apple-darwin
release: 1.88.0-nightly
LLVM version: 20.1.4

Anything else?

No response

@bvanjoi bvanjoi added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 7, 2025
@xizheyin
Copy link
Contributor

xizheyin commented May 8, 2025

@rustbot claim

jhpratt added a commit to jhpratt/rust that referenced this issue May 9, 2025
Use span before macro expansion in lint for-loops-over-falibles

Fixes rust-lang#140747

I think there are going to be a lot of cases where macros are expanded in the compiler resulting in span offsets, and I'd like to know how that's typically handled. Does it have to be handled specially every time?
jhpratt added a commit to jhpratt/rust that referenced this issue May 9, 2025
Use span before macro expansion in lint for-loops-over-falibles

Fixes rust-lang#140747

I think there are going to be a lot of cases where macros are expanded in the compiler resulting in span offsets, and I'd like to know how that's typically handled. Does it have to be handled specially every time?
@bors bors closed this as completed in b165a4c May 9, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 9, 2025
Rollup merge of rust-lang#140801 - xizheyin:issue-140747, r=SparrowLii

Use span before macro expansion in lint for-loops-over-falibles

Fixes rust-lang#140747

I think there are going to be a lot of cases where macros are expanded in the compiler resulting in span offsets, and I'd like to know how that's typically handled. Does it have to be handled specially every time?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants