1 This file offers some tips on the coding conventions for rustc. This
2 chapter covers [formatting](#formatting), [coding for correctness](#cc),
3 [using crates from crates.io](#cio), and some tips on
4 [structuring your PR for easy review](#er).
6 <a name="formatting"></a>
8 # Formatting and the tidy script
10 rustc is moving towards the [Rust standard coding style][fmt].
11 This is enforced by the "tidy" script and can be mostly
12 automated using `./x.py fmt`.
14 As the output of [rustfmt] is not completely stable,
15 formatting this repository using `cargo fmt` is not recommended.
17 The tidy script runs automatically when you do `./x.py test` and can be run
18 in isolation with `./x.py test tidy`.
20 [fmt]: https://github.com/rust-dev-tools/fmt-rfcs
21 [rustfmt]:https://github.com/rust-lang/rustfmt
23 <a name="copyright"></a>
27 In the past, files began with a copyright and license notice. Please **omit**
28 this notice for new files licensed under the standard terms (dual
31 All of the copyright notices should be gone by now, but if you come across one
32 in the rust-lang/rust repo, feel free to open a PR to remove it.
36 Lines should be at most 100 characters. It's even better if you can
39 **Ignoring the line length limit.** Sometimes – in particular for
40 tests – it can be necessary to exempt yourself from this limit. In
41 that case, you can add a comment towards the top of the file like so:
44 // ignore-tidy-linelength
49 Prefer 4-space indent.
53 # Coding for correctness
55 Beyond formatting, there are a few other tips that are worth
58 ## Prefer exhaustive matches
60 Using `_` in a match is convenient, but it means that when new
61 variants are added to the enum, they may not get handled correctly.
62 Ask yourself: if a new variant were added to this enum, what's the
63 chance that it would want to use the `_` code, versus having some
64 other treatment? Unless the answer is "low", then prefer an
65 exhaustive match. (The same advice applies to `if let` and `while
66 let`, which are effectively tests for a single variant.)
68 ## Use "TODO" comments for things you don't want to forget
70 As a useful tool to yourself, you can insert a `// TODO` comment
71 for something that you want to get back to before you land your PR:
76 unimplemented!(); // TODO write this
81 The tidy script will report an error for a `// TODO` comment, so this
82 code would not be able to land until the TODO is fixed (or removed).
84 This can also be useful in a PR as a way to signal from one commit that you are
85 leaving a bug that a later commit will fix:
89 return true; // TODO wrong, but will be fixed in a later commit
95 # Using crates from crates.io
97 It is allowed to use crates from crates.io, though external
98 dependencies should not be added gratuitously. All such crates must
99 have a suitably permissive license. There is an automatic check which
100 inspects the Cargo metadata to ensure this.
104 # How to structure your PR
106 How you prepare the commits in your PR can make a big difference for the
107 reviewer. Here are some tips.
109 **Isolate "pure refactorings" into their own commit.** For example, if
110 you rename a method, then put that rename into its own commit, along
111 with the renames of all the uses.
113 **More commits is usually better.** If you are doing a large change,
114 it's almost always better to break it up into smaller steps that can
115 be independently understood. The one thing to be aware of is that if
116 you introduce some code following one strategy, then change it
117 dramatically (versus adding to it) in a later commit, that
118 'back-and-forth' can be confusing.
120 **Format liberally.** While only the final commit of a PR must be correctly
121 formatted, it is both easier to review and less noisy to format each commit
122 individually using `./x.py fmt`.
124 **No merges.** We do not allow merge commits into our history, other
125 than those by bors. If you get a merge conflict, rebase instead via a
126 command like `git rebase -i rust-lang/master` (presuming you use the
127 name `rust-lang` for your remote).
129 **Individual commits do not have to build (but it's nice).** We do not
130 require that every intermediate commit successfully builds – we only
131 expect to be able to bisect at a PR level. However, if you *can* make
132 individual commits build, that is always helpful.
136 Apart from normal Rust style/naming conventions, there are also some specific
139 - `cx` tends to be short for "context" and is often used as a suffix. For
140 example, `tcx` is a common name for the [Typing Context][tcx].
142 - [`'tcx`][tcx] is used as the lifetime name for the Typing Context.
144 - Because `crate` is a keyword, if you need a variable to represent something
145 crate-related, often the spelling is changed to `krate`.