List configuration values can now be extended instead of replaced

This commit is contained in:
xFrednet 2022-06-04 14:35:44 +02:00
parent d9ddce8a22
commit c31b4a9d21
No known key found for this signature in database
GPG Key ID: F5C59D0E669E5302
13 changed files with 174 additions and 24 deletions

View File

@ -9,6 +9,29 @@
use std::str::FromStr;
use std::{cmp, env, fmt, fs, io, iter};
#[rustfmt::skip]
const DEFAULT_DOC_VALID_IDENTS: &[&str] = &[
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
"DirectX",
"ECMAScript",
"GPLv2", "GPLv3",
"GitHub", "GitLab",
"IPv4", "IPv6",
"ClojureScript", "CoffeeScript", "JavaScript", "PureScript", "TypeScript",
"NaN", "NaNs",
"OAuth", "GraphQL",
"OCaml",
"OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenDNS",
"WebGL",
"TensorFlow",
"TrueType",
"iOS", "macOS", "FreeBSD",
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
"MinGW",
"CamelCase",
];
const DEFAULT_BLACKLISTED_NAMES: &[&str] = &["foo", "baz", "quux"];
/// Holds information used by `MISSING_ENFORCED_IMPORT_RENAMES` lint.
#[derive(Clone, Debug, Deserialize)]
pub struct Rename {
@ -178,8 +201,10 @@ pub(crate) fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
(msrv: Option<String> = None),
/// Lint: BLACKLISTED_NAME.
///
/// The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses
(blacklisted_names: Vec<String> = ["foo", "baz", "quux"].iter().map(ToString::to_string).collect()),
/// The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses. The value
/// `".."` can be used as part of the list to indicate, that the configured values should be appended to the
/// default configuration of Clippy. By default any configuraction will replace the default value.
(blacklisted_names: Vec<String> = super::DEFAULT_BLACKLISTED_NAMES.iter().map(ToString::to_string).collect()),
/// Lint: COGNITIVE_COMPLEXITY.
///
/// The maximum cognitive complexity a function can have
@ -191,27 +216,14 @@ pub(crate) fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
(cyclomatic_complexity_threshold: Option<u64> = None),
/// Lint: DOC_MARKDOWN.
///
/// The list of words this lint should not consider as identifiers needing ticks
(doc_valid_idents: Vec<String> = [
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
"DirectX",
"ECMAScript",
"GPLv2", "GPLv3",
"GitHub", "GitLab",
"IPv4", "IPv6",
"ClojureScript", "CoffeeScript", "JavaScript", "PureScript", "TypeScript",
"NaN", "NaNs",
"OAuth", "GraphQL",
"OCaml",
"OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenDNS",
"WebGL",
"TensorFlow",
"TrueType",
"iOS", "macOS", "FreeBSD",
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
"MinGW",
"CamelCase",
].iter().map(ToString::to_string).collect()),
/// The list of words this lint should not consider as identifiers needing ticks. The value
/// `".."` can be used as part of the list to indicate, that the configured values should be appended to the
/// default configuration of Clippy. By default any configuraction will replace the default value. For example:
/// * `doc-valid-idents = ["ClipPy"]` would replace the default list with `["ClipPy"]`.
/// * `doc-valid-idents = ["ClipPy", ".."]` would append `ClipPy` to the default list.
///
/// Default list:
(doc_valid_idents: Vec<String> = super::DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string).collect()),
/// Lint: TOO_MANY_ARGUMENTS.
///
/// The maximum number of argument a function or method can have
@ -401,7 +413,21 @@ pub fn read(path: &Path) -> TryConf {
Err(e) => return TryConf::from_error(e),
Ok(content) => content,
};
toml::from_str(&content).unwrap_or_else(TryConf::from_error)
match toml::from_str::<TryConf>(&content) {
Ok(mut conf) => {
extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS);
extend_vec_if_indicator_present(&mut conf.conf.blacklisted_names, DEFAULT_BLACKLISTED_NAMES);
conf
},
Err(e) => TryConf::from_error(e),
}
}
fn extend_vec_if_indicator_present(vec: &mut Vec<String>, default: &[&str]) {
if vec.contains(&"..".to_string()) {
vec.extend(default.iter().map(ToString::to_string));
}
}
const SEPARATOR_WIDTH: usize = 4;

