Skip to content

Commit 441e59a

Browse files
authored
Rollup merge of #112942 - joshtriplett:style-guide-tweaks, r=compiler-errors
style-guide: Organizational and editing tweaks (no semantic changes) I'd recommend reviewing this PR commit-by-commit; each commit is self-contained and should be easy to review at a glance. - style-guide: Move text about block vs visual indent to indentation section - style-guide: Move and expand text about trailing commas - style-guide: s/right-ward/rightward/ - style-guide: Consistently refer to rustfmt as `rustfmt` - style-guide: Remove inaccurate statement about rustfmt - style-guide: Define (and capitalize) "ASCIIbetically" - style-guide: Update cargo.md for authors being optional and not recommended - style-guide: Avoid normative recommendations for formatting tool configurability - style-guide: Clarify advice on names matching keywords - style-guide: Reword an awkwardly phrased recommendation (and fix a typo) - style-guide: Rephrase a confusingly ordered, ambiguous sentence (and fix a typo) - style-guide: Avoid hyphenating "semicolon" - style-guide: Make link text in SUMMARY.md match the headings in the linked pages - style-guide: Define what an item is - style-guide: Avoid referring to the style team in the past tense
2 parents 46aacf5 + fcc23a3 commit 441e59a

File tree

7 files changed

+88
-72
lines changed

7 files changed

+88
-72
lines changed

Diff for: src/doc/style-guide/src/README.md

+43-8
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Rust code has similar formatting, less mental effort is required to comprehend a
1616
new project, lowering the barrier to entry for new developers.
1717

1818
Thus, there are productivity benefits to using a formatting tool (such as
19-
rustfmt), and even larger benefits by using a community-consistent formatting,
20-
typically by using a formatting tool's default settings.
19+
`rustfmt`), and even larger benefits by using a community-consistent
20+
formatting, typically by using a formatting tool's default settings.
2121

2222

2323
## Formatting conventions
@@ -28,8 +28,47 @@ typically by using a formatting tool's default settings.
2828
* Each level of indentation must be four spaces (that is, all indentation
2929
outside of string literals and comments must be a multiple of four).
3030
* The maximum width for a line is 100 characters.
31-
* A tool should be configurable for all three of these variables.
31+
* A tool may choose to make some of these configurable.
3232

33+
#### Block indent
34+
35+
Prefer block indent over visual indent:
36+
37+
```rust
38+
// Block indent
39+
a_function_call(
40+
foo,
41+
bar,
42+
);
43+
44+
// Visual indent
45+
a_function_call(foo,
46+
bar);
47+
```
48+
49+
This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
50+
example) and less rightward drift.
51+
52+
### Trailing commas
53+
54+
Lists should have a trailing comma when followed by a newline:
55+
56+
```rust
57+
function_call(
58+
argument,
59+
another_argument,
60+
);
61+
62+
let array = [
63+
element,
64+
another_element,
65+
yet_another_element,
66+
];
67+
```
68+
69+
This makes moving code (e.g., by copy and paste) easier, and makes diffs
70+
smaller, as appending or removing items does not require modifying another line
71+
to add or remove a comma.
3372

3473
### Blank lines
3574

@@ -48,11 +87,7 @@ fn bar() {}
4887
fn baz() {}
4988
```
5089

51-
Formatting tools should make the bounds on blank lines configurable: there
52-
should be separate minimum and maximum numbers of newlines between both
53-
statements and (top-level) items (i.e., four options). As described above, the
54-
defaults for both statements and items should be minimum: 1, maximum: 2.
55-
90+
Formatting tools may wish to make the bounds on blank lines configurable.
5691

5792
### [Module-level items](items.md)
5893
### [Statements](statements.md)

Diff for: src/doc/style-guide/src/SUMMARY.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[Introduction](README.md)
44

5-
- [Module-level items](items.md)
5+
- [Items](items.md)
66
- [Statements](statements.md)
77
- [Expressions](expressions.md)
8-
- [Types](types.md)
9-
- [Non-formatting conventions](advice.md)
8+
- [Types and Bounds](types.md)
9+
- [Other style advice](advice.md)
1010
- [`Cargo.toml` conventions](cargo.md)
11-
- [Principles used for deciding these guidelines](principles.md)
11+
- [Guiding principles and rationale](principles.md)

Diff for: src/doc/style-guide/src/advice.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ if y {
2525
* Local variables shall be `snake_case`,
2626
* Macro names shall be `snake_case`,
2727
* Constants (`const`s and immutable `static`s) shall be `SCREAMING_SNAKE_CASE`.
28-
* When a name is forbidden because it is a reserved word (e.g., `crate`), use a
29-
trailing underscore to make the name legal (e.g., `crate_`), or use raw
30-
identifiers if possible.
28+
* When a name is forbidden because it is a reserved word (such as `crate`),
29+
either use a raw identifier (`r#crate`) or use a trailing underscore
30+
(`crate_`). Don't misspell the word (`krate`).
3131

