Skip to content

rustfmt fails to format braceless closure that starts with a comment #4855

Open
@matthiaskrgr

Description

@matthiaskrgr

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions