Skip to content

Commit ad70bff

Browse files
committed
Auto merge of #8944 - xFrednet:8877-append-doc-idents, r=Manishearth
List configuration values can now be extended instead of replaced I've seen some `clippy.toml` files, that have a few additions to the default list of a configuration and then a copy of our default. The list will therefore not be updated, when we add new names. This change should make it simple for new users to append values instead of replacing them. I'm uncertain if the documentation of the `".."` is apparent. Any suggestions are welcome. I've also check that the lint list displays the examples correctly. <details> <summary>Lint list screenshots</summary> ![image](https://user-images.githubusercontent.com/17087237/171999434-393f2f83-09aa-4bab-8b05-bd4973150f27.png) ![image](https://user-images.githubusercontent.com/17087237/171999401-e6942b53-25e6-4b09-89e5-d867c7463156.png) </details> --- changelog: enhancement: [`doc_markdown`]: Users can now indicate, that the `doc-valid-idents` should extend the default and not replace it changelog: enhancement: [`blacklisted-name`]: Users can now indicate, that the `blacklisted-names` should extend the default and not replace it Closes: #8877 That's it. Have a fantastic weekend to everyone reading this. Here is a cookie 🍪
2 parents 0f6e50f + c31b4a9 commit ad70bff

File tree

13 files changed

+174
-24
lines changed

13 files changed

+174
-24
lines changed

clippy_lints/src/utils/conf.rs

+50-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ use std::path::{Path, PathBuf};
99
use std::str::FromStr;
1010
use std::{cmp, env, fmt, fs, io, iter};
1111

12+
#[rustfmt::skip]
13+
const DEFAULT_DOC_VALID_IDENTS: &[&str] = &[
14+
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
15+
"DirectX",
16+
"ECMAScript",
17+
"GPLv2", "GPLv3",
18+
"GitHub", "GitLab",
19+
"IPv4", "IPv6",
20+
"ClojureScript", "CoffeeScript", "JavaScript", "PureScript", "TypeScript",
21+
"NaN", "NaNs",
22+
"OAuth", "GraphQL",
23+
"OCaml",
24+
"OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenDNS",
25+
"WebGL",
26+
"TensorFlow",
27+
"TrueType",
28+
"iOS", "macOS", "FreeBSD",
29+
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
30+
"MinGW",
31+
"CamelCase",
32+
];
33+
const DEFAULT_BLACKLISTED_NAMES: &[&str] = &["foo", "baz", "quux"];
34+
1235
/// Holds information used by `MISSING_ENFORCED_IMPORT_RENAMES` lint.
1336
#[derive(Clone, Debug, Deserialize)]
1437
pub struct Rename {
@@ -178,8 +201,10 @@ define_Conf! {
178201
(msrv: Option<String> = None),
179202
/// Lint: BLACKLISTED_NAME.
180203
///
181-
/// The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses
182-
(blacklisted_names: Vec<String> = ["foo", "baz", "quux"].iter().map(ToString::to_string).collect()),
204+
/// The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses. The value
205+
/// `".."` can be used as part of the list to indicate, that the configured values should be appended to the
206+
/// default configuration of Clippy. By default any configuraction will replace the default value.
207+
(blacklisted_names: Vec<String> = super::DEFAULT_BLACKLISTED_NAMES.iter().map(ToString::to_string).collect()),
183208
/// Lint: COGNITIVE_COMPLEXITY.
184209
///
185210
/// The maximum cognitive complexity a function can have
@@ -191,27 +216,14 @@ define_Conf! {
191216
(cyclomatic_complexity_threshold: Option<u64> = None),
192217
/// Lint: DOC_MARKDOWN.
193218
///
194-
/// The list of words this lint should not consider as identifiers needing ticks
195-
(doc_valid_idents: Vec<String> = [
196-
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
197-
"DirectX",
198-
"ECMAScript",
199-
"GPLv2", "GPLv3",
200-
"GitHub", "GitLab",
201-
"IPv4", "IPv6",
202-
"ClojureScript", "CoffeeScript", "JavaScript", "PureScript", "TypeScript",
203-
"NaN", "NaNs",
204-
"OAuth", "GraphQL",
205-
"OCaml",
206-
"OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenDNS",
207-
"WebGL",
208-
"TensorFlow",
209-
"TrueType",
210-
"iOS", "macOS", "FreeBSD",
211-
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
212-
"MinGW",
213-
"CamelCase",
214-
].iter().map(ToString::to_string).collect()),
219+
/// The list of words this lint should not consider as identifiers needing ticks. The value
220+
/// `".."` can be used as part of the list to indicate, that the configured values should be appended to the
221+
/// default configuration of Clippy. By default any configuraction will replace the default value. For example:
222+
/// * `doc-valid-idents = ["ClipPy"]` would replace the default list with `["ClipPy"]`.
223+
/// * `doc-valid-idents = ["ClipPy", ".."]` would append `ClipPy` to the default list.
224+
///
225+
/// Default list:
226+
(doc_valid_idents: Vec<String> = super::DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string).collect()),
215227
/// Lint: TOO_MANY_ARGUMENTS.
216228
///
217229
/// The maximum number of argument a function or method can have
@@ -401,7 +413,21 @@ pub fn read(path: &Path) -> TryConf {
401413
Err(e) => return TryConf::from_error(e),
402414
Ok(content) => content,
403415
};
404-
toml::from_str(&content).unwrap_or_else(TryConf::from_error)
416+
match toml::from_str::<TryConf>(&content) {
417+
Ok(mut conf) => {
418+
extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS);
419+
extend_vec_if_indicator_present(&mut conf.conf.blacklisted_names, DEFAULT_BLACKLISTED_NAMES);
420+
421+
conf
422+
},
423+
Err(e) => TryConf::from_error(e),
424+
}
425+
}
426+
427+
fn extend_vec_if_indicator_present(vec: &mut Vec<String>, default: &[&str]) {
428+
if vec.contains(&"..".to_string()) {
429+
vec.extend(default.iter().map(ToString::to_string));
430+
}
405431
}
406432

