Skip to content

Commit 85c7964

Browse files
authored
Merge pull request #2333 from rust-lang/convetions-chapt-cleaning
Coding conventions chapter cleaning
2 parents 78a5c64 + 07d3c69 commit 85c7964

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/conventions.md

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# Coding conventions
2+
13
This file offers some tips on the coding conventions for rustc. This
24
chapter covers [formatting](#formatting), [coding for correctness](#cc),
35
[using crates from crates.io](#cio), and some tips on
46
[structuring your PR for easy review](#er).
57

68
<a id="formatting"></a>
79

8-
# Formatting and the tidy script
10+
## Formatting and the tidy script
911

1012
rustc is moving towards the [Rust standard coding style][fmt].
1113

@@ -20,52 +22,50 @@ Formatting is checked by the `tidy` script. It runs automatically when you do
2022
`./x test` and can be run in isolation with `./x fmt --check`.
2123

2224
If you want to use format-on-save in your editor, the pinned version of
23-
`rustfmt` is built under `build/<target>/stage0/bin/rustfmt`. You'll have to
24-
pass the <!-- date-check: nov 2022 --> `--edition=2021` argument yourself when calling
25-
`rustfmt` directly.
25+
`rustfmt` is built under `build/<target>/stage0/bin/rustfmt`.
2626

2727
[fmt]: https://github.com/rust-dev-tools/fmt-rfcs
28-
2928
[`rustfmt`]:https://github.com/rust-lang/rustfmt
3029

31-
## Formatting C++ code
30+
### Formatting C++ code
3231

3332
The compiler contains some C++ code for interfacing with parts of LLVM that
3433
don't have a stable C API.
3534
When modifying that code, use this command to format it:
3635

37-
```sh
38-
./x test tidy --extra-checks=cpp:fmt --bless
36+
```console
37+
./x test tidy --extra-checks cpp:fmt --bless
3938
```
4039

4140
This uses a pinned version of `clang-format`, to avoid relying on the local
4241
environment.
4342

44-
## Formatting and linting Python code
43+
### Formatting and linting Python code
4544

4645
The Rust repository contains quite a lot of Python code. We try to keep
47-
it both linted and formatted by the [ruff][ruff] tool.
46+
it both linted and formatted by the [ruff] tool.
4847

4948
When modifying Python code, use this command to format it:
50-
```sh
51-
./x test tidy --extra-checks=py:fmt --bless
49+
50+
```console
51+
./x test tidy --extra-checks py:fmt --bless
5252
```
5353

54-
and the following command to run lints:
55-
```sh
56-
./x test tidy --extra-checks=py:lint
54+
And, the following command to run lints:
55+
56+
```console
57+
./x test tidy --extra-checks py:lint
5758
```
5859

59-
This uses a pinned version of `ruff`, to avoid relying on the local
60-
environment.
60+
These use a pinned version of `ruff`, to avoid relying on the local environment.
6161

6262
[ruff]: https://github.com/astral-sh/ruff
6363

6464
<a id="copyright"></a>
6565

6666
<!-- REUSE-IgnoreStart -->
6767
<!-- Prevent REUSE from interpreting the heading as a copyright notice -->
68-
## Copyright notice
68+
### Copyright notice
6969
<!-- REUSE-IgnoreEnd -->
7070

7171
In the past, files began with a copyright and license notice. Please **omit**
@@ -75,41 +75,42 @@ MIT/Apache-2.0).
7575
All of the copyright notices should be gone by now, but if you come across one
7676
in the rust-lang/rust repo, feel free to open a PR to remove it.
7777

78-
## Line length
78+
### Line length
7979

8080
Lines should be at most 100 characters. It's even better if you can
8181
keep things to 80.
8282

83-
**Ignoring the line length limit.** Sometimes – in particular for
84-
tests – it can be necessary to exempt yourself from this limit. In
85-
that case, you can add a comment towards the top of the file like so:
83+
Sometimes, and particularly for tests, it can be necessary to exempt yourself from this limit.
84+
In that case, you can add a comment towards the top of the file like so:
8685

8786
```rust
8887
// ignore-tidy-linelength
8988
```
9089

91-
## Tabs vs spaces
90+
### Tabs vs spaces
9291

93-
Prefer 4-space indent.
92+
Prefer 4-space indents.
9493

9594
<a id="cc"></a>
9695

97-
# Coding for correctness
96+
## Coding for correctness
9897

9998
Beyond formatting, there are a few other tips that are worth
10099
following.
101100

102-
## Prefer exhaustive matches
101+
### Prefer exhaustive matches
103102

104103
Using `_` in a match is convenient, but it means that when new
105104
variants are added to the enum, they may not get handled correctly.
106105
Ask yourself: if a new variant were added to this enum, what's the
107106
chance that it would want to use the `_` code, versus having some
108107
other treatment? Unless the answer is "low", then prefer an
109-
exhaustive match. (The same advice applies to `if let` and `while
110-
let`, which are effectively tests for a single variant.)
108+
exhaustive match.
109+
110+
The same advice applies to `if let` and `while let`,
111+
which are effectively tests for a single variant.
111112

112-
## Use "TODO" comments for things you don't want to forget
113+
### Use "TODO" comments for things you don't want to forget
113114

114115
As a useful tool to yourself, you can insert a `// TODO` comment
115116
for something that you want to get back to before you land your PR:
@@ -136,13 +137,13 @@ if foo {
136137

137138
<a id="cio"></a>
138139

139-
# Using crates from crates.io
140+
## Using crates from crates.io
140141

141142
See the [crates.io dependencies][crates] section.
142143

143144
<a id="er"></a>
144145

145-
# How to structure your PR
146+
## How to structure your PR
146147

147148
How you prepare the commits in your PR can make a big difference for the
148149
reviewer. Here are some tips.
@@ -172,7 +173,7 @@ require that every intermediate commit successfully builds – we only
172173
expect to be able to bisect at a PR level. However, if you *can* make
173174
individual commits build, that is always helpful.
174175

175-
# Naming conventions
176+
## Naming conventions
176177

177178
Apart from normal Rust style/naming conventions, there are also some specific
178179
to the compiler.

0 commit comments

Comments
 (0)