3232
### Modules
3333

Diff for: src/doc/style-guide/src/cargo.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cargo.toml conventions
1+
# `Cargo.toml` conventions
22

33
## Formatting conventions
44

@@ -25,16 +25,17 @@ not indent any key names; start all key names at the start of a line.
2525
Use multi-line strings (rather than newline escape sequences) for any string
2626
values that include multiple lines, such as the crate description.
2727

28-
For array values, such as a list of authors, put the entire list on the same
28+
For array values, such as a list of features, put the entire list on the same
2929
line as the key, if it fits. Otherwise, use block indentation: put a newline
3030
after the opening square bracket, indent each item by one indentation level,
3131
put a comma after each item (including the last), and put the closing square
3232
bracket at the start of a line by itself after the last item.
3333

3434
```rust
35-
authors = [
36-
"A Uthor <[email protected]>",
37-
"Another Author <[email protected]>",
35+
some_feature = [
36+
"another_feature",
37+
"yet_another_feature",
38+
"some_dependency?/some_feature",
3839
]
3940
```
4041

@@ -54,11 +55,11 @@ version = "4.5.6"
5455

5556
## Metadata conventions
5657

57-
The authors list should consist of strings that each contain an author name
58-
followed by an email address in angle brackets: `Full Name <email@address>`.
59-
It should not contain bare email addresses, or names without email addresses.
60-
(The authors list may also include a mailing list address without an associated
61-
name.)
58+
The authors list, if present, should consist of strings that each contain an
59+
author name followed by an email address in angle brackets: `Full Name
60+
<email@address>`. It should not contain bare email addresses, or names without
61+
email addresses. (The authors list may also include a mailing list address
62+
without an associated name.)
6263

6364
The license field must contain a valid [SPDX
6465
expression](https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60),

Diff for: src/doc/style-guide/src/items.md

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Items
22

3+
Items consist of the set of things permitted at the top level of a module.
4+
However, Rust also allows some items to appear within some other types of
5+
items, such as within a function. The same formatting conventions apply whether
6+
an item appears at module level or within another item.
7+
38
`extern crate` statements must be first in a file. They must be ordered
49
alphabetically.
510

@@ -15,8 +20,8 @@ Tools should make the above ordering optional.
1520

1621
### Function definitions
1722

18-
In Rust, one finds functions by searching for `fn [function-name]`; It's
19-
important that you style your code so that it's very searchable in this way.
23+
In Rust, people often find functions by searching for `fn [function-name]`, so
24+
the formatting of function definitions shold enable this.
2025

2126
The proper ordering and spacing is:
2227

@@ -63,8 +68,9 @@ let y = (11, 22, 33);
6368

6469
In the declaration, put each variant on its own line, block indented.
6570

66-
Format each variant accordingly as either a struct, tuple struct, or identifier,
67-
which doesn't require special formatting (but without the `struct` keyword.
71+
Format each variant accordingly as either a struct (but without the `struct`
72+
keyword), a tuple struct, or an identifier (which doesn't require special
73+
formatting):
6874

6975
```rust
7076
enum FooBar {
@@ -139,7 +145,7 @@ union Foo {
139145

140146
Put the whole struct on one line if possible. Types in the parentheses should be
141147
separated by a comma and space with no trailing comma. No spaces around the
142-
parentheses or semi-colon:
148+
parentheses or semicolon:
143149

144150
```rust
145151
pub struct Foo(String, u8);
@@ -230,7 +236,7 @@ impl Bar
230236

231237
`extern crate foo;`
232238

233-
Use spaces around keywords, no spaces around the semi-colon.
239+
Use spaces around keywords, no spaces around the semicolon.
234240

235241

