Skip to content

Commit 76d1e26

Browse files
authored
Merge pull request #2612 from phansch/document_how_to_use_the_author_lint
Document the author lint
2 parents fef7fb3 + b1b0b36 commit 76d1e26

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

CONTRIBUTING.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,44 @@ to lint-writing, though it does get into advanced stuff. Most lints consist of a
5151
`LintPass` with one or more of its default methods overridden. See the existing lints for examples
5252
of this.
5353

54+
55+
#### Author lint
56+
57+
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.
58+
59+
Create a new UI test with the pattern you want to match:
60+
61+
```rust
62+
// ./tests/ui/my_lint.rs
63+
64+
// The custom_attribute needs to be enabled for the author lint to work
65+
#![feature(plugin, custom_attribute)]
66+
67+
fn main() {
68+
#[clippy(author)]
69+
let arr: [i32; 1] = [7]; // Replace line with the code you want to match
70+
}
71+
```
72+
73+
Now you run `TESTNAME=ui/my_lint cargo test --test compile-test` to produce
74+
the file with the generated code:
75+
76+
```rust
77+
// ./tests/ui/my_lint.stdout
78+
79+
if_chain! {
80+
if let Expr_::ExprArray(ref elements) = stmt.node;
81+
if elements.len() == 1;
82+
if let Expr_::ExprLit(ref lit) = elements[0].node;
83+
if let LitKind::Int(7, _) = lit.node;
84+
then {
85+
// report your lint here
86+
}
87+
}
88+
```
89+
90+
#### Documentation
91+
5492
Please document your lint with a doc comment akin to the following:
5593

5694
```rust
@@ -71,6 +109,8 @@ Please document your lint with a doc comment akin to the following:
71109
/// ```
72110
```
73111

112+
Once your lint is merged it will show up in the [lint list](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
113+
74114
### Running test suite
75115

76116
Clippy uses UI tests. UI tests check that the output of the compiler is exactly as expected.

clippy_lints/src/utils/author.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::collections::HashMap;
1414
///
1515
/// **Example:**
1616
/// ```rust
17+
/// // ./tests/ui/my_lint.rs
1718
/// fn foo() {
1819
/// // detect the following pattern
1920
/// #[clippy(author)]
@@ -24,9 +25,11 @@ use std::collections::HashMap;
2425
/// }
2526
/// ```
2627
///
27-
/// prints
28+
/// Running `TESTNAME=ui/my_lint cargo test --test compile-test` will produce
29+
/// a `./tests/ui/new_lint.stdout` file with the generated code:
2830
///
29-
/// ```
31+
/// ```rust
32+
/// // ./tests/ui/new_lint.stdout
3033
/// if_chain!{
3134
/// if let Expr_::ExprIf(ref cond, ref then, None) = item.node,
3235
/// if let Expr_::ExprBinary(BinOp::Eq, ref left, ref right) = cond.node,

0 commit comments

Comments
 (0)