@@ -48,16 +48,19 @@ case), and we don't need type information, so it will have an early pass type
48
48
take a look at our [ lint naming guidelines] [ lint_naming ] .
49
49
50
50
## Defining Our Lint
51
+
51
52
To get started, there are two ways to define our lint.
52
53
53
54
### Standalone
55
+
54
56
Command: ` cargo dev new_lint --name=foo_functions --pass=early --category=pedantic `
55
57
(category will default to nursery if not provided)
56
58
57
59
This command will create a new file: ` clippy_lints/src/foo_functions.rs ` , as well
58
60
as [ register the lint] ( #lint-registration ) .
59
61
60
62
### Specific Type
63
+
61
64
Command: ` cargo dev new_lint --name=foo_functions --type=functions --category=pedantic `
62
65
63
66
This command will create a new file: ` clippy_lints/src/{type}/foo_functions.rs ` .
@@ -71,6 +74,7 @@ the example command. These are groupings of lints with common behaviors, so if y
71
74
lint falls into one, it would be best to add it to that type.
72
75
73
76
### Tests Location
77
+
74
78
Both commands will create a file: ` tests/ui/foo_functions.rs ` . For cargo lints,
75
79
two project hierarchies (fail/pass) will be created by default under ` tests/ui-cargo ` .
76
80
@@ -147,9 +151,9 @@ If our new lint is named e.g. `foo_categories`, after running `cargo dev
147
151
new_lint --name=foo_categories --type=cargo --category=cargo` we will find by
148
152
default two new crates, each with its manifest file:
149
153
150
- * ` tests/ui-cargo/foo_categories/fail/Cargo.toml ` : this file should cause the
154
+ - ` tests/ui-cargo/foo_categories/fail/Cargo.toml ` : this file should cause the
151
155
new lint to raise an error.
152
- * ` tests/ui-cargo/foo_categories/pass/Cargo.toml ` : this file should not trigger
156
+ - ` tests/ui-cargo/foo_categories/pass/Cargo.toml ` : this file should not trigger
153
157
the lint.
154
158
155
159
If you need more cases, you can copy one of those crates (under
@@ -230,22 +234,22 @@ declare_clippy_lint! {
230
234
}
231
235
```
232
236
233
- * The section of lines prefixed with ` /// ` constitutes the lint documentation
237
+ - The section of lines prefixed with ` /// ` constitutes the lint documentation
234
238
section. This is the default documentation style and will be displayed [ like
235
239
this] [ example_lint_page ] . To render and open this documentation locally in a
236
240
browser, run ` cargo dev serve ` .
237
- * The ` #[clippy::version] ` attribute will be rendered as part of the lint
241
+ - The ` #[clippy::version] ` attribute will be rendered as part of the lint
238
242
documentation. The value should be set to the current Rust version that the
239
243
lint is developed in, it can be retrieved by running ` rustc -vV ` in the
240
- rust-clippy directory. The version is listed under * release * . (Use the version
244
+ rust-clippy directory. The version is listed under _ release _ . (Use the version
241
245
without the ` -nightly ` ) suffix.
242
- * ` FOO_FUNCTIONS ` is the name of our lint. Be sure to follow the [ lint naming
246
+ - ` FOO_FUNCTIONS ` is the name of our lint. Be sure to follow the [ lint naming
243
247
guidelines] [ lint_naming ] here when naming your lint. In short, the name should
244
248
state the thing that is being checked for and read well when used with
245
249
` allow ` /` warn ` /` deny ` .
246
- * ` pedantic ` sets the lint level to ` Allow ` . The exact mapping can be found
250
+ - ` pedantic ` sets the lint level to ` Allow ` . The exact mapping can be found
247
251
[ here] [ category_level_mapping ]
248
- * The last part should be a text that explains what exactly is wrong with the
252
+ - The last part should be a text that explains what exactly is wrong with the
249
253
code
250
254
251
255
The rest of this file contains an empty implementation for our lint pass, which
@@ -330,7 +334,7 @@ We implement the [`check_fn`][check_fn] method from the
330
334
[ ` EarlyLintPass ` ] [ early_lint_pass ] trait. This gives us access to various
331
335
information about the function that is currently being checked. More on that in
332
336
the next section. Let's worry about the details later and emit our lint for
333
- * every * function definition first.
337
+ _ every _ function definition first.
334
338
335
339
Depending on how complex we want our lint message to be, we can choose from a
336
340
variety of lint emission functions. They can all be found in
@@ -459,7 +463,7 @@ pub struct ManualStrip {
459
463
460
464
impl ManualStrip {
461
465
pub fn new (conf : & 'static Conf ) -> Self {
462
- Self { msrv : conf . msrv. clone () }
466
+ Self { msrv : conf . msrv. into () }
463
467
}
464
468
}
465
469
```
@@ -630,9 +634,9 @@ code, creating conflicting diagnostics.
630
634
When you are creating a lint that ends up in this scenario, the following tips should be encouraged to guide
631
635
classification:
632
636
633
- * The only case where they should be in the same category is if that category is ` restriction ` . For example,
637
+ - The only case where they should be in the same category is if that category is ` restriction ` . For example,
634
638
` semicolon_inside_block ` and ` semicolon_outside_block ` .
635
- * For all the other cases, they should be in different categories with different levels of allowance. For example,
639
+ - For all the other cases, they should be in different categories with different levels of allowance. For example,
636
640
` implicit_return ` (restriction, allow) and ` needless_return ` (style, warn).
637
641
638
642
For lints that are in different categories, it is also recommended that at least one of them should be in the
@@ -681,6 +685,7 @@ for some users. Adding a configuration is done in the following steps:
681
685
1 . This first requires the definition of a lint impl struct. Lint impl
682
686
structs are usually generated with the ` declare_lint_pass! ` macro. This
683
687
struct needs to be defined manually to add some kind of metadata to it:
688
+
684
689
``` rust
685
690
// Generated struct definition
686
691
declare_lint_pass! (StructName => [
@@ -697,6 +702,7 @@ for some users. Adding a configuration is done in the following steps:
697
702
698
703
2 . Next add the configuration value and a corresponding creation method like
699
704
this :
705
+
700
706
```rust
701
707
pub struct StructName {
702
708
configuration_ident : Type ,
@@ -712,6 +718,7 @@ for some users. Adding a configuration is done in the following steps:
712
718
}
713
719
}
714
720
```
721
+
715
722
3 . Passing the configuration value to the lint impl struct :
716
723
717
724
First find the struct construction in the [`clippy_lints ` lib file ]. The
@@ -750,31 +757,31 @@ for some users. Adding a configuration is done in the following steps:
750
757
751
758
Here are some pointers to things you are likely going to need for every lint:
752
759
753
- * [ Clippy utils] [ utils ] - Various helper functions. Maybe the function you need
760
+ - [ Clippy utils] [ utils ] - Various helper functions. Maybe the function you need
754
761
is already in here ([ ` is_type_diagnostic_item ` ] , [ ` implements_trait ` ] ,
755
762
[ ` snippet ` ] , etc)
756
- * [ Clippy diagnostics] [ diagnostics ]
757
- * [ Let chains] [ let-chains ]
758
- * [ ` from_expansion ` ] [ from_expansion ] and
763
+ - [ Clippy diagnostics] [ diagnostics ]
764
+ - [ Let chains] [ let-chains ]
765
+ - [ ` from_expansion ` ] [ from_expansion ] and
759
766
[ ` in_external_macro ` ] [ in_external_macro ]
760
- * [ ` Span ` ] [ span ]
761
- * [ ` Applicability ` ] [ applicability ]
762
- * [ Common tools for writing lints] ( common_tools_writing_lints.md ) helps with
767
+ - [ ` Span ` ] [ span ]
768
+ - [ ` Applicability ` ] [ applicability ]
769
+ - [ Common tools for writing lints] ( common_tools_writing_lints.md ) helps with
763
770
common operations
764
- * [ The rustc-dev-guide] [ rustc-dev-guide ] explains a lot of internal compiler
771
+ - [ The rustc-dev-guide] [ rustc-dev-guide ] explains a lot of internal compiler
765
772
concepts
766
- * [ The nightly rustc docs] [ nightly_docs ] which has been linked to throughout
773
+ - [ The nightly rustc docs] [ nightly_docs ] which has been linked to throughout
767
774
this guide
768
775
769
776
For ` EarlyLintPass ` lints:
770
777
771
- * [ ` EarlyLintPass ` ] [ early_lint_pass ]
772
- * [ ` rustc_ast::ast ` ] [ ast ]
778
+ - [ ` EarlyLintPass ` ] [ early_lint_pass ]
779
+ - [ ` rustc_ast::ast ` ] [ ast ]
773
780
774
781
For ` LateLintPass ` lints:
775
782
776
- * [ ` LateLintPass ` ] [ late_lint_pass ]
777
- * [ ` Ty::TyKind ` ] [ ty ]
783
+ - [ ` LateLintPass ` ] [ late_lint_pass ]
784
+ - [ ` Ty::TyKind ` ] [ ty ]
778
785
779
786
While most of Clippy's lint utils are documented, most of rustc's internals lack
780
787
documentation currently. This is unfortunate, but in most cases you can probably
0 commit comments