Improve readability of parse_check_cfg.

This commit is contained in:
Nicholas Nethercote 2023-10-30 15:06:48 +11:00
parent 5c6a12c1af
commit 371f972571

View File

@ -164,34 +164,31 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
expected_error(); expected_error();
}; };
if meta_item.has_name(sym::names) { let mut set_old_syntax = || {
// defaults are flipped for the old syntax // defaults are flipped for the old syntax
if old_syntax == None { if old_syntax == None {
check_cfg.exhaustive_names = false; check_cfg.exhaustive_names = false;
check_cfg.exhaustive_values = false; check_cfg.exhaustive_values = false;
} }
old_syntax = Some(true); old_syntax = Some(true);
};
if meta_item.has_name(sym::names) {
set_old_syntax();
check_cfg.exhaustive_names = true; check_cfg.exhaustive_names = true;
for arg in args { for arg in args {
if arg.is_word() && arg.ident().is_some() { if arg.is_word() && let Some(ident) = arg.ident() {
let ident = arg.ident().expect("multi-segment cfg key");
check_cfg.expecteds.entry(ident.name).or_insert(ExpectedValues::Any); check_cfg.expecteds.entry(ident.name).or_insert(ExpectedValues::Any);
} else { } else {
error!("`names()` arguments must be simple identifiers"); error!("`names()` arguments must be simple identifiers");
} }
} }
} else if meta_item.has_name(sym::values) { } else if meta_item.has_name(sym::values) {
// defaults are flipped for the old syntax set_old_syntax();
if old_syntax == None {
check_cfg.exhaustive_names = false;
check_cfg.exhaustive_values = false;
}
old_syntax = Some(true);
if let Some((name, values)) = args.split_first() { if let Some((name, values)) = args.split_first() {
if name.is_word() && name.ident().is_some() { if name.is_word() && let Some(ident) = name.ident() {
let ident = name.ident().expect("multi-segment cfg key");
let expected_values = check_cfg let expected_values = check_cfg
.expecteds .expecteds
.entry(ident.name) .entry(ident.name)
@ -244,9 +241,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
error!("`cfg()` names cannot be after values"); error!("`cfg()` names cannot be after values");
} }
names.push(ident); names.push(ident);
} else if arg.has_name(sym::any) } else if arg.has_name(sym::any) && let Some(args) = arg.meta_item_list() {
&& let Some(args) = arg.meta_item_list()
{
if any_specified { if any_specified {
error!("`any()` cannot be specified multiple times"); error!("`any()` cannot be specified multiple times");
} }
@ -254,9 +249,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
if !args.is_empty() { if !args.is_empty() {
error!("`any()` must be empty"); error!("`any()` must be empty");
} }
} else if arg.has_name(sym::values) } else if arg.has_name(sym::values) && let Some(args) = arg.meta_item_list() {
&& let Some(args) = arg.meta_item_list()
{
if names.is_empty() { if names.is_empty() {
error!("`values()` cannot be specified before the names"); error!("`values()` cannot be specified before the names");
} else if values_specified { } else if values_specified {
@ -267,22 +260,16 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
for arg in args { for arg in args {
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) { if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
values.insert(Some(*s)); values.insert(Some(*s));
} else if arg.has_name(sym::any) } else if arg.has_name(sym::any) && let Some(args) = arg.meta_item_list() {
&& let Some(args) = arg.meta_item_list()
{
if values_any_specified { if values_any_specified {
error!( error!("`any()` in `values()` cannot be specified multiple times");
"`any()` in `values()` cannot be specified multiple times"
);
} }
values_any_specified = true; values_any_specified = true;
if !args.is_empty() { if !args.is_empty() {
error!("`any()` must be empty"); error!("`any()` must be empty");
} }
} else { } else {
error!( error!("`values()` arguments must be string literals or `any()`");
"`values()` arguments must be string literals or `any()`"
);
} }
} }
} else { } else {