diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 25163bb9f3797..fd2dddb3b96e5 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -92,25 +92,26 @@ macro_rules! define_Conf { #[cfg(feature = "metadata-collector-lint")] pub mod metadata { - use crate::utils::internal_lints::metadata_collector::ClippyConfigurationBasicInfo; + use crate::utils::internal_lints::metadata_collector::ClippyConfiguration; - pub(crate) fn get_configuration_metadata() -> Vec { + macro_rules! wrap_option { + () => (None); + ($x:literal) => (Some($x)); + } + + pub(crate) fn get_configuration_metadata() -> Vec { vec![ $( { - #[allow(unused_mut, unused_assignments)] - let mut deprecation_reason = None; + let deprecation_reason = wrap_option!($($dep)?); - // only set if a deprecation reason was set - $(deprecation_reason = Some(stringify!($dep));)? - - ClippyConfigurationBasicInfo { - name: stringify!($name), - config_type: stringify!($ty), - default: stringify!($default), - doc_comment: $doc, + ClippyConfiguration::new( + stringify!($name), + stringify!($ty), + format!("{:?}", super::defaults::$name()), + $doc, deprecation_reason, - } + ) }, )+ ] diff --git a/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/clippy_lints/src/utils/internal_lints/metadata_collector.rs index bd0de8ad0341a..b7c86ae6e1844 100644 --- a/clippy_lints/src/utils/internal_lints/metadata_collector.rs +++ b/clippy_lints/src/utils/internal_lints/metadata_collector.rs @@ -149,7 +149,8 @@ impl MetadataCollector { fn get_lint_configs(&self, lint_name: &str) -> Option { self.config .iter() - .filter_map(|x| x.lints.iter().any(|x| x == lint_name).then(|| format!("{}", x))) + .filter(|config| config.lints.iter().any(|lint| lint == lint_name)) + .map(ToString::to_string) .reduce(|acc, x| acc + &x) .map(|configurations| format!(CONFIGURATION_SECTION_TEMPLATE!(), configurations = configurations)) } @@ -261,52 +262,40 @@ impl Serialize for ApplicabilityInfo { // ================================================================== // Configuration // ================================================================== -#[derive(Debug)] -pub(crate) struct ClippyConfigurationBasicInfo { - pub name: &'static str, - pub config_type: &'static str, - pub default: &'static str, - pub doc_comment: &'static str, - pub deprecation_reason: Option<&'static str>, -} - #[derive(Debug, Clone, Default)] -struct ClippyConfiguration { +pub struct ClippyConfiguration { name: String, - lints: Vec, - doc: String, config_type: &'static str, default: String, + lints: Vec, + doc: String, deprecation_reason: Option<&'static str>, } -fn collect_configs() -> Vec { - let cons = crate::utils::conf::metadata::get_configuration_metadata(); - cons.iter() - .map(move |x| { - let (lints, doc) = parse_config_field_doc(x.doc_comment) - .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string())); - - ClippyConfiguration { - name: to_kebab(x.name), - lints, - doc, - config_type: x.config_type, - default: clarify_default(x.default), - deprecation_reason: x.deprecation_reason, - } - }) - .collect() -} +impl ClippyConfiguration { + pub fn new( + name: &'static str, + config_type: &'static str, + default: String, + doc_comment: &'static str, + deprecation_reason: Option<&'static str>, + ) -> Self { + let (lints, doc) = parse_config_field_doc(doc_comment) + .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string())); -fn clarify_default(default: &'static str) -> String { - if let Some((_start, init)) = default.split_once('[') { - if let Some((init, _end)) = init.split_once(']') { - return format!("[{}]", init); + Self { + name: to_kebab(name), + lints, + doc, + config_type, + default, + deprecation_reason, } } +} - default.to_string() +fn collect_configs() -> Vec { + crate::utils::conf::metadata::get_configuration_metadata() } /// This parses the field documentation of the config struct.