Skip to content

Commit 0373dc3

Browse files
committed
Added documentation for adding a configuration to lints
* Fixed some spelling
1 parent a905cf6 commit 0373dc3

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

doc/adding_lints.md

+76
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ because that's clearly a non-descriptive name.
2323
- [Running rustfmt](#running-rustfmt)
2424
- [Debugging](#debugging)
2525
- [PR Checklist](#pr-checklist)
26+
- [Adding configuration to a lint](#adding-configuration-to-a-lint)
2627
- [Cheatsheet](#cheatsheet)
2728

2829
## Setup
@@ -526,6 +527,81 @@ Before submitting your PR make sure you followed all of the basic requirements:
526527
- \[ ] Added lint documentation
527528
- \[ ] Run `cargo dev fmt`
528529

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+
529605
## Cheatsheet
530606

531607
Here are some pointers to things you are likely going to need for every lint:

doc/basics.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ the codebase take a look at [Adding Lints] or [Common Tools].
1111
- [Get the Code](#get-the-code)
1212
- [Building and Testing](#building-and-testing)
1313
- [`cargo dev`](#cargo-dev)
14+
- [Common Abbreviations](#common-abbreviations)
1415
- [PR](#pr)
1516

1617
## Get the Code
@@ -109,7 +110,7 @@ See <https://rustc-dev-guide.rust-lang.org/contributing.html#opening-a-pr>.
109110
| TCX | Type context |
110111

111112
This is a concise list of abbreviations that can come up during clippy development. An extensive
112-
genal list can be found in the [rustc-dev-guide glossary][glossary]. Always feel free to ask if
113+
general list can be found in the [rustc-dev-guide glossary][glossary]. Always feel free to ask if
113114
an abbreviation or meaning is unclear to you.
114115

115-
[glossary]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html
116+
[glossary]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html

0 commit comments

Comments
 (0)