Skip to content

Commit

Permalink
Harden duplicates checking and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bossmc committed Jul 28, 2022
1 parent 08e7ec4 commit ea25ef1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
12 changes: 9 additions & 3 deletions clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl fmt::Display for ConfError {

impl Error for ConfError {}

fn conf_error(s: String) -> Box<dyn Error> {
Box::new(ConfError(s))
fn conf_error(s: impl Into<String>) -> Box<dyn Error> {
Box::new(ConfError(s.into()))
}

macro_rules! define_Conf {
Expand Down Expand Up @@ -154,7 +154,13 @@ macro_rules! define_Conf {
$name = Some(value);
// $new_conf is the same as one of the defined `$name`s, so
// this variable is defined in line 2 of this function.
$($new_conf = Some(value);)?
$(match $new_conf {
Some(_) => errors.push(conf_error(concat!(
"duplicate field `", stringify!($new_conf),
"` (provided as `", stringify!($name), "`)"
))),
None => $new_conf = Some(value),
})?
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/conf_deprecated_key/clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# that one is an error
# that one is a warning
cyclomatic-complexity-threshold = 2

# that one is white-listed
Expand Down
5 changes: 5 additions & 0 deletions tests/ui-toml/duplicated_keys/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cognitive-complexity-threshold = 2
# This is the deprecated name for the same key
cyclomatic-complexity-threshold = 3
# Check we get duplication warning regardless of order
cognitive-complexity-threshold = 4
1 change: 1 addition & 0 deletions tests/ui-toml/duplicated_keys/duplicated_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
8 changes: 8 additions & 0 deletions tests/ui-toml/duplicated_keys/duplicated_keys.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive_complexity_threshold` (provided as `cyclomatic_complexity_threshold`)

error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive-complexity-threshold`

warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead

error: aborting due to 2 previous errors; 1 warning emitted

0 comments on commit ea25ef1

Please sign in to comment.