rust-lang / rust-lang/rustfmt

Enhance formatting for ConstBlock expressions

Open
#4,483 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

1x-backport:pending E-help-wanted good first issue
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

#4478 included some initial support for the new ConstBlock expression kind variant, but there's opportunities to enhance and improve the formatting.

The relevant section of the codebase can be found in formatting/expr.rs
https://github.com/rust-lang/rustfmt/blob/cbe01e47024c852e2ed852671e76f7929c4bab91/src/formatting/expr.rs#L127-L129

There are 3 core activities to completing this issue:

  1. Honor the brace_style configuration option
  2. Account for any comments between the const keyword and the block
  3. Add test cases
1. brace_style

This should be relatively easy. When brace_style is set to AlwaysNextLine (can be checked with context.config.brace_style()) then we'll want to insert a newline and start the block on the next line (the correct newline and indentation can be achieved via shape.to_string_with_newline(context.config)), otherwise there should just be a space between the keyword and block

2. comments

First step will be to determine the span between the end of the const keyword and the start of the block. Consider using context.snippet.span_after to figure out the lo for this "between" span, and for the hi you will want to use the lo of the block (anon_const.value.span.lo())

Once you have that span there are a few different options and comment utility functions for formatting the expression and ensuring comments are properly formatted. You may want to take a look at one or more of them:

  • contains_comment
  • combine_strs_with_missing_comments
  • rewrite_missing_comment
  • recover_missing_comment_in_span
3. tests

Below are a few (nonexhaustive) set of tests to cover some top of mind scenarios.

You will probably want to add a const_block_always_next_line.rs file under the configs/brace_style directory (https://github.com/rust-lang/rustfmt/tree/master/tests/source/configs/brace_style) for both tests/source and tests/target

fn foo() -> i32 {
    const {
        let x = 5 + 10;
        x / 3
    }
}

fn bar() -> i32 {
    const { 4 }
}

fn foo() -> i32 {
    const
{
        let x = 5 + 10;
        x / 3
    }
}

fn foo() -> i32 {
    const // baz
{
        let x = 5 + 10;
        x / 3
    }
}

fn foo() -> i32 {
    const /*qux */ {
        let x = 5 + 10;
        x / 3
    }
}

fn foo() -> i32 {
    const
// baz
{
        let x = 5 + 10;
        x / 3
    }
}

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 in src/formatting/expr.rs at the ConstBlock handling, then inspect the referenced comment helpers such as context.snippet.span_after and contains_comment. Add coverage under tests/source/configs/brace_style and tests/target, including AlwaysNextLine and comments between const and the block. Done means brace_style is honored, comments are preserved and formatted, and the new test cases pass.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.