rust-lang / rust-lang/rustfmt

improve formatting of boolean expression with groups

Open
#3,108 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-binary-ops C-feature-request
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

I think the formatting of the following boolean expression could be improved so that it is clear that:

  • the first two operands are OR'ed
  • and its result is AND'ed with the third operand.

https://github.com/rust-lang-nursery/rustfmt/blob/075aa909cd2bcdc603f45e0b65119c6a7e3b2030/src/expr.rs#L886-L888

A possible formatting would be:

let force_newline_brace =
    (pat_expr_string.contains('\n') || pat_expr_string.len() > one_line_budget) 
        && !last_line_extendable(&pat_expr_string);

If there is not enough space, we could have something like:

let force_newline_brace =
    (pat_expr_string12345678.contains('\n')
     || pat_expr_string123456789.len() > one_line_budget) 
        && !last_line_extendable(&pat_expr_string);

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 with src/expr.rs around lines 886-888, where the boolean expression is formatted. Compare the current output with the grouped examples in the issue and trace the surrounding formatting logic. Done means rustfmt produces grouping that makes the OR operands and their AND relationship clear, including when the expression wraps.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.