Skip to content

Commit 20ce7f1

Browse files
authored
Rollup merge of #113380 - joshtriplett:style-guide-cleanup-must-should-may, r=calebcartwright
style-guide: clean up "must"/"should"/"may" Avoid using "should" or "may" for required parts of the default style. The style guide inconsistently used language like "there should be a space" or "it should be on its own line", or "may be written on a single line", for things that are required components of the default Rust style. "should" and especially "may" come across as optional. While the style guide overall now has a statement at the top that the default style itself is a *recommendation*, the *definition* of the default style should not be ambiguous about what's part of the default style. Rewrite language in the style guide to only use "should" and "may" and similar for truly optional components of the style (e.g. things a tool cannot or should not enforce in its default configuration). In their place, either use "must", or rewrite in imperative style ("put a space", "start it on the same line"). The latter also substantially reduces the use of passive voice. Looking for "should"s also flagged some recommendations the style guide made for configurability of tools (e.g. a tool "should" have a given configuration option). I've removed those recommendations, per discussion with the style team; it's not the domain of the style guide to make such recommendations, only to define the default Rust style. In the process of making this change, I also fixed a typo, fixed a text structure issue, fixed an example that didn't match the Rust style (missing a trailing comma), and added an additional example for clarity. (Those changes would have conflicted with this one.) Those changes appear in separate commits. These are all purely editorial changes, and do not affect the semantic definition of the Rust style.
2 parents d26f0b7 + 77d09cb commit 20ce7f1

File tree

5 files changed

+260
-270
lines changed

5 files changed

+260
-270
lines changed

src/doc/style-guide/src/README.md

+21-25
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ options.
4040
* Each level of indentation must be four spaces (that is, all indentation
4141
outside of string literals and comments must be a multiple of four).
4242
* The maximum width for a line is 100 characters.
43-
* A tool may choose to make some of these configurable.
4443

4544
#### Block indent
4645

@@ -63,7 +62,8 @@ example) and less rightward drift.
6362

6463
### Trailing commas
6564

66-
Lists should have a trailing comma when followed by a newline:
65+
In comma-separated lists of any kind, use a trailing comma when followed by a
66+
newline:
6767

6868
```rust
6969
function_call(
@@ -99,8 +99,6 @@ fn bar() {}
9999
fn baz() {}
100100
```
101101

102-
Formatting tools may wish to make the bounds on blank lines configurable.
103-
104102
### [Module-level items](items.md)
105103
### [Statements](statements.md)
106104
### [Expressions](expressions.md)
@@ -114,17 +112,17 @@ formatter might skip formatting of comments.
114112

115113
Prefer line comments (`//`) to block comments (`/* ... */`).
116114

117-
When using line comments there should be a single space after the opening sigil.
115+
When using line comments, put a single space after the opening sigil.
118116

119-
When using single-line block comments there should be a single space after the
120-
opening sigil and before the closing sigil. Multi-line block comments should
121-
have a newline after the opening sigil and before the closing sigil.
117+
When using single-line block comments, put a single space after the opening
118+
sigil and before the closing sigil. For multi-line block comments, put a
119+
newline after the opening sigil, and a newline before the closing sigil.
122120

123-
Prefer to put a comment on its own line. Where a comment follows code, there
124-
should be a single space before it. Where a block comment is inline, there
125-
should be surrounding whitespace as if it were an identifier or keyword. There
126-
should be no trailing whitespace after a comment or at the end of any line in a
127-
multi-line comment. Examples:
121+
Prefer to put a comment on its own line. Where a comment follows code, put a
122+
single space before it. Where a block comment appears inline, use surrounding
123+
whitespace as if it were an identifier or keyword. Do not include trailing
124+
whitespace after a comment or at the end of any line in a multi-line comment.
125+
Examples:
128126

129127
```rust
130128
// A comment on an item.
@@ -173,7 +171,7 @@ Prefer line comments (`///`) to block comments (`/** ... */`).
173171
Prefer outer doc comments (`///` or `/** ... */`), only use inner doc comments
174172
(`//!` and `/*! ... */`) to write module-level or crate-level documentation.
175173

176-
Doc comments should come before attributes.
174+
Put doc comments before attributes.
177175

178176
### Attributes
179177

@@ -198,18 +196,20 @@ struct CRepr {
198196
}
199197
```
200198

201-
For attributes with an equal sign, there should be a single space before and
202-
after the `=`, e.g., `#[foo = 42]`.
199+
For attributes with an equal sign, put a single space before and after the `=`,
200+
e.g., `#[foo = 42]`.
203201

204202
There must only be a single `derive` attribute. Note for tool authors: if
205203
combining multiple `derive` attributes into a single attribute, the ordering of
206-
the derived names should be preserved. E.g., `#[derive(bar)] #[derive(foo)]
207-
struct Baz;` should be formatted to `#[derive(bar, foo)] struct Baz;`.
204+
the derived names must generally be preserved for correctness:
205+
`#[derive(Foo)] #[derive(Bar)] struct Baz;` must be formatted to
206+
`#[derive(Foo, Bar)] struct Baz;`.
208207

209208
### *small* items
210209

211-
In many places in this guide we specify that a formatter may format an item
212-
differently if it is *small*, for example struct literals:
210+
In many places in this guide we specify formatting that depends on a code
211+
construct being *small*. For example, single-line vs multi-line struct
212+
literals:
213213

214214
```rust
215215
// Normal formatting
@@ -218,7 +218,7 @@ Foo {
218218
f2: another_expression(),
219219
}
220220

221-
// *small* formatting
221+
// "small" formatting
222222
Foo { f1, f2 }
223223
```
224224

@@ -231,10 +231,6 @@ complexity of an item (for example, that all components must be simple names,
231231
not more complex sub-expressions). For more discussion on suitable heuristics,
232232
see [this issue](https://github.com/rust-lang-nursery/fmt-rfcs/issues/47).
233233

234-
Tools should give the user an option to ignore such heuristics and always use
235-
the normal formatting.
236-
237-
238234
## [Non-formatting conventions](advice.md)
239235

240236
## [Cargo.toml conventions](cargo.md)

0 commit comments

Comments
 (0)