11# Coding conventions
22
3- This file offers some tips on the coding conventions for rustc. This
4- chapter covers [ formatting] ( #formatting ) , [ coding for correctness] ( #cc ) ,
5- [ using crates from crates.io] ( #cio ) , and some tips on
6- [ structuring your PR for easy review] ( #er ) .
3+ This file offers some tips on the coding conventions for rustc.
4+ This chapter covers [ formatting] ( #formatting ) , [ coding for correctness] ( #cc ) ,
5+ [ using crates from crates.io] ( #cio ) , and some tips on [ structuring your PR for easy review] ( #er ) .
76
87<a id =" formatting " ></a >
98
@@ -15,11 +14,11 @@ However, for now we don't use stable `rustfmt`; we use a pinned version with a
1514special config, so this may result in different style from normal [ ` rustfmt ` ] .
1615Therefore, formatting this repository using ` cargo fmt ` is not recommended.
1716
18- Instead, formatting should be done using ` ./x fmt ` . It's a good habit to run
19- ` ./x fmt ` before every commit, as this reduces conflicts later.
17+ Instead, formatting should be done using ` ./x fmt ` .
18+ It's a good habit to run ` ./x fmt ` before every commit, as this reduces conflicts later.
2019
21- Formatting is checked by the ` tidy ` script. It runs automatically when you do
22- ` ./x test ` and can be run in isolation with ` ./x fmt --check ` .
20+ Formatting is checked by the ` tidy ` script.
21+ It runs automatically when you do ` ./x test ` and can be run in isolation with ` ./x fmt --check ` .
2322
2423> ** Note: Formatting and test suites**
2524>
@@ -47,13 +46,12 @@ When modifying that code, use this command to format it:
4746./x test tidy --extra-checks cpp:fmt --bless
4847```
4948
50- This uses a pinned version of ` clang-format ` , to avoid relying on the local
51- environment.
49+ This uses a pinned version of ` clang-format ` , to avoid relying on the local environment.
5250
5351### Formatting and linting Python code
5452
55- The Rust repository contains quite a lot of Python code. We try to keep
56- it both linted and formatted by the [ ruff] tool.
53+ The Rust repository contains quite a lot of Python code.
54+ We try to keep it both linted and formatted by the [ ruff] tool.
5755
5856When modifying Python code, use this command to format it:
5957
@@ -78,17 +76,17 @@ These use a pinned version of `ruff`, to avoid relying on the local environment.
7876### Copyright notice
7977<!-- REUSE-IgnoreEnd -->
8078
81- In the past, files began with a copyright and license notice. Please ** omit **
82- this notice for new files licensed under the standard terms (dual
79+ In the past, files began with a copyright and license notice.
80+ Please ** omit ** this notice for new files licensed under the standard terms (dual
8381MIT/Apache-2.0).
8482
8583All of the copyright notices should be gone by now, but if you come across one
8684in the rust-lang/rust repo, feel free to open a PR to remove it.
8785
8886### Line length
8987
90- Lines should be at most 100 characters. It's even better if you can
91- keep things to 80.
88+ Lines should be at most 100 characters.
89+ It's even better if you can keep things to 80.
9290
9391Sometimes, and particularly for tests, it can be necessary to exempt yourself from this limit.
9492In that case, you can add a comment towards the top of the file like so:
@@ -105,17 +103,15 @@ Prefer 4-space indents.
105103
106104## Coding for correctness
107105
108- Beyond formatting, there are a few other tips that are worth
109- following.
106+ Beyond formatting, there are a few other tips that are worth following.
110107
111108### Prefer exhaustive matches
112109
113110Using ` _ ` in a match is convenient, but it means that when new
114111variants are added to the enum, they may not get handled correctly.
115112Ask yourself: if a new variant were added to this enum, what's the
116- chance that it would want to use the ` _ ` code, versus having some
117- other treatment? Unless the answer is "low", then prefer an
118- exhaustive match.
113+ chance that it would want to use the ` _ ` code, versus having some other treatment?
114+ Unless the answer is "low", then prefer an exhaustive match.
119115
120116The same advice applies to ` if let ` and ` while let ` ,
121117which are effectively tests for a single variant.
@@ -155,41 +151,39 @@ See the [crates.io dependencies][crates] section.
155151
156152## How to structure your PR
157153
158- How you prepare the commits in your PR can make a big difference for the
159- reviewer. Here are some tips.
154+ How you prepare the commits in your PR can make a big difference for the reviewer.
155+ Here are some tips.
160156
161157** Isolate "pure refactorings" into their own commit.** For example, if
162158you rename a method, then put that rename into its own commit, along
163159with the renames of all the uses.
164160
165161** More commits is usually better.** If you are doing a large change,
166- it's almost always better to break it up into smaller steps that can
167- be independently understood. The one thing to be aware of is that if
162+ it's almost always better to break it up into smaller steps that can be independently understood.
163+ The one thing to be aware of is that if
168164you introduce some code following one strategy, then change it
169- dramatically (versus adding to it) in a later commit, that
170- 'back-and-forth' can be confusing.
165+ dramatically (versus adding to it) in a later commit, that 'back-and-forth' can be confusing.
171166
172167** Format liberally.** While only the final commit of a PR must be correctly
173168formatted, it is both easier to review and less noisy to format each commit
174169individually using ` ./x fmt ` .
175170
176- ** No merges.** We do not allow merge commits into our history, other
177- than those by bors. If you get a merge conflict, rebase instead via a
171+ ** No merges.** We do not allow merge commits into our history, other than those by bors.
172+ If you get a merge conflict, rebase instead via a
178173command like ` git rebase --interactive rust-lang/main ` (presuming you use the
179174name ` rust-lang ` for your remote).
180175
181176** Individual commits do not have to build (but it's nice).** We do not
182177require that every intermediate commit successfully builds – we only
183- expect to be able to bisect at a PR level. However, if you * can * make
184- individual commits build, that is always helpful.
178+ expect to be able to bisect at a PR level.
179+ However, if you * can * make individual commits build, that is always helpful.
185180
186181## Naming conventions
187182
188- Apart from normal Rust style/naming conventions, there are also some specific
189- to the compiler.
183+ Apart from normal Rust style/naming conventions, there are also some specific to the compiler.
190184
191- - ` cx ` tends to be short for "context" and is often used as a suffix. For
192- example, ` tcx ` is a common name for the [ Typing Context] [ tcx ] .
185+ - ` cx ` tends to be short for "context" and is often used as a suffix.
186+ For example, ` tcx ` is a common name for the [ Typing Context] [ tcx ] .
193187
194188- [ ` 'tcx ` ] [ tcx ] is used as the lifetime name for the Typing Context.
195189
0 commit comments