@@ -23,6 +23,7 @@ because that's clearly a non-descriptive name.
23
23
- [ Running rustfmt] ( #running-rustfmt )
24
24
- [ Debugging] ( #debugging )
25
25
- [ PR Checklist] ( #pr-checklist )
26
+ - [ Adding configuration to a lint] ( #adding-configuration-to-a-lint )
26
27
- [ Cheatsheet] ( #cheatsheet )
27
28
28
29
## Setup
@@ -526,6 +527,81 @@ Before submitting your PR make sure you followed all of the basic requirements:
526
527
- \[ ] Added lint documentation
527
528
- \[ ] Run ` cargo dev fmt `
528
529
530
+ ## Adding configuration to a lint
531
+
532
+ Clippy supports the configuration of lints values using a ` clippy.toml ` file in the workspace
533
+ directory. Adding a configuration to a lint can be useful for thresholds or to constrain some
534
+ behavior that can be seen as a false positive for some users. Adding a configuration is done
535
+ in the following steps:
536
+
537
+ 1 . Adding a new configuration entry to [ clippy_lints::utils::conf] ( /clippy_lints/src/utils/conf.rs )
538
+ like this:
539
+ ``` rust
540
+ /// Lint: LINT_NAME. <The configuration field doc comment>
541
+ (configuration_ident , " configuration_value" : Type , DefaultValue ),
542
+ ```
543
+ The configuration value and identifier should usually be the same . The doc comment will be
544
+ automatically added to the lint documentation .
545
+ 2 . Adding the configuration value to the lint impl struct :
546
+ 1 . This first requires the definition of a lint impl struct . Lint impl structs are usually
547
+ generated with the `declare_lint_pass! ` macro . This struct needs to be defined manually
548
+ to add some kind of metadata to it :
549
+ ```rust
550
+ // Generated struct definition
551
+ declare_lint_pass! (StructName => [
552
+ LINT_NAME
553
+ ]);
554
+
555
+ // New manual definition struct
556
+ #[derive(Copy , Clone )]
557
+ pub struct StructName {}
558
+
559
+ impl_lint_pass! (StructName => [
560
+ LINT_NAME
561
+ ]);
562
+ ```
563
+
564
+ 2 . Next add the configuration value and a corresponding creation method like this :
565
+ ```rust
566
+ #[derive(Copy , Clone )]
567
+ pub struct StructName {
568
+ configuration_ident : Type ,
569
+ }
570
+
571
+ // ...
572
+
573
+ impl StructName {
574
+ pub fn new (configuration_ident : Type ) -> Self {
575
+ Self {
576
+ configuration_ident ,
577
+ }
578
+ }
579
+ }
580
+ ```
581
+ 3 . Passing the configuration value to the lint impl struct :
582
+
583
+ First find the struct construction in the [clippy_lints lib file ](/ clippy_lints / src / lib . rs).
584
+ Make sure that `clippy dev update_lints ` added it beforehand . The configuration value is now
585
+ cloned or copied into a local value that is then passed to the impl struct like this :
586
+ ```rust
587
+ // Default generated registration:
588
+ store . register_late_pass (|| box module :: StructName );
589
+
590
+ // New registration with configuration value
591
+ let configuration_ident = conf . configuration_ident. clone ();
592
+ store . register_late_pass (move || box module :: StructName :: new (configuration_ident ));
593
+ ```
594
+
595
+ Congratulations the work is almost done . The configuration value can now be accessed
596
+ in the linting code via `self . configuration_ident`.
597
+
598
+ 4 . Adding tests :
599
+ 1 . The default configured value can be tested like any normal lint in [`tests / ui `](/ tests / ui ).
600
+ 2 . The configuration itself will be tested separately in [`tests / ui - toml `](/ tests / ui - toml ).
601
+ Simply add a new subfolder with a fitting name . This folder contains a `clippy . toml` file
602
+ with the configuration value and a rust file that should be linted by clippy . The test can
603
+ otherwise be written as usual .
604
+
529
605
## Cheatsheet
530
606
531
607
Here are some pointers to things you are likely going to need for every lint :
0 commit comments