rust-lang / rust-lang/rustfmt

Parse error is reported for malformed body in `cfg_if!`

Open
#5,813 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ecosystem-cfg-if A-macros A-parser C-bug
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

On the current nightly, rustfmt errors out after suggesting to add braces in an inappropriate location to a closure where it doesn't make sense to use braces begin with.

Example of affected code: https://github.com/HadrienG2/grayscott/blob/e64ff006867e7c66c9834cff20d920612578df7f/data/src/concentration/simd/safe_arch.rs#L524 .

Error message:

error: closure bodies that contain statements must be surrounded by braces
   --> /home/hadrien/Bureau/grayscott/data/src/concentration/simd/safe_arch.rs:524:45
    |
524 |                     let masks256 = array2(|i| self[i].ge(other[i]));
    |                                             ^
525 |                     let [lo128; hi128] = masks256.map(safe_arch::convert_to_m128_from_m256d);
    |                                      ^
    |
note: statement found outside of a block
   --> /home/hadrien/Bureau/grayscott/data/src/concentration/simd/safe_arch.rs:525:31
    |
524 |                     let masks256 = array2(|i| self[i].ge(other[i]));
    |                                               -------------------- this expression is a statement because of the trailing semicolon
525 |                     let [lo128; hi128] = masks256.map(safe_arch::convert_to_m128_from_m256d);
    |                               ^ this `;` turns the preceding closure into a statement
note: the closure body may be incorrectly delimited
   --> /home/hadrien/Bureau/grayscott/data/src/concentration/simd/safe_arch.rs:524:43
    |
524 |                     let masks256 = array2(|i| self[i].ge(other[i]));
    |                                           ^^^^^^^^^^^^^^^^^^^^^^^^ this is the parsed closure...
525 |                     let [lo128; hi128] = masks256.map(safe_arch::convert_to_m128_from_m256d);
    |                                      - ...but likely you meant the closure to end here
help: try adding braces
    |
524 ~                     let masks256 = array2(|i| { self[i].ge(other[i]));
525 ~                     let [lo128; hi128}] = masks256.map(safe_arch::convert_to_m128_from_m256d);
    |

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the affected expression in data/src/concentration/simd/safe_arch.rs at the linked revision. Start by examining rustfmt's handling of the malformed closure body and its diagnostic suggestion. Done means the code no longer receives an inappropriate brace suggestion and reports the malformed body correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.