]> git.lizzy.rs Git - rust.git/blob - CONTRIBUTING.md
Update for rust-lang/rust#50536
[rust.git] / CONTRIBUTING.md
1 # Contributing to rust-clippy
2
3 Hello fellow Rustacean! Great to see your interest in compiler internals and lints!
4
5 Clippy welcomes contributions from everyone. There are many ways to contribute to Clippy and the following document explains how
6 you can contribute and how to get started.
7 If you have any questions about contributing or need help with anything, feel free to ask questions on issues or
8 visit the `#clippy` IRC channel on `irc.mozilla.org`.
9
10 All contributors are expected to follow the [Rust Code of Conduct](http://www.rust-lang.org/conduct.html).
11
12 * [Getting started](#getting-started)
13   * [Finding something to fix/improve](#finding-something-to-fiximprove)
14 * [Writing code](#writing-code)
15   * [Author lint](#author-lint)
16   * [Documentation](#documentation)
17   * [Running test suite](#running-test-suite)
18   * [Testing manually](#testing-manually)
19   * [How Clippy works](#how-clippy-works)
20   * [Fixing nightly build failures](#fixing-nightly-build-failures)
21 * [Contributions](#contributions)
22
23 ## Getting started
24
25 High level approach:
26
27 1. Find something to fix/improve
28 2. Change code (likely some file in `clippy_lints/src/`)
29 3. Run `cargo test` in the root directory and wiggle code until it passes
30 4. Open a PR (also can be done between 2. and 3. if you run into problems)
31
32 ### Finding something to fix/improve
33
34 All issues on Clippy are mentored, if you want help with a bug just ask @Manishearth, @llogiq, @mcarton or @oli-obk.
35
36 Some issues are easier than others. The [`good first issue`](https://github.com/rust-lang-nursery/rust-clippy/labels/good%20first%20issue)
37 label can be used to find the easy issues. If you want to work on an issue, please leave a comment
38 so that we can assign it to you!
39
40 Issues marked [`T-AST`](https://github.com/rust-lang-nursery/rust-clippy/labels/T-AST) involve simple
41 matching of the syntax tree structure, and are generally easier than
42 [`T-middle`](https://github.com/rust-lang-nursery/rust-clippy/labels/T-middle) issues, which involve types
43 and resolved paths.
44
45 [`T-AST`](https://github.com/rust-lang-nursery/rust-clippy/labels/T-AST) issues will generally need you to match against a predefined syntax structure. To figure out
46 how this syntax structure is encoded in the AST, it is recommended to run `rustc -Z ast-json` on an
47 example of the structure and compare with the
48 [nodes in the AST docs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast). Usually
49 the lint will end up to be a nested series of matches and ifs,
50 [like so](https://github.com/rust-lang-nursery/rust-clippy/blob/de5ccdfab68a5e37689f3c950ed1532ba9d652a0/src/misc.rs#L34).
51
52 [`E-medium`](https://github.com/rust-lang-nursery/rust-clippy/labels/E-medium) issues are generally
53 pretty easy too, though it's recommended you work on an E-easy issue first. They are mostly classified
54 as `E-medium`, since they might be somewhat involved code wise, but not difficult per-se.
55
56 [`T-middle`](https://github.com/rust-lang-nursery/rust-clippy/labels/T-middle) issues can
57 be more involved and require verifying types. The
58 [`ty`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty) module contains a
59 lot of methods that are useful, though one of the most useful would be `expr_ty` (gives the type of
60 an AST expression). `match_def_path()` in Clippy's `utils` module can also be useful.
61
62 ## Writing code
63
64 Compiling clippy from scratch can take almost a minute or more depending on your machine.
65 However, since Rust 1.24.0 incremental compilation is enabled by default and compile times for small changes should be quick.
66
67 [Llogiq's blog post on lints](https://llogiq.github.io/2015/06/04/workflows.html) is a nice primer
68 to lint-writing, though it does get into advanced stuff. Most lints consist of an implementation of
69 `LintPass` with one or more of its default methods overridden. See the existing lints for examples
70 of this.
71
72
73 ### Author lint
74
75 There is also the internal `author` lint to generate clippy code that detects the offending pattern. It does not work for all of the Rust syntax, but can give a good starting point.
76
77 First, create a new UI test file in the `tests/ui/` directory with the pattern you want to match:
78
79 ```rust
80 // ./tests/ui/my_lint.rs
81
82 // The custom_attribute needs to be enabled for the author lint to work
83 #![feature(plugin, custom_attribute)]
84
85 fn main() {
86     #[clippy(author)]
87     let arr: [i32; 1] = [7]; // Replace line with the code you want to match
88 }
89 ```
90
91 Now you run `TESTNAME=ui/my_lint cargo test --test compile-test` to produce
92 a `.stdout` file with the generated code:
93
94 ```rust
95 // ./tests/ui/my_lint.stdout
96
97 if_chain! {
98     if let Expr_::ExprArray(ref elements) = stmt.node;
99     if elements.len() == 1;
100     if let Expr_::ExprLit(ref lit) = elements[0].node;
101     if let LitKind::Int(7, _) = lit.node;
102     then {
103         // report your lint here
104     }
105 }
106 ```
107
108 If the command was executed successfully, you can copy the code over to where you are implementing your lint.
109
110 ### Documentation
111
112 Please document your lint with a doc comment akin to the following:
113
114 ```rust
115 /// **What it does:** Checks for ... (describe what the lint matches).
116 ///
117 /// **Why is this bad?** Supply the reason for linting the code.
118 ///
119 /// **Known problems:** None. (Or describe where it could go wrong.)
120 ///
121 /// **Example:**
122 ///
123 /// ```rust
124 /// // Bad
125 /// Insert a short example of code that triggers the lint
126 ///
127 /// // Good
128 /// Insert a short example of improved code that doesn't trigger the lint
129 /// ```
130 ```
131
132 Once your lint is merged it will show up in the [lint list](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
133
134 ### Running test suite
135
136 Use `cargo test` to run the whole testsuite.
137
138 If you don't want to wait for all tests to finish, you can also execute a single test file by using `TESTNAME` to specify the test to run:
139
140 ```bash
141 TESTNAME=ui/empty_line_after_outer_attr cargo test --test compile-test
142 ```
143
144 Clippy uses UI tests. UI tests check that the output of the compiler is exactly as expected.
145 Of course there's little sense in writing the output yourself or copying it around.
146 Therefore you should use `tests/ui/update-all-references.sh` (after running
147 `cargo test`) and check whether the output looks as you expect with `git diff`. Commit all
148 `*.stderr` files, too.
149
150 ### Testing manually
151
152 Manually testing against an example file is useful if you have added some
153 `println!`s and test suite output becomes unreadable.  To try clippy with your
154 local modifications, run `cargo run --bin clippy-driver -- -L ./target/debug input.rs` from the
155 working copy root.
156
157 ### How Clippy works
158
159 Clippy is a [rustc compiler plugin][compiler_plugin]. The main entry point is at [`src/lib.rs`][main_entry]. In there, the lint registration is delegated to the [`clippy_lints`][lint_crate] crate.
160
161 [`clippy_lints/src/lib.rs`][lint_crate_entry] imports all the different lint modules and registers them with the rustc plugin registry. For example, the [`else_if_without_else`][else_if_without_else] lint is registered like this:
162
163 ```rust
164 // ./clippy_lints/src/lib.rs
165
166 // ...
167 pub mod else_if_without_else;
168 // ...
169
170 pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
171     // ...
172     reg.register_early_lint_pass(box else_if_without_else::ElseIfWithoutElse);
173     // ...
174
175     reg.register_lint_group("clippy_restriction", vec![
176         // ...
177         else_if_without_else::ELSE_IF_WITHOUT_ELSE,
178         // ...
179     ]);
180 }
181 ```
182
183 The [`rustc_plugin::PluginRegistry`][plugin_registry] provides two methods to register lints: [register_early_lint_pass][reg_early_lint_pass] and [register_late_lint_pass][reg_late_lint_pass].
184 Both take an object that implements an [`EarlyLintPass`][early_lint_pass] or [`LateLintPass`][late_lint_pass] respectively. This is done in every single lint. 
185 It's worth noting that the majority of `clippy_lints/src/lib.rs` is autogenerated by `util/update_lints.py` and you don't have to add anything by hand. When you are writing your own lint, you can use that script to save you some time.
186
187 ```rust
188 // ./clippy_lints/src/else_if_without_else.rs
189
190 use rustc::lint::*;
191
192 // ...
193
194 pub struct ElseIfWithoutElse;
195
196 // ...
197
198 impl EarlyLintPass for ElseIfWithoutElse {
199     // ... the functions needed, to make the lint work
200 }
201 ```
202
203 The difference between `EarlyLintPass` and `LateLintPass` is that the methods of the `EarlyLintPass` trait only provide AST information. The methods of the `LateLintPass` trait are executed after type checking and contain type information via the `LateContext` parameter.
204
205 That's why the `else_if_without_else` example uses the `register_early_lint_pass` function. Because the [actual lint logic][else_if_without_else] does not depend on any type information.
206
207 ### Fixing nightly build failures
208
209 Clippy will sometimes break with new nightly version releases. This is expected because Clippy still depends on nightly Rust. Most of the times we have to adapt to the changes and only very rarely there's an actual bug in rust.
210
211 In order to find out why Clippy does not work properly with a new nightly version, you can use the [rust-toolstate commit history][toolstate_commit_history].
212 You will then have to look for the last commit that contains `test-pass -> build-fail` or `test-pass` -> `test-fail` for the `clippy-driver` component. [Here][toolstate_commit] is an example.
213
214 The commit message contains a link to the PR. The PRs are usually small enough to discover the breaking API change and if they are bigger, they likely include some discussion that may help you to fix Clippy.
215
216 Fixing nightly build failures is also a good way to learn about actual rustc internals.
217
218 ## Contributions
219
220 Contributions to Clippy should be made in the form of GitHub pull requests. Each pull request will
221 be reviewed by a core contributor (someone with permission to land patches) and either landed in the
222 main tree or given feedback for changes that would be required.
223
224 All code in this repository is under the [Mozilla Public License, 2.0](https://www.mozilla.org/MPL/2.0/)
225
226 <!-- adapted from https://github.com/servo/servo/blob/master/CONTRIBUTING.md -->
227
228 [main_entry]: https://github.com/rust-lang-nursery/rust-clippy/blob/c5b39a5917ffc0f1349b6e414fa3b874fdcf8429/src/lib.rs#L14
229 [lint_crate]: https://github.com/rust-lang-nursery/rust-clippy/tree/c5b39a5917ffc0f1349b6e414fa3b874fdcf8429/clippy_lints/src
230 [lint_crate_entry]: https://github.com/rust-lang-nursery/rust-clippy/blob/c5b39a5917ffc0f1349b6e414fa3b874fdcf8429/clippy_lints/src/lib.rs
231 [else_if_without_else]: https://github.com/rust-lang-nursery/rust-clippy/blob/c5b39a5917ffc0f1349b6e414fa3b874fdcf8429/clippy_lints/src/else_if_without_else.rs
232 [compiler_plugin]: https://doc.rust-lang.org/unstable-book/language-features/plugin.html#lint-plugins
233 [plugin_registry]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_plugin/registry/struct.Registry.html
234 [reg_early_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_plugin/registry/struct.Registry.html#method.register_early_lint_pass
235 [reg_late_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_plugin/registry/struct.Registry.html#method.register_late_lint_pass
236 [early_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.EarlyLintPass.html
237 [late_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.LateLintPass.html
238 [toolstate_commit_history]: https://github.com/rust-lang-nursery/rust-toolstate/commits/master
239 [toolstate_commit]: https://github.com/rust-lang-nursery/rust-toolstate/commit/6ce0459f6bfa7c528ae1886492a3e0b5ef0ee547