rust-lang / rust-lang/rustfmt

rustfmt fails to format braceless closure that starts with a comment

Open
#4,855 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-closures A-comments P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

This code from where I was doing exercism rust exercises

pub fn brackets_are_balanced(string: &str) -> bool {
    let mut ret = None;
    let mut stack = Vec::new();
    string.chars().for_each(|c|
        // remember opened brackets
        if ['[','{','('].contains(&c) {
            stack.push(c);
    } else if 
        // if we encounter closing brackets
            [']','}',')'].contains(&c) {
                let pair = (stack.pop(), c) ;
          
                match pair {
                    // make sure we have a corresponding bracket at the stack 
                    (Some('['), ']')|   (Some('{'), '}') |       (Some('('), ')')  =>  {},
                    // if this is not the case, return false
                _ => {ret = Some(false);},
                }
        }
   
   
    );
    if let Some(ret) = ret {
        ret
    } else {
        stack.is_empty()
    }
}

rustfmt really did not like that code:

 --> /home/matthias/exercism/rust/matching-brackets/src/lib.rs:8:8:14
  |
8 |     } else if 
  |              ^
  |

error[internal]: left behind trailing whitespace
  --> /home/matthias/exercism/rust/matching-brackets/src/lib.rs:12:12:1
   |
12 |           
   | ^^^^^^^^^^
   |

error[internal]: left behind trailing whitespace
  --> /home/matthias/exercism/rust/matching-brackets/src/lib.rs:20:20:1
   |
20 |    
   | ^^^
   |

error[internal]: left behind trailing whitespace
  --> /home/matthias/exercism/rust/matching-brackets/src/lib.rs:21:21:1
   |
21 |    
   | ^^^
   |

warning: rustfmt has failed to format. See previous 4 errors.

rustfmt 1.4.37-nightly (f58631b 2021-05-28)

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

Start by reproducing the supplied braceless-closure example with the reported rustfmt version and inspect how comments and trailing whitespace are handled around the closure conditions. Done means rustfmt formats the example without internal errors or leftover trailing whitespace; the issue does not name a source file or test to update.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.