Skip to content

Update recommended rustfmt config #96

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion content/wiki/formatting_scheme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@ For those files, the formatting produced by `rustfmt` is required.
The following rustfmt config is recommended:

```toml
# LINEBENDER RUSTFMT CONFIG - v1
# LINEBENDER RUSTFMT CONFIG - v2
# Ensure lines end with \n even if the git configuration core.autocrlf is not set to true
newline_style = "Unix"

# `Foobar { foo, bar }` is more readable than `Foo { foo: foo, bar: bar }`
use_field_init_shorthand = true

# Forces let else blocks to always be their own line(s).
# Enforcing this helps readers scan all early returns of a function at a glance.
single_line_let_else_max_width = 0
Copy link
Member

Choose a reason for hiding this comment

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

Copying my previous review comment from the last time you submitted this:

If we do decide to set this, I'd expect it to have the same value as single_line_if_else_max_width, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I disagree, for the reason exposed in the comment above.

I think

let foo = if x { y } else { z };

is fundamentally different from

let Some(foo) = x { y } else { return z };

and that, unlike the if else case, the else return case deserves to be spread out no matter its length.

In general, I think a control-flow-altering statement should always be on its own line.

(In theory you can do a single line if x { y } else { return z }, but only by treating the return statement as an expression, which is rare in practice.)

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for providing reasoning. I'm not completely sure I agree; for one thing, no style guide would ever have the formatting:

some_fallible_operation()
    ?
    .action()
    ?;

To be clear, my preference would just be to stick with the default, rather than matching the two at zero; I don't think it's a significant enough win, and IDEs can already highlight exit points for you in a much more complete manner

That being said, I'm not going to argue for it further; e.g. the Xilem repository which already has this set has literally no occurrences which would be formatted on a single line anyway.


# Commented out because it is still unstable, but works fine in practice.
# imports_granularity = "Module"

# END LINEBENDER RUSTFMT CONFIG
```

You may also want to occasionally use this unstable config:

```toml
# Groups imports in a predictable way: first core/alloc/std, then other crates, then the current crate.
group_imports = "StdExternalCrate"
```

We don't recommend it as a permanent config value even for nightly projects, because the way it reorders items [isn't always ideal](https://github.com/linebender/linebender.github.io/issues/87).
Copy link
Member

Choose a reason for hiding this comment

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

Maybe "exports" rather than "items" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think "items" is fine for a short description.


## Markdown

In Markdown files, every paragraph should have one line per sentence.
Expand Down