You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+61Lines changed: 61 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -147,6 +147,56 @@ enabled as a plugin:
147
147
#![plugin(clippy)]
148
148
```
149
149
150
+
### How Clippy works
151
+
152
+
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.
153
+
154
+
[`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:
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].
177
+
Both take an object that implements an [`EarlyLintPass`][early_lint_pass] or [`LateLintPass`][late_lint_pass] respectively. This is done in every single lint.
178
+
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.
179
+
180
+
```rust
181
+
// ./clippy_lints/src/else_if_without_else.rs
182
+
183
+
userustc::lint::*;
184
+
185
+
// ...
186
+
187
+
pubstructElseIfWithoutElse;
188
+
189
+
// ...
190
+
191
+
implEarlyLintPassforElseIfWithoutElse {
192
+
// ... the functions needed, to make the lint work
193
+
}
194
+
```
195
+
196
+
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.
197
+
198
+
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.
199
+
150
200
## Contributions
151
201
152
202
Contributions to Clippy should be made in the form of GitHub pull requests. Each pull request will
@@ -156,3 +206,14 @@ main tree or given feedback for changes that would be required.
156
206
All code in this repository is under the [Mozilla Public License, 2.0](https://www.mozilla.org/MPL/2.0/)
157
207
158
208
<!-- adapted from https://github.com/servo/servo/blob/master/CONTRIBUTING.md -->
0 commit comments