View File

@ -0,0 +1,10 @@
#[warn(clippy::blacklisted_name)]
fn main() {
// `foo` is part of the default configuration
let foo = "bar";
// `ducks` was unrightfully blacklisted
let ducks = ["quack", "quack"];
// `fox` is okay
let fox = ["what", "does", "the", "fox", "say", "?"];
}

View File

@ -0,0 +1,16 @@
error: use of a blacklisted/placeholder name `foo`
--> $DIR/blacklisted_names.rs:5:9
|
LL | let foo = "bar";
| ^^^
|
= note: `-D clippy::blacklisted-name` implied by `-D warnings`
error: use of a blacklisted/placeholder name `ducks`
--> $DIR/blacklisted_names.rs:7:9
|
LL | let ducks = ["quack", "quack"];
| ^^^^^
error: aborting due to 2 previous errors

View File

@ -0,0 +1 @@
blacklisted-names = ["ducks", ".."]

View File

@ -0,0 +1,10 @@
#[warn(clippy::blacklisted_name)]
fn main() {
// `foo` is part of the default configuration
let foo = "bar";
// `ducks` was unrightfully blacklisted
let ducks = ["quack", "quack"];
// `fox` is okay
let fox = ["what", "does", "the", "fox", "say", "?"];
}

View File

@ -0,0 +1,10 @@
error: use of a blacklisted/placeholder name `ducks`
--> $DIR/blacklisted_names.rs:7:9
|
LL | let ducks = ["quack", "quack"];
| ^^^^^
|
= note: `-D clippy::blacklisted-name` implied by `-D warnings`
error: aborting due to previous error

View File

@ -0,0 +1 @@
blacklisted-names = ["ducks"]

View File

@ -0,0 +1 @@
doc-valid-idents = ["ClipPy", ".."]

View File

@ -0,0 +1,12 @@
#![warn(clippy::doc_markdown)]
/// This is a special interface for ClipPy which doesn't require backticks
fn allowed_name() {}
/// OAuth and LaTeX are inside Clippy's default list.
fn default_name() {}
/// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
fn unknown_name() {}
fn main() {}

View File

@ -0,0 +1,14 @@
error: item in documentation is missing backticks
--> $DIR/doc_markdown.rs:9:5
|
LL | /// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::doc-markdown` implied by `-D warnings`
help: try
|
LL | /// `TestItemThingyOfCoolness` might sound cool but is not on the list and should be linted.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

View File

@ -0,0 +1 @@
doc-valid-idents = ["ClipPy"]

View File

@ -0,0 +1,12 @@
#![warn(clippy::doc_markdown)]
/// This is a special interface for ClipPy which doesn't require backticks
fn allowed_name() {}
/// OAuth and LaTeX are inside Clippy's default list.
fn default_name() {}
/// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
fn unknown_name() {}
fn main() {}

View File

@ -0,0 +1,36 @@
error: item in documentation is missing backticks
--> $DIR/doc_markdown.rs:6:5
|
LL | /// OAuth and LaTeX are inside Clippy's default list.
| ^^^^^
|
= note: `-D clippy::doc-markdown` implied by `-D warnings`
help: try
|
LL | /// `OAuth` and LaTeX are inside Clippy's default list.
| ~~~~~~~
error: item in documentation is missing backticks
--> $DIR/doc_markdown.rs:6:15
|
LL | /// OAuth and LaTeX are inside Clippy's default list.
| ^^^^^
|
help: try
|
LL | /// OAuth and `LaTeX` are inside Clippy's default list.
| ~~~~~~~
error: item in documentation is missing backticks
--> $DIR/doc_markdown.rs:9:5
|
LL | /// TestItemThingyOfCoolness might sound cool but is not on the list and should be linted.
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | /// `TestItemThingyOfCoolness` might sound cool but is not on the list and should be linted.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors