Skip to content

Commit ea25ef1

Browse files
committed
Harden duplicates checking and add tests
1 parent 08e7ec4 commit ea25ef1

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

clippy_lints/src/utils/conf.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl fmt::Display for ConfError {
9292

9393
impl Error for ConfError {}
9494

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

9999
macro_rules! define_Conf {
@@ -154,7 +154,13 @@ macro_rules! define_Conf {
154154
$name = Some(value);
155155
// $new_conf is the same as one of the defined `$name`s, so
156156
// this variable is defined in line 2 of this function.
157-
$($new_conf = Some(value);)?
157+
$(match $new_conf {
158+
Some(_) => errors.push(conf_error(concat!(
159+
"duplicate field `", stringify!($new_conf),
160+
"` (provided as `", stringify!($name), "`)"
161+
))),
162+
None => $new_conf = Some(value),
163+
})?
158164
},
159165
}
160166
}

tests/ui-toml/conf_deprecated_key/clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# that one is an error
1+
# that one is a warning
22
cyclomatic-complexity-threshold = 2
33

44
# that one is white-listed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cognitive-complexity-threshold = 2
2+
# This is the deprecated name for the same key
3+
cyclomatic-complexity-threshold = 3
4+
# Check we get duplication warning regardless of order
5+
cognitive-complexity-threshold = 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive_complexity_threshold` (provided as `cyclomatic_complexity_threshold`)
2+
3+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive-complexity-threshold`
4+
5+
warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead
6+
7+
error: aborting due to 2 previous errors; 1 warning emitted
8+

0 commit comments

Comments
 (0)