diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
commit | e02c5b5930c2c9ba3e5423fe12e2ef0155017297 (patch) | |
tree | fd60ebbbb5299e16e5fca8c773ddb74f764760db /src/doc/style-guide | |
parent | Adding debian version 1.73.0+dfsg1-1. (diff) | |
download | rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.tar.xz rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/style-guide')
-rw-r--r-- | src/doc/style-guide/src/README.md | 13 | ||||
-rw-r--r-- | src/doc/style-guide/src/expressions.md | 35 | ||||
-rw-r--r-- | src/doc/style-guide/src/items.md | 38 |
3 files changed, 71 insertions, 15 deletions
diff --git a/src/doc/style-guide/src/README.md b/src/doc/style-guide/src/README.md index f4d759673..9a59d8055 100644 --- a/src/doc/style-guide/src/README.md +++ b/src/doc/style-guide/src/README.md @@ -32,6 +32,19 @@ This should not be interpreted as forbidding developers from following a non-default style, or forbidding tools from adding any particular configuration options. +## Bugs + +If the style guide differs from rustfmt, that may represent a bug in rustfmt, +or a bug in the style guide; either way, please report it to the style team or +the rustfmt team or both, for investigation and fix. + +If implementing a new formatting tool based on the style guide and default Rust +style, please test it on the corpus of existing Rust code, and avoid causing +widespread breakage. The implementation and testing of such a tool may surface +bugs in either the style guide or rustfmt, as well as bugs in the tool itself. + +We typically resolve bugs in a fashion that avoids widespread breakage. + ## Formatting conventions ### Indentation and line width diff --git a/src/doc/style-guide/src/expressions.md b/src/doc/style-guide/src/expressions.md index 32c604f9f..12037b599 100644 --- a/src/doc/style-guide/src/expressions.md +++ b/src/doc/style-guide/src/expressions.md @@ -328,6 +328,26 @@ foo_bar Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather than at other binary operators. +### Casts (`as`) + +Format `as` casts like a binary operator. In particular, always include spaces +around `as`, and if line-breaking, break before the `as` (never after) and +block-indent the subsequent line. Format the type on the right-hand side using +the rules for types. + +However, unlike with other binary operators, if chaining a series of `as` casts +that require line-breaking, and line-breaking before the first `as` suffices to +make the remainder fit on the next line, don't break before any subsequent +`as`; instead, leave the series of types all on the same line: + +```rust +let cstr = very_long_expression() + as *const str as *const [u8] as *const std::os::raw::c_char; +``` + +If the subsequent line still requires line-breaking, break and block-indent +before each `as` as with other binary operators. + ## Control flow Do not include extraneous parentheses for `if` and `while` expressions. @@ -400,7 +420,12 @@ constructs. For example, a macro use `foo!(a, b, c)` can be parsed like a function call (ignoring the `!`), so format it using the rules for function calls. -### Special case macros +The style guide defines specific formatting for particular macros in the +language or standard library. The style guide does not define formatting for +any third-party macros, even if similar to those in the language or standard +library. + +### Format string macros For macros which take a format string, if all other arguments are *small*, format the arguments before the format string on a single line if they fit, and @@ -421,14 +446,6 @@ assert_eq!( ); ``` -## Casts (`as`) - -Put spaces before and after `as`: - -```rust -let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char; -``` - ## Chains of fields and method calls A chain is a sequence of field accesses, method calls, and/or uses of the try diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index a6d941f6d..b215de6ad 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -367,26 +367,52 @@ where ## Type aliases Keep type aliases on one line when they fit. If necessary to break the line, do -so after the `=`, and block-indent the right-hand side: +so before the `=`, and block-indent the right-hand side: ```rust pub type Foo = Bar<T>; // If multi-line is required -type VeryLongType<T, U: SomeBound> = - AnEvenLongerType<T, U, Foo<T>>; +type VeryLongType<T, U: SomeBound> + = AnEvenLongerType<T, U, Foo<T>>; ``` -Where possible avoid `where` clauses and keep type constraints inline. Where -that is not possible split the line before and after the `where` clause (and -split the `where` clause as normal), e.g., +When there is a trailing `where` clause after the type, and no `where` clause +present before the type, break before the `=` and indent. Then break before the +`where` keyword and format the clauses normally, e.g., ```rust +// With only a trailing where clause type VeryLongType<T, U> + = AnEvenLongerType<T, U, Foo<T>> +where + T: U::AnAssociatedType, + U: SomeBound; +``` + +When there is a `where` clause before the type, format it normally, and break +after the last clause. Do not indent before the `=` to leave it visually +distinct from the indented clauses that precede it. If there is additionally a +`where` clause after the type, break before the `where` keyword and format the +clauses normally. + +```rust +// With only a preceding where clause. +type WithPrecedingWC<T, U> where T: U::AnAssociatedType, U: SomeBound, = AnEvenLongerType<T, U, Foo<T>>; + +// Or with both a preceding and trailing where clause. +type WithPrecedingWC<T, U> +where + T: U::AnAssociatedType, + U: SomeBound, += AnEvenLongerType<T, U, Foo<T>> +where + T: U::AnAssociatedType2, + U: SomeBound2; ``` ## Associated types |