Skip to content

Commit aceb443

Browse files
committed
Auto merge of #10199 - tylerjw:document-extending-list-configs, r=xFrednet
Document lint configuration values in Clippy's book changelog: document lint configuration values in Clippy's book fixes #9991 r? `@xFrednet`
2 parents 483b7ac + c0da8ac commit aceb443

File tree

8 files changed

+634
-13
lines changed

8 files changed

+634
-13
lines changed

.github/workflows/clippy_bors.yml

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ jobs:
157157
- name: Test metadata collection
158158
run: cargo collect-metadata
159159

160+
- name: Test lint_configuration.md is up-to-date
161+
run: |
162+
echo "run \`cargo collect-metadata\` if this fails"
163+
git update-index --refresh
164+
160165
integration_build:
161166
needs: changelog
162167
runs-on: ubuntu-latest

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,21 @@ value` mapping e.g.
194194
```toml
195195
avoid-breaking-exported-api = false
196196
disallowed-names = ["toto", "tata", "titi"]
197-
cognitive-complexity-threshold = 30
198197
```
199198

200-
See the [list of configurable lints](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration),
201-
the lint descriptions contain the names and meanings of these configuration variables.
199+
The [table of configurations](https://doc.rust-lang.org/nightly/clippy/lint_configuration.html)
200+
contains all config values, their default, and a list of lints they affect.
201+
Each [configurable lint](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration)
202+
, also contains information about these values.
203+
204+
For configurations that are a list type with default values such as
205+
[disallowed-names](https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names),
206+
you can use the unique value `".."` to extend the default values instead of replacing them.
207+
208+
```toml
209+
# default of disallowed-names is ["foo", "baz", "quux"]
210+
disallowed-names = ["bar", ".."] # -> ["bar", "foo", "baz", "quux"]
211+
```
202212

203213
> **Note**
204214
>

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Installation](installation.md)
66
- [Usage](usage.md)
77
- [Configuration](configuration.md)
8+
- [Lint Configuration](lint_configuration.md)
89
- [Clippy's Lints](lints.md)
910
- [Continuous Integration](continuous_integration/README.md)
1011
- [GitHub Actions](continuous_integration/github_actions.md)

book/src/configuration.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ basic `variable = value` mapping eg.
88
```toml
99
avoid-breaking-exported-api = false
1010
disallowed-names = ["toto", "tata", "titi"]
11-
cognitive-complexity-threshold = 30
1211
```
1312

14-
See the [list of configurable lints](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration),
15-
the lint descriptions contain the names and meanings of these configuration variables.
13+
The [table of configurations](./lint_configuration.md)
14+
contains all config values, their default, and a list of lints they affect.
15+
Each [configurable lint](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration)
16+
, also contains information about these values.
17+
18+
For configurations that are a list type with default values such as
19+
[disallowed-names](https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names),
20+
you can use the unique value `".."` to extend the default values instead of replacing them.
21+
22+
```toml
23+
# default of disallowed-names is ["foo", "baz", "quux"]
24+
disallowed-names = ["bar", ".."] # -> ["bar", "foo", "baz", "quux"]
25+
```
1626

1727
To deactivate the "for further information visit *lint-link*" message you can define the `CLIPPY_DISABLE_DOCS_LINKS`
1828
environment variable.

book/src/development/adding_lints.md

+4
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ for some users. Adding a configuration is done in the following steps:
700700
`clippy.toml` file with the configuration value and a rust file that
701701
should be linted by Clippy. The test can otherwise be written as usual.
702702

703+
5. Update [Lint Configuration](../lint_configuration.md)
704+
705+
Run `cargo collect-metadata` to generate documentation changes for the book.
706+
703707
[`clippy_lints::utils::conf`]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/utils/conf.rs
704708
[`clippy_lints` lib file]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/lib.rs
705709
[`tests/ui`]: https://github.com/rust-lang/rust-clippy/blob/master/tests/ui

book/src/lint_configuration.md

+523
Large diffs are not rendered by default.

clippy_lints/src/utils/conf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ define_Conf! {
219219
///
220220
/// #### Noteworthy
221221
///
222-
/// A type, say `SomeType`, listed in this configuration has the same behavior of `["SomeType" , "*"], ["*", "SomeType"]` in `arithmetic_side_effects_allowed_binary`.
222+
/// A type, say `SomeType`, listed in this configuration has the same behavior of
223+
/// `["SomeType" , "*"], ["*", "SomeType"]` in `arithmetic_side_effects_allowed_binary`.
223224
(arithmetic_side_effects_allowed: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
224225
/// Lint: ARITHMETIC_SIDE_EFFECTS.
225226
///

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+73-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use clippy_utils::diagnostics::span_lint;
1414
use clippy_utils::ty::{match_type, walk_ptrs_ty_depth};
1515
use clippy_utils::{last_path_segment, match_def_path, match_function_call, match_path, paths};
1616
use if_chain::if_chain;
17+
use itertools::Itertools;
1718
use rustc_ast as ast;
1819
use rustc_data_structures::fx::FxHashMap;
1920
use rustc_hir::{
@@ -34,8 +35,10 @@ use std::path::Path;
3435
use std::path::PathBuf;
3536
use std::process::Command;
3637

37-
/// This is the output file of the lint collector.
38-
const OUTPUT_FILE: &str = "../util/gh-pages/lints.json";
38+
/// This is the json output file of the lint collector.
39+
const JSON_OUTPUT_FILE: &str = "../util/gh-pages/lints.json";
40+
/// This is the markdown output file of the lint collector.
41+
const MARKDOWN_OUTPUT_FILE: &str = "../book/src/lint_configuration.md";
3942
/// These lints are excluded from the export.
4043
const BLACK_LISTED_LINTS: &[&str] = &["lint_author", "dump_hir", "internal_metadata_collector"];
4144
/// These groups will be ignored by the lint group matcher. This is useful for collections like
@@ -176,6 +179,23 @@ This lint has the following configuration variables:
176179
)
177180
})
178181
}
182+
183+
fn configs_to_markdown(&self, map_fn: fn(&ClippyConfiguration) -> String) -> String {
184+
self.config
185+
.iter()
186+
.filter(|config| config.deprecation_reason.is_none())
187+
.filter(|config| !config.lints.is_empty())
188+
.map(map_fn)
189+
.join("\n")
190+
}
191+
192+
fn get_markdown_docs(&self) -> String {
193+
format!(
194+
"## Lint Configuration Options\n| <div style=\"width:290px\">Option</div> | Default Value |\n|--|--|\n{}\n\n{}\n",
195+
self.configs_to_markdown(ClippyConfiguration::to_markdown_table_entry),
196+
self.configs_to_markdown(ClippyConfiguration::to_markdown_paragraph),
197+
)
198+
}
179199
}
180200

181201
impl Drop for MetadataCollector {
@@ -199,12 +219,37 @@ impl Drop for MetadataCollector {
199219

200220
collect_renames(&mut lints);
201221

202-
// Outputting
203-
if Path::new(OUTPUT_FILE).exists() {
204-
fs::remove_file(OUTPUT_FILE).unwrap();
222+
// Outputting json
223+
if Path::new(JSON_OUTPUT_FILE).exists() {
224+
fs::remove_file(JSON_OUTPUT_FILE).unwrap();
205225
}
206-
let mut file = OpenOptions::new().write(true).create(true).open(OUTPUT_FILE).unwrap();
226+
let mut file = OpenOptions::new()
227+
.write(true)
228+
.create(true)
229+
.open(JSON_OUTPUT_FILE)
230+
.unwrap();
207231
writeln!(file, "{}", serde_json::to_string_pretty(&lints).unwrap()).unwrap();
232+
233+
// Outputting markdown
234+
if Path::new(MARKDOWN_OUTPUT_FILE).exists() {
235+
fs::remove_file(MARKDOWN_OUTPUT_FILE).unwrap();
236+
}
237+
let mut file = OpenOptions::new()
238+
.write(true)
239+
.create(true)
240+
.open(MARKDOWN_OUTPUT_FILE)
241+
.unwrap();
242+
writeln!(
243+
file,
244+
"<!--
245+
This file is generated by `cargo collect-metadata`.
246+
Please use that command to update the file and do not edit it by hand.
247+
-->
248+
249+
{}",
250+
self.get_markdown_docs(),
251+
)
252+
.unwrap();
208253
}
209254
}
210255

@@ -505,6 +550,28 @@ impl ClippyConfiguration {
505550
deprecation_reason,
506551
}
507552
}
553+
554+
fn to_markdown_paragraph(&self) -> String {
555+
format!(
556+
"### {}\n{}\n\n**Default Value:** `{}` (`{}`)\n\n{}\n\n",
557+
self.name,
558+
self.doc
559+
.lines()
560+
.map(|line| line.strip_prefix(" ").unwrap_or(line))
561+
.join("\n"),
562+
self.default,
563+
self.config_type,
564+
self.lints
565+
.iter()
566+
.map(|name| name.to_string().split_whitespace().next().unwrap().to_string())
567+
.map(|name| format!("* [{name}](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"))
568+
.join("\n"),
569+
)
570+
}
571+
572+
fn to_markdown_table_entry(&self) -> String {
573+
format!("| [{}](#{}) | `{}` |", self.name, self.name, self.default)
574+
}
508575
}
509576

510577
fn collect_configs() -> Vec<ClippyConfiguration> {

0 commit comments

Comments
 (0)