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: Document formatting of as casts (mostly like a binary operator) #114394

Merged
merged 1 commit into from
Sep 21, 2023
Merged
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
28 changes: 20 additions & 8 deletions src/doc/style-guide/src/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -421,14 +441,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
Expand Down