1+ # Coding conventions
2+
13This file offers some tips on the coding conventions for rustc. This
24chapter 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
1012rustc 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
2224If 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
3332The compiler contains some C++ code for interfacing with parts of LLVM that
3433don't have a stable C API.
3534When 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
4140This uses a pinned version of ` clang-format ` , to avoid relying on the local
4241environment.
4342
44- ## Formatting and linting Python code
43+ ### Formatting and linting Python code
4544
4645The 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
4948When 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
7171In the past, files began with a copyright and license notice. Please ** omit**
@@ -75,41 +75,42 @@ MIT/Apache-2.0).
7575All of the copyright notices should be gone by now, but if you come across one
7676in the rust-lang/rust repo, feel free to open a PR to remove it.
7777
78- ## Line length
78+ ### Line length
7979
8080Lines should be at most 100 characters. It's even better if you can
8181keep 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
9998Beyond formatting, there are a few other tips that are worth
10099following.
101100
102- ## Prefer exhaustive matches
101+ ### Prefer exhaustive matches
103102
104103Using ` _ ` in a match is convenient, but it means that when new
105104variants are added to the enum, they may not get handled correctly.
106105Ask yourself: if a new variant were added to this enum, what's the
107106chance that it would want to use the ` _ ` code, versus having some
108107other 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
114115As a useful tool to yourself, you can insert a ` // TODO ` comment
115116for 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
141142See 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
147148How you prepare the commits in your PR can make a big difference for the
148149reviewer. Here are some tips.
@@ -172,7 +173,7 @@ require that every intermediate commit successfully builds – we only
172173expect to be able to bisect at a PR level. However, if you * can* make
173174individual commits build, that is always helpful.
174175
175- # Naming conventions
176+ ## Naming conventions
176177
177178Apart from normal Rust style/naming conventions, there are also some specific
178179to the compiler.
0 commit comments