236242
### Modules
@@ -245,7 +251,7 @@ mod foo;
245251
```
246252

247253
Use spaces around keywords and before the opening brace, no spaces around the
248-
semi-colon.
254+
semicolon.
249255

250256
### macro\_rules!
251257

@@ -478,8 +484,8 @@ foo::{
478484
A *group* of imports is a set of imports on the same or sequential lines. One or
479485
more blank lines or other items (e.g., a function) separate groups of imports.
480486

481-
Within a group of imports, imports must be sorted ascii-betically. Groups of
482-
imports must not be merged or re-ordered.
487+
Within a group of imports, imports must be sorted ASCIIbetically (uppercase
488+
before lowercase). Groups of imports must not be merged or re-ordered.
483489

484490

485491
E.g., input:
@@ -505,13 +511,9 @@ use b;
505511
Because of `macro_use`, attributes must also start a new group and prevent
506512
re-ordering.
507513

508-
Note that tools which only have access to syntax (such as Rustfmt) cannot tell
509-
which imports are from an external crate or the std lib, etc.
510-
511-
512514
#### Ordering list import
513515

514-
Names in a list import must be sorted ascii-betically, but with `self` and
516+
Names in a list import must be sorted ASCIIbetically, but with `self` and
515517
`super` first, and groups and glob imports last. This applies recursively. For
516518
example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
517519
`use foo::bar::{a, b::c, b::d, b::d::{x, y, z}, b::{self, r, s}};`.

Diff for: src/doc/style-guide/src/principles.md

+4-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Guiding principles and rationale
22

3-
When deciding on style guidelines, the style team tried to be guided by the
4-
following principles (in rough priority order):
3+
When deciding on style guidelines, the style team follows these guiding
4+
principles (in rough priority order):
55

66
* readability
77
- scan-ability
@@ -19,35 +19,11 @@ following principles (in rough priority order):
1919
* specifics
2020
- compatibility with version control practices - preserving diffs,
2121
merge-friendliness, etc.
22-
- preventing right-ward drift
22+
- preventing rightward drift
2323
- minimising vertical space
2424

2525
* application
2626
- ease of manual application
27-
- ease of implementation (in Rustfmt, and in other tools/editors/code generators)
27+
- ease of implementation (in `rustfmt`, and in other tools/editors/code generators)
2828
- internal consistency
2929
- simplicity of formatting rules
30-
31-
32-
## Overarching guidelines
33-
34-
Prefer block indent over visual indent. E.g.,
35-
36-
```rust
37-
// Block indent
38-
a_function_call(
39-
foo,
40-
bar,
41-
);
42-
43-
// Visual indent
44-
a_function_call(foo,
45-
bar);
46-
```
47-
48-
This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
49-
example) and less rightward drift.
50-
51-
Lists should have a trailing comma when followed by a newline, see the block
52-
indent example above. This choice makes moving code (e.g., by copy and paste)
53-
easier and makes smaller diffs.

Diff for: src/doc/style-guide/src/statements.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
## Statements
2+
13
### Let statements
24

35
There should be spaces after the `:` and on both sides of the `=` (if they are
4-
present). No space before the semi-colon.
6+
present). No space before the semicolon.
57

68
```rust
79
// A comment.
@@ -194,7 +196,7 @@ used to determine whether a let-else statement is *short*.
194196
### Macros in statement position
195197

196198
A macro use in statement position should use parentheses or square brackets as
197-
delimiters and should be terminated with a semi-colon. There should be no spaces
199+
delimiters and should be terminated with a semicolon. There should be no spaces
198200
between the name, `!`, the delimiters, or the `;`.
199201

200202
```rust
@@ -205,13 +207,13 @@ a_macro!(...);
205207

206208
### Expressions in statement position
207209

208-
There should be no space between the expression and the semi-colon.
210+
There should be no space between the expression and the semicolon.
209211

210212
```
211213
<expr>;
212214
```
213215

214-
All expressions in statement position should be terminated with a semi-colon,
216+
All expressions in statement position should be terminated with a semicolon,
215217
unless they end with a block or are used as the value for a block.
216218

217219
E.g.,
@@ -229,7 +231,7 @@ loop {
229231
}
230232
```
231233

232-
Use a semi-colon where an expression has void type, even if it could be
234+
Use a semicolon where an expression has void type, even if it could be
233235
propagated. E.g.,
234236

235237
```rust

0 commit comments

Comments
 (0)