|
1 |
| -rustc is slowly moving towards the [Rust standard coding style][fmt]; |
2 |
| -at the moment, however, it follows a rather more *chaotic* |
3 |
| -style. There are a few things that are always true. |
| 1 | +This file offers some tips on the coding conventions for rustc. This |
| 2 | +chapter covers [formatting](#f), [coding for correctness](#cc), |
| 3 | +[using crates from crates.io](#cio), and some tips on |
| 4 | +[structuring your PR for easy review](#er). |
4 | 5 |
|
5 |
| -[fmt]: https://github.com/rust-lang-nursery/fmt-rfcs |
| 6 | +<a name=f> |
6 | 7 |
|
7 |
| -# The tidy script |
| 8 | +# Formatting and the tidy script |
8 | 9 |
|
9 |
| -Running `./x.py test` begins with a "tidy" step. This tidy step checks |
10 |
| -that your source files meet some basic conventions. |
| 10 | +rustc is slowly moving towards the [Rust standard coding style][fmt]; |
| 11 | +at the moment, however, it follows a rather more *chaotic* style. We |
| 12 | +do have some mandatory formatting conventions, which are automatically |
| 13 | +enforced by a script we affectionately[^not_true] call the "tidy" |
| 14 | +script. The tidy script runs automatically when you do `./x.py test`. |
| 15 | + |
| 16 | +[^not_true]: Secretly, I hate tidy. -nmatsakis |
| 17 | +[fmt]: https://github.com/rust-lang-nursery/fmt-rfcs |
11 | 18 |
|
12 | 19 | <a name=copyright>
|
13 | 20 |
|
14 |
| -## Copyright notice |
| 21 | +### Copyright notice |
15 | 22 |
|
16 | 23 | All files must begin with the following copyright notice:
|
17 | 24 |
|
@@ -50,14 +57,91 @@ the copyright notice) like so:
|
50 | 57 |
|
51 | 58 | Prefer 4-space indent.
|
52 | 59 |
|
53 |
| -# Warnings and lints |
| 60 | +<a name=cc> |
| 61 | + |
| 62 | +# Coding for correctness |
| 63 | + |
| 64 | +Beyond formatting, there are a few other tips that are worth |
| 65 | +following. |
| 66 | + |
| 67 | +## Prefer exhaustive matches |
| 68 | + |
| 69 | +Using `_` in a match is convenient, but it means that when new |
| 70 | +variants are added to the enum, they may not get handled correctly. |
| 71 | +Ask yourself: if a new variant were added to this enum, what's the |
| 72 | +chance that it would want to use the `_` code, versus having some |
| 73 | +other treatment? Unless the answer is "low", then prefer an |
| 74 | +exhaustive match. (The same advice applies to `if let` and `while |
| 75 | +let`, which are effectively tests for a single variant.) |
54 | 76 |
|
55 |
| -In general, Rust crates |
| 77 | +## Use "TODO" comments for things you don't want to forget |
56 | 78 |
|
57 |
| -# Policy on using crates from crates.io |
| 79 | +As a useful tool to yourself, you can insert a `// TODO` comment |
| 80 | +for something that you want to get back to before you land your PR: |
| 81 | + |
| 82 | +```rust,ignore |
| 83 | +fn do_something() { |
| 84 | + if something_else { |
| 85 | + unimplemented!(): // TODO write this |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The tidy script will report an error for a `// TODO` comment, so this |
| 91 | +code would not be able to land until the TODO is fixed (or removed). |
| 92 | + |
| 93 | +This can also be useful in a PR as a way to signal from one commit that you are |
| 94 | +leaving a bug that a later commit will fix: |
| 95 | + |
| 96 | +```rust,ignore |
| 97 | +if foo { |
| 98 | + return true; // TODO wrong, but will be fixed in a later commit |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +<a name=cio> |
| 103 | + |
| 104 | +# Using crates from crates.io |
58 | 105 |
|
59 | 106 | It is allowed to use crates from crates.io, though external
|
60 | 107 | dependencies should not be added gratuitously. All such crates must
|
61 | 108 | have a suitably permissive license. There is an automatic check which
|
62 | 109 | inspects the Cargo metadata to ensure this.
|
63 | 110 |
|
| 111 | +<a name=er> |
| 112 | + |
| 113 | +# How to structure your PR |
| 114 | + |
| 115 | +How you prepare the commits in your PR can make a big difference for the reviewer. |
| 116 | +Here are some tips. |
| 117 | + |
| 118 | +**Isolate "pure refactorings" into their own commit.** For example, if |
| 119 | +you rename a method, then put that rename into its own commit, along |
| 120 | +with the renames of all the uses. |
| 121 | + |
| 122 | +**More commits is usually better.** If you are doing a large change, |
| 123 | +it's almost always better to break it up into smaller steps that can |
| 124 | +be independently understood. The one thing to be aware of is that if |
| 125 | +you introduce some code following one strategy, then change it |
| 126 | +dramatically (versus adding to it) in a later commit, that |
| 127 | +'back-and-forth' can be confusing. |
| 128 | + |
| 129 | +**If you run rustfmt and the file was not already formatted, isolate |
| 130 | +that into its own commit.** This is really the same as the previous |
| 131 | +rule, but it's worth highlighting. It's ok to rustfmt files, but since |
| 132 | +we do not currently run rustfmt all the time, that can introduce a lot |
| 133 | +of noise into your commit. Please isolate that into its own |
| 134 | +commit. This also makes rebases a lot less painful, since rustfmt |
| 135 | +tends to cause a lot of merge conflicts, and having those isolated |
| 136 | +into their own commit makes htem easier to resolve. |
| 137 | + |
| 138 | +**No merges.** We do not allow merge commits into our history, other |
| 139 | +than those by bors. If you get a merge conflict, rebase instead via a |
| 140 | +command like `git rebase -i rust-lang/master` (presuming you use the |
| 141 | +name `rust-lang` for your remote). |
| 142 | + |
| 143 | +**Individual commits do not have to build (but it's nice).** We do not |
| 144 | +require that every intermediate commit successfully builds -- we only |
| 145 | +expect to be able to bisect at a PR level. However, if you *can* make |
| 146 | +individual commits build, that is always helpful. |
| 147 | + |
0 commit comments