1
+ # Coding conventions
2
+
1
3
This file offers some tips on the coding conventions for rustc. This
2
4
chapter covers [ formatting] ( #formatting ) , [ coding for correctness] ( #cc ) ,
3
5
[ using crates from crates.io] ( #cio ) , and some tips on
4
6
[ structuring your PR for easy review] ( #er ) .
5
7
6
8
<a id =" formatting " ></a >
7
9
8
- # Formatting and the tidy script
10
+ ## Formatting and the tidy script
9
11
10
12
rustc is moving towards the [ Rust standard coding style] [ fmt ] .
11
13
@@ -20,52 +22,50 @@ Formatting is checked by the `tidy` script. It runs automatically when you do
20
22
` ./x test ` and can be run in isolation with ` ./x fmt --check ` .
21
23
22
24
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 ` .
26
26
27
27
[ fmt ] : https://github.com/rust-dev-tools/fmt-rfcs
28
-
29
28
[ `rustfmt` ] :https://github.com/rust-lang/rustfmt
30
29
31
- ## Formatting C++ code
30
+ ### Formatting C++ code
32
31
33
32
The compiler contains some C++ code for interfacing with parts of LLVM that
34
33
don't have a stable C API.
35
34
When modifying that code, use this command to format it:
36
35
37
- ``` sh
38
- ./x test tidy --extra-checks= cpp:fmt --bless
36
+ ``` console
37
+ ./x test tidy --extra-checks cpp:fmt --bless
39
38
```
40
39
41
40
This uses a pinned version of ` clang-format ` , to avoid relying on the local
42
41
environment.
43
42
44
- ## Formatting and linting Python code
43
+ ### Formatting and linting Python code
45
44
46
45
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.
48
47
49
48
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
52
52
```
53
53
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
57
58
```
58
59
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.
61
61
62
62
[ ruff ] : https://github.com/astral-sh/ruff
63
63
64
64
<a id =" copyright " ></a >
65
65
66
66
<!-- REUSE-IgnoreStart -->
67
67
<!-- Prevent REUSE from interpreting the heading as a copyright notice -->
68
- ## Copyright notice
68
+ ### Copyright notice
69
69
<!-- REUSE-IgnoreEnd -->
70
70
71
71
In the past, files began with a copyright and license notice. Please ** omit**
@@ -75,41 +75,42 @@ MIT/Apache-2.0).
75
75
All of the copyright notices should be gone by now, but if you come across one
76
76
in the rust-lang/rust repo, feel free to open a PR to remove it.
77
77
78
- ## Line length
78
+ ### Line length
79
79
80
80
Lines should be at most 100 characters. It's even better if you can
81
81
keep things to 80.
82
82
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:
86
85
87
86
``` rust
88
87
// ignore-tidy-linelength
89
88
```
90
89
91
- ## Tabs vs spaces
90
+ ### Tabs vs spaces
92
91
93
- Prefer 4-space indent .
92
+ Prefer 4-space indents .
94
93
95
94
<a id =" cc " ></a >
96
95
97
- # Coding for correctness
96
+ ## Coding for correctness
98
97
99
98
Beyond formatting, there are a few other tips that are worth
100
99
following.
101
100
102
- ## Prefer exhaustive matches
101
+ ### Prefer exhaustive matches
103
102
104
103
Using ` _ ` in a match is convenient, but it means that when new
105
104
variants are added to the enum, they may not get handled correctly.
106
105
Ask yourself: if a new variant were added to this enum, what's the
107
106
chance that it would want to use the ` _ ` code, versus having some
108
107
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.
111
112
112
- ## Use "TODO" comments for things you don't want to forget
113
+ ### Use "TODO" comments for things you don't want to forget
113
114
114
115
As a useful tool to yourself, you can insert a ` // TODO ` comment
115
116
for something that you want to get back to before you land your PR:
@@ -136,13 +137,13 @@ if foo {
136
137
137
138
<a id =" cio " ></a >
138
139
139
- # Using crates from crates.io
140
+ ## Using crates from crates.io
140
141
141
142
See the [ crates.io dependencies] [ crates ] section.
142
143
143
144
<a id =" er " ></a >
144
145
145
- # How to structure your PR
146
+ ## How to structure your PR
146
147
147
148
How you prepare the commits in your PR can make a big difference for the
148
149
reviewer. Here are some tips.
@@ -172,7 +173,7 @@ require that every intermediate commit successfully builds – we only
172
173
expect to be able to bisect at a PR level. However, if you * can* make
173
174
individual commits build, that is always helpful.
174
175
175
- # Naming conventions
176
+ ## Naming conventions
176
177
177
178
Apart from normal Rust style/naming conventions, there are also some specific
178
179
to the compiler.
0 commit comments