Skip to content

Commit 08e7ec4

Browse files
flip1995bossmc
andcommitted
Read and use deprecated configuration (as well as emitting a warning)
Co-authored-by: Andy Caldwell <andycaldwell@microsoft.com>
1 parent d814681 commit 08e7ec4

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

clippy_lints/src/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub fn read_conf(sess: &Session) -> Conf {
487487
},
488488
};
489489

490-
let TryConf { conf, errors } = utils::conf::read(&file_name);
490+
let TryConf { conf, errors, warnings } = utils::conf::read(&file_name);
491491
// all conf errors are non-fatal, we just use the default conf in case of error
492492
for error in errors {
493493
sess.err(&format!(
@@ -497,6 +497,15 @@ pub fn read_conf(sess: &Session) -> Conf {
497497
));
498498
}
499499

500+
for warning in warnings {
501+
sess.struct_warn(&format!(
502+
"error reading Clippy's configuration file `{}`: {}",
503+
file_name.display(),
504+
format_error(warning)
505+
))
506+
.emit();
507+
}
508+
500509
conf
501510
}
502511

clippy_lints/src/utils/conf.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ pub enum DisallowedType {
6868
pub struct TryConf {
6969
pub conf: Conf,
7070
pub errors: Vec<Box<dyn Error>>,
71+
pub warnings: Vec<Box<dyn Error>>,
7172
}
7273

7374
impl TryConf {
7475
fn from_error(error: impl Error + 'static) -> Self {
7576
Self {
7677
conf: Conf::default(),
7778
errors: vec![Box::new(error)],
79+
warnings: vec![],
7880
}
7981
}
8082
}
@@ -97,7 +99,7 @@ fn conf_error(s: String) -> Box<dyn Error> {
9799
macro_rules! define_Conf {
98100
($(
99101
$(#[doc = $doc:literal])+
100-
$(#[conf_deprecated($dep:literal)])?
102+
$(#[conf_deprecated($dep:literal, $new_conf:ident)])?
101103
($name:ident: $ty:ty = $default:expr),
102104
)*) => {
103105
/// Clippy lint configuration
@@ -137,17 +139,23 @@ macro_rules! define_Conf {
137139

138140
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error> where V: MapAccess<'de> {
139141
let mut errors = Vec::new();
142+
let mut warnings = Vec::new();
140143
$(let mut $name = None;)*
141144
// could get `Field` here directly, but get `str` first for diagnostics
142145
while let Some(name) = map.next_key::<&str>()? {
143146
match Field::deserialize(name.into_deserializer())? {
144147
$(Field::$name => {
145-
$(errors.push(conf_error(format!("deprecated field `{}`. {}", name, $dep)));)?
148+
$(warnings.push(conf_error(format!("deprecated field `{}`. {}", name, $dep)));)?
146149
match map.next_value() {
147150
Err(e) => errors.push(conf_error(e.to_string())),
148151
Ok(value) => match $name {
149152
Some(_) => errors.push(conf_error(format!("duplicate field `{}`", name))),
150-
None => $name = Some(value),
153+
None => {
154+
$name = Some(value);
155+
// $new_conf is the same as one of the defined `$name`s, so
156+
// this variable is defined in line 2 of this function.
157+
$($new_conf = Some(value);)?
158+
},
151159
}
152160
}
153161
})*
@@ -156,7 +164,7 @@ macro_rules! define_Conf {
156164
}
157165
}
158166
let conf = Conf { $($name: $name.unwrap_or_else(defaults::$name),)* };
159-
Ok(TryConf { conf, errors })
167+
Ok(TryConf { conf, errors, warnings })
160168
}
161169
}
162170

@@ -216,8 +224,8 @@ define_Conf! {
216224
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY.
217225
///
218226
/// Use the Cognitive Complexity lint instead.
219-
#[conf_deprecated("Please use `cognitive-complexity-threshold` instead")]
220-
(cyclomatic_complexity_threshold: Option<u64> = None),
227+
#[conf_deprecated("Please use `cognitive-complexity-threshold` instead", cognitive_complexity_threshold)]
228+
(cyclomatic_complexity_threshold: u64 = 25),
221229
/// Lint: DOC_MARKDOWN.
222230
///
223231
/// The list of words this lint should not consider as identifiers needing ticks. The value

tests/ui-toml/conf_deprecated_key/clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# that one is an error
2-
cyclomatic-complexity-threshold = 42
2+
cyclomatic-complexity-threshold = 2
33

44
# that one is white-listed
55
[third-party]
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
fn main() {}
2+
3+
#[warn(clippy::cognitive_complexity)]
4+
fn cognitive_complexity() {
5+
let x = vec![1, 2, 3];
6+
for i in x {
7+
if i == 1 {
8+
println!("{}", i);
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead
1+
warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead
22

3-
error: aborting due to previous error
3+
error: the function has a cognitive complexity of (3/2)
4+
--> $DIR/conf_deprecated_key.rs:4:4
5+
|
6+
LL | fn cognitive_complexity() {
7+
| ^^^^^^^^^^^^^^^^^^^^
8+
|
9+
= note: `-D clippy::cognitive-complexity` implied by `-D warnings`
10+
= help: you could split it up into multiple smaller functions
11+
12+
error: aborting due to previous error; 1 warning emitted
413

0 commit comments

Comments
 (0)