]> git.lizzy.rs Git - rust.git/blobdiff - doc/adding_lints.md
more Use->DropTemps fixes
[rust.git] / doc / adding_lints.md
index 53e671e17e034f2db5bf2965df39510fa38d156a..5781ab7f9999c7860054a4b99d98242df4e5bda8 100644 (file)
@@ -115,8 +115,8 @@ where all the lint code is. We are going to call the file
 `clippy_lints/src/foo_functions.rs` and import some initial things we need:
 
 ```rust
-use rustc::lint::{LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::lint::{LintArray, LintPass, EarlyLintPass};
+use rustc::{declare_lint_pass, declare_tool_lint};
 ```
 
 The next step is to provide a lint declaration. Lints are declared using the
@@ -147,20 +147,9 @@ lint pass:
 
 // .. imports and lint declaration ..
 
-#[derive(Copy, Clone)]
-pub struct FooFunctionsPass;
-
-impl LintPass for FooFunctionsPass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(
-            FOO_FUNCTIONS,
-        )
-    }
+declare_lint_pass!(FooFunctions => [FOO_FUNCTIONS]);
 
-    fn name(&self) -> &'static str {
-        "FooFunctions"
-    }
-}
+impl EarlyLintPass for FooFunctions {}
 ```
 
 Don't worry about the `name` method here. As long as it includes the name of the
@@ -174,7 +163,7 @@ will have to register our lint pass manually in the `register_plugins` function
 in `clippy_lints/src/lib.rs`:
 
 ```rust
-reg.register_early_lint_pass(box foo_functions::FooFunctionsPass);
+reg.register_early_lint_pass(box foo_functions::FooFunctions);
 ```
 
 This should fix the `unknown clippy lint: clippy::foo_functions` error that we
@@ -209,10 +198,10 @@ use rustc::{declare_tool_lint, lint_array};
 With UI tests and the lint declaration in place, we can start working on the
 implementation of the lint logic.
 
-Let's start by implementing the `EarlyLintPass` for our `FooFunctionsPass`:
+Let's start by implementing the `EarlyLintPass` for our `FooFunctions`:
 
 ```rust
-impl EarlyLintPass for FooFunctionsPass {
+impl EarlyLintPass for FooFunctions {
     fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
         // TODO: Emit lint here
     }
@@ -234,7 +223,7 @@ provide an extra help message and we can't really suggest a better name
 automatically. This is how it looks:
 
 ```rust
-impl EarlyLintPass for Pass {
+impl EarlyLintPass for FooFunctions {
     fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
         span_help_and_lint(
             cx,
@@ -261,7 +250,7 @@ Both provide access to the name of the function/method via an [`Ident`][ident].
 With that we can expand our `check_fn` method to:
 
 ```rust
-impl EarlyLintPass for Pass {
+impl EarlyLintPass for FooFunctions {
     fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
         if is_foo_fn(fn_kind) {
             span_help_and_lint(
@@ -302,16 +291,6 @@ running `cargo test` should produce the expected output. Remember to run
 `cargo test` (as opposed to `cargo uitest`) will also ensure that our lint
 implementation is not violating any Clippy lints itself.
 
-If you are still following the example, you will see that `FooFunctionsPass`
-violates a Clippy lint. So we are going to rename that struct to just `Pass`:
-
-```rust
-#[derive(Copy, Clone)]
-pub struct Pass;
-
-impl LintPass for Pass { /* .. */ }
-```
-
 That should be it for the lint implementation. Running `cargo test` should now
 pass.
 
@@ -392,6 +371,7 @@ Before submitting your PR make sure you followed all of the basic requirements:
 - [ ] `cargo test` passes locally
 - [ ] Executed `util/dev update_lints`
 - [ ] Added lint documentation
+- [ ] Run `cargo fmt`
 
 ### Cheatsheet