Skip to content

Commit c31b4a9

Browse files
committed
List configuration values can now be extended instead of replaced
1 parent d9ddce8 commit c31b4a9

File tree

13 files changed

+174
-24
lines changed

13 files changed

+174
-24
lines changed

clippy_lints/src/utils/conf.rs

Lines changed: 50 additions & 24 deletions
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;
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blacklisted-names = ["ducks", ".."]
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blacklisted-names = ["ducks"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["ClipPy", ".."]
Lines changed: 12 additions & 0 deletions
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() {}
Lines changed: 14 additions & 0 deletions
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+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["ClipPy"]
Lines changed: 12 additions & 0 deletions
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() {}
Lines changed: 36 additions & 0 deletions
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)