407433
const SEPARATOR_WIDTH: usize = 4;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[warn(clippy::blacklisted_name)]
2+
3+
fn main() {
4+
// `foo` is part of the default configuration
5+
let foo = "bar";
6+
// `ducks` was unrightfully blacklisted
7+
let ducks = ["quack", "quack"];
8+
// `fox` is okay
9+
let fox = ["what", "does", "the", "fox", "say", "?"];
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: use of a blacklisted/placeholder name `foo`
2+
--> $DIR/blacklisted_names.rs:5:9
3+
|
4+
LL | let foo = "bar";
5+
| ^^^
6+
|
7+
= note: `-D clippy::blacklisted-name` implied by `-D warnings`
8+
9+
error: use of a blacklisted/placeholder name `ducks`
10+
--> $DIR/blacklisted_names.rs:7:9
11+
|
12+
LL | let ducks = ["quack", "quack"];
13+
| ^^^^^
14+
15+
error: aborting due to 2 previous errors
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blacklisted-names = ["ducks", ".."]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[warn(clippy::blacklisted_name)]
2+
3+
fn main() {
4+
// `foo` is part of the default configuration
5+
let foo = "bar";
6+
// `ducks` was unrightfully blacklisted
7+
let ducks = ["quack", "quack"];
8+
// `fox` is okay
9+
let fox = ["what", "does", "the", "fox", "say", "?"];
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: use of a blacklisted/placeholder name `ducks`
2+
--> $DIR/blacklisted_names.rs:7:9
3+
|
4+
LL | let ducks = ["quack", "quack"];
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::blacklisted-name` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blacklisted-names = ["ducks"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["ClipPy", ".."]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
/// This is a special interface for ClipPy which doesn't require backticks
4+
fn allowed_name() {}
5+
6+
/// OAuth and LaTeX are inside Clippy's default list.
7+
fn default_name() {}
8+
9+
/// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
10+
fn unknown_name() {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: item in documentation is missing backticks
2+
--> $DIR/doc_markdown.rs:9:5
3+
|
4+
LL | /// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::doc-markdown` implied by `-D warnings`
8+
help: try
9+
|
10+
LL | /// `TestItemThingyOfCoolness` might sound cool but is not on the list and should be linted.
11+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["ClipPy"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
/// This is a special interface for ClipPy which doesn't require backticks
4+
fn allowed_name() {}
5+
6+
/// OAuth and LaTeX are inside Clippy's default list.
7+
fn default_name() {}
8+
9+
/// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
10+
fn unknown_name() {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: item in documentation is missing backticks
2+
--> $DIR/doc_markdown.rs:6:5
3+
|
4+
LL | /// OAuth and LaTeX are inside Clippy's default list.
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::doc-markdown` implied by `-D warnings`
8+
help: try
9+
|
10+
LL | /// `OAuth` and LaTeX are inside Clippy's default list.
11+
| ~~~~~~~
12+
13+
error: item in documentation is missing backticks
14+
--> $DIR/doc_markdown.rs:6:15
15+
|
16+
LL | /// OAuth and LaTeX are inside Clippy's default list.
17+
| ^^^^^
18+
|
19+
help: try
20+
|
21+
LL | /// OAuth and `LaTeX` are inside Clippy's default list.
22+
| ~~~~~~~
23+
24+
error: item in documentation is missing backticks
25+
--> $DIR/doc_markdown.rs:9:5
26+
|
27+
LL | /// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^
29+
|
30+
help: try
31+
|
32+
LL | /// `TestItemThingyOfCoolness` might sound cool but is not on the list and should be linted.
33+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
35+
error: aborting due to 3 previous errors
36+

0 commit comments

Comments
 (0)