rust-lang / rust-lang/rustfmt

Add single line if block support for single expression

Open
#5,939 1 comment 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What did you expect?
if false { continue }
let Some(x) = opt else { return };
What did you get?
if false { 
    continue  
}
let Some(x) = opt else { return };

cargo fmt puts let-else on a single line, and it could do the same to if blocks that have a single expression (i.e. return, break, or continue). That way there can be a consistent style for all guards.

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 locating rustfmt's formatting logic for if blocks and the existing handling for let-else expressions. Run cargo fmt on the examples in the issue, then verify that if blocks containing only return, break, or continue stay on one line while other blocks retain their current formatting.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.