Skip to content

style guide: add let-chain rules #139456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

calebcartwright
Copy link
Member

Reopens #110568

refs #53667 and I suppose #132833 as well

This reflects the style rules that the style team had already agreed upon back in 2023, with the addition of literals in the lhs being permissible for single line formatting, and the removal of unnecessary language/example snippets around non-&& operators that was a small hiccup in the original PR.

It also reflects current formatting behavior implemented in rustfmt (though note that the adjustment to include literals has been implemented & merged, but is still pending a sync to nightly)

@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2025

r? @joshtriplett

rustbot has assigned @joshtriplett.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-style Relevant to the style team, which will review and decide on the PR/issue. labels Apr 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2025

Some changes occurred in src/doc/style-guide

cc @rust-lang/style

Comment on lines +597 to +598
&& let Some(relatively_long_thing) =
a_long_long_long_long_long_long_really_reallllllllllyyyyyyy_long_expression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the = be on the second line here, given the explanation above of "prefer to break before the ="?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

I think the "before operator" prescription may have been a snippet of text we missed in #113145 (and some more context linked in rust-lang/style-team#179) when we changed the text to match the existing behavior of always breaking after the operator

for example, rustfmt has long formatted chainless if let Some(....) .... with breaks after = - https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=9f656a763b2b09c559d39568c2d5079c

fn main() {
    if let Some(x) = a_long_long_long_long_long_long_really_realllyyyy_long_expressionddddddddddddddddd {
        // ..
    }

    if let Some(relatively_long_thing) =
    a_long_long_long_long_long_long_really_realllyyyy_long_expression
{
        // ..
    }
}

is formatted as:

fn main() {
    if let Some(x) =
        a_long_long_long_long_long_long_really_realllyyyy_long_expressionddddddddddddddddd
    {
        // ..
    }

    if let Some(relatively_long_thing) =
        a_long_long_long_long_long_long_really_realllyyyy_long_expression
    {
        // ..
    }
}

I'm going to change the text in the control flow section to indicate breaking after under the same rationale as we had previously of "bug fix" to the style guide text, but happy to discuss more if you think that's the wrong direction

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I agree that we should match the existing behavior.

@joshtriplett
Copy link
Member

@calebcartwright One review comment; with that addressed, r=me.

@calebcartwright calebcartwright force-pushed the style-let-chains-final branch from b7080a1 to ee07e3f Compare April 7, 2025 00:08
@joshtriplett
Copy link
Member

LGTM, r=me whenever you think it ready.

@calebcartwright
Copy link
Member Author

@bors r=joshtriplett rollup

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

📌 Commit ee07e3f has been approved by joshtriplett

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 7, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang#138314 (fix usage of `autodiff` macro with inner functions)
 - rust-lang#139426 (Make the UnifyKey and UnifyValue imports non-nightly)
 - rust-lang#139431 (Remove LLVM 18 inline ASM span fallback)
 - rust-lang#139456 (style guide: add let-chain rules)
 - rust-lang#139467 (More trivial tweaks)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1b8dbda into rust-lang:master Apr 7, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
Rollup merge of rust-lang#139456 - calebcartwright:style-let-chains-final, r=joshtriplett

style guide: add let-chain rules

Reopens rust-lang#110568

refs rust-lang#53667 and I suppose rust-lang#132833 as well

This reflects the style rules that the style team had already agreed upon back in 2023, with the addition of literals in the lhs being permissible for single line formatting, and the removal of unnecessary language/example snippets around non-`&&` operators that was a small hiccup in the original PR.

It also reflects current formatting behavior implemented in rustfmt (though note that the adjustment to include literals has been implemented & merged, but is still pending a sync to nightly)
@calebcartwright calebcartwright deleted the style-let-chains-final branch April 7, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-style Relevant to the style team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants