Skip to content

Commit c56b328

Browse files
committed
Auto merge of #6630 - xFrednet:0000-configuration-documentation, r=llogiq
Documentation for adding configuration to a lint and common abbreviations This PR adds some commonly used abbreviations to the `basis.md` file and a guide on how to implement a configuration value for a lint. * [Rendered `/doc/basics.md` (Abbreviation list)](https://github.com/xFrednet/rust-clippy/blob/0000-configuration-documentation/doc/basics.md#common-abbreviations) * [Rendered `/doc/adding_lints.md` (Configuration value guide)](https://github.com/xFrednet/rust-clippy/blob/0000-configuration-documentation/doc/adding_lints.md#adding-configuration-to-a-lint) I'm not sure if the guide is written in the best way. Style suggestions are appreciated. 🙃 --- Again a big **thank you** for everyone who helped to collect the abbreviation list over on [zulip]. I had a lot of fun, and it was also very informative. Keep up the good work 🙃 [zulip]: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Common.20abbreviations.20in.20basics.2Emd/near/223548065 --- changelog: none
2 parents 70386ff + 0373dc3 commit c56b328

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
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

+20
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
@@ -94,3 +95,22 @@ cargo dev ra_setup
9495

9596
We follow a rustc no merge-commit policy.
9697
See <https://rustc-dev-guide.rust-lang.org/contributing.html#opening-a-pr>.
98+
99+
## Common Abbreviations
100+
101+
| Abbreviation | Meaning |
102+
| ------------ | -------------------------------------- |
103+
| UB | Undefined Behavior |
104+
| FP | False Positive |
105+
| FN | False Negative |
106+
| ICE | Internal Compiler Error |
107+
| AST | Abstract Syntax Tree |
108+
| MIR | Mid-Level Intermediate Representation |
109+
| HIR | High-Level Intermediate Representation |
110+
| TCX | Type context |
111+
112+
This is a concise list of abbreviations that can come up during clippy development. An extensive
113+
general list can be found in the [rustc-dev-guide glossary][glossary]. Always feel free to ask if
114+
an abbreviation or meaning is unclear to you.
115+
116+
[glossary]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html

0 commit comments

Comments
 (0)