Skip to content
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

style-guide: Organizational and editing tweaks (no semantic changes) #112942

Merged
merged 15 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
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
51 changes: 43 additions & 8 deletions src/doc/style-guide/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Rust code has similar formatting, less mental effort is required to comprehend a
new project, lowering the barrier to entry for new developers.

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


## Formatting conventions
Expand All @@ -28,8 +28,47 @@ typically by using a formatting tool's default settings.
* Each level of indentation must be four spaces (that is, all indentation
outside of string literals and comments must be a multiple of four).
* The maximum width for a line is 100 characters.
* A tool should be configurable for all three of these variables.
* A tool may choose to make some of these configurable.
joshtriplett marked this conversation as resolved.
Show resolved Hide resolved

#### Block indent

Prefer block indent over visual indent:

```rust
// Block indent
a_function_call(
foo,
bar,
);

// Visual indent
a_function_call(foo,
bar);
```

This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
example) and less rightward drift.

### Trailing commas

Lists should have a trailing comma when followed by a newline:

```rust
function_call(
argument,
another_argument,
);

let array = [
element,
another_element,
yet_another_element,
];
```

This makes moving code (e.g., by copy and paste) easier, and makes diffs
smaller, as appending or removing items does not require modifying another line
to add or remove a comma.

### Blank lines

Expand All @@ -48,11 +87,7 @@ fn bar() {}
fn baz() {}
```

Formatting tools should make the bounds on blank lines configurable: there
should be separate minimum and maximum numbers of newlines between both
statements and (top-level) items (i.e., four options). As described above, the
defaults for both statements and items should be minimum: 1, maximum: 2.

Formatting tools may wish to make the bounds on blank lines configurable.

### [Module-level items](items.md)
### [Statements](statements.md)
Expand Down
8 changes: 4 additions & 4 deletions src/doc/style-guide/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[Introduction](README.md)

- [Module-level items](items.md)
- [Items](items.md)
- [Statements](statements.md)
- [Expressions](expressions.md)
- [Types](types.md)
- [Non-formatting conventions](advice.md)
- [Types and Bounds](types.md)
- [Other style advice](advice.md)
- [`Cargo.toml` conventions](cargo.md)
- [Principles used for deciding these guidelines](principles.md)
- [Guiding principles and rationale](principles.md)
6 changes: 3 additions & 3 deletions src/doc/style-guide/src/advice.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ if y {
* Local variables shall be `snake_case`,
* Macro names shall be `snake_case`,
* Constants (`const`s and immutable `static`s) shall be `SCREAMING_SNAKE_CASE`.
* When a name is forbidden because it is a reserved word (e.g., `crate`), use a
trailing underscore to make the name legal (e.g., `crate_`), or use raw
identifiers if possible.
* When a name is forbidden because it is a reserved word (such as `crate`),
either use a raw identifier (`r#crate`) or use a trailing underscore
(`crate_`). Don't misspell the word (`krate`).
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved

### Modules

Expand Down
21 changes: 11 additions & 10 deletions src/doc/style-guide/src/cargo.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cargo.toml conventions
# `Cargo.toml` conventions

## Formatting conventions

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

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

```rust
authors = [
"A Uthor <[email protected]>",
"Another Author <[email protected]>",
some_feature = [
"another_feature",
"yet_another_feature",
"some_dependency?/some_feature",
]
```

Expand All @@ -54,11 +55,11 @@ version = "4.5.6"

## Metadata conventions

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

The license field must contain a valid [SPDX
expression](https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60),
Expand Down
30 changes: 16 additions & 14 deletions src/doc/style-guide/src/items.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Items

Items consist of the set of things permitted at the top level of a module.
However, Rust also allows some items to appear within some other types of
items, such as within a function. The same formatting conventions apply whether
an item appears at module level or within another item.

`extern crate` statements must be first in a file. They must be ordered
alphabetically.

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

### Function definitions

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

The proper ordering and spacing is:

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

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

Format each variant accordingly as either a struct, tuple struct, or identifier,
which doesn't require special formatting (but without the `struct` keyword.
Format each variant accordingly as either a struct (but without the `struct`
keyword), a tuple struct, or an identifier (which doesn't require special
formatting):

```rust
enum FooBar {
Expand Down Expand Up @@ -139,7 +145,7 @@ union Foo {

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

```rust
pub struct Foo(String, u8);
Expand Down Expand Up @@ -230,7 +236,7 @@ impl Bar

`extern crate foo;`

Use spaces around keywords, no spaces around the semi-colon.
Use spaces around keywords, no spaces around the semicolon.


### Modules
Expand All @@ -245,7 +251,7 @@ mod foo;
```

Use spaces around keywords and before the opening brace, no spaces around the
semi-colon.
semicolon.

### macro\_rules!

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

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


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

Note that tools which only have access to syntax (such as Rustfmt) cannot tell
which imports are from an external crate or the std lib, etc.


#### Ordering list import

Names in a list import must be sorted ascii-betically, but with `self` and
Names in a list import must be sorted ASCIIbetically, but with `self` and
`super` first, and groups and glob imports last. This applies recursively. For
example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
`use foo::bar::{a, b::c, b::d, b::d::{x, y, z}, b::{self, r, s}};`.
Expand Down
32 changes: 4 additions & 28 deletions src/doc/style-guide/src/principles.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Guiding principles and rationale

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

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

* application
- ease of manual application
- ease of implementation (in Rustfmt, and in other tools/editors/code generators)
- ease of implementation (in `rustfmt`, and in other tools/editors/code generators)
- internal consistency
- simplicity of formatting rules


## Overarching guidelines

Prefer block indent over visual indent. E.g.,

```rust
// Block indent
a_function_call(
foo,
bar,
);

// Visual indent
a_function_call(foo,
bar);
```

This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
example) and less rightward drift.

Lists should have a trailing comma when followed by a newline, see the block
indent example above. This choice makes moving code (e.g., by copy and paste)
easier and makes smaller diffs.
12 changes: 7 additions & 5 deletions src/doc/style-guide/src/statements.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## Statements

### Let statements

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

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

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

```rust
Expand All @@ -205,13 +207,13 @@ a_macro!(...);

### Expressions in statement position

There should be no space between the expression and the semi-colon.
There should be no space between the expression and the semicolon.

```
<expr>;
```

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

E.g.,
Expand All @@ -229,7 +231,7 @@ loop {
}
```

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

```rust
Expand Down