Add AcceptContext::expect_no_args

This commit is contained in:
Sasha Pourcelot
2026-04-28 14:01:03 +00:00
parent f53b654a88
commit f77fc05d25
13 changed files with 36 additions and 56 deletions
@@ -52,10 +52,7 @@ fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Attribute
cx.adcx().expected_identifier(mode.span());
return None;
};
let Ok(()) = mode.args().no_args() else {
cx.adcx().expected_identifier(mode.span());
return None;
};
cx.expect_no_args(mode.args())?;
let Some(mode) = mode.path().word() else {
cx.adcx().expected_identifier(mode.span());
return None;
@@ -85,11 +82,7 @@ fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Attribute
.expected_specific_argument(activity.span(), DiffActivity::all_activities());
return None;
};
let Ok(()) = activity.args().no_args() else {
cx.adcx()
.expected_specific_argument(activity.span(), DiffActivity::all_activities());
return None;
};
cx.expect_no_args(activity.args())?;
let Some(activity) = activity.path().word() else {
cx.adcx()
.expected_specific_argument(activity.span(), DiffActivity::all_activities());
@@ -195,10 +195,9 @@ pub(crate) struct NakedParser {
impl AttributeParser for NakedParser {
const ATTRIBUTES: AcceptMapping<Self> =
&[(&[sym::naked], template!(Word), |this, cx, args| {
if let Err(span) = args.no_args() {
cx.adcx().expected_no_args(span);
let Some(()) = cx.expect_no_args(args) else {
return;
}
};
if let Some(earlier) = this.span {
let span = cx.attr_span;
@@ -299,11 +299,9 @@ fn extend(
cx.adcx().expected_identifier(elem.span());
continue;
};
if let Err(arg_span) = elem.args().no_args() {
cx.adcx().expected_no_args(arg_span);
let Some(()) = cx.expect_no_args(elem.args()) else {
continue;
}
};
let path = elem.path();
let Some(ident) = path.word() else {
cx.adcx().expected_identifier(path.span());
@@ -345,10 +343,9 @@ fn extend(
cx.adcx().expected_identifier(elem.span());
continue;
};
if let Err(arg_span) = elem.args().no_args() {
cx.adcx().expected_no_args(arg_span);
let Some(()) = cx.expect_no_args(elem.args()) else {
continue;
}
};
let path = elem.path();
let Some(ident) = path.word() else {
@@ -162,7 +162,7 @@ fn parse_single_test_doc_attr_item(
match path.word_sym() {
Some(sym::no_crate_inject) => {
if let Err(span) = args.no_args() {
if let Err(span) = args.as_no_args() {
expected_no_args(cx, span);
return;
}
@@ -295,7 +295,7 @@ fn parse_inline(
args: &ArgParser,
inline: DocInline,
) {
if let Err(span) = args.no_args() {
if let Err(span) = args.as_no_args() {
expected_no_args(cx, span);
return;
}
@@ -449,7 +449,7 @@ fn parse_single_doc_attr_item(&mut self, cx: &mut AcceptContext<'_, '_>, mip: &M
macro_rules! no_args {
($ident: ident) => {{
if let Err(span) = args.no_args() {
if let Err(span) = args.as_no_args() {
expected_no_args(cx, span);
return;
}
@@ -468,7 +468,7 @@ macro_rules! no_args {
}
macro_rules! no_args_and_not_crate_level {
($ident: ident) => {{
if let Err(span) = args.no_args() {
if let Err(span) = args.as_no_args() {
expected_no_args(cx, span);
return;
}
@@ -484,7 +484,7 @@ macro_rules! no_args_and_crate_level {
no_args_and_crate_level!($ident, |span| {});
}};
($ident: ident, |$span:ident| $extra_validation:block) => {{
if let Err(span) = args.no_args() {
if let Err(span) = args.as_no_args() {
expected_no_args(cx, span);
return;
}
@@ -86,10 +86,9 @@ impl AttributeParser for MacroUseParser {
cx.adcx().expected_identifier(item.span());
continue;
};
if let Err(err_span) = item.args().no_args() {
cx.adcx().expected_no_args(err_span);
let Some(()) = cx.expect_no_args(item.args()) else {
continue;
}
};
let Some(item) = item.path().word() else {
cx.adcx().expected_identifier(item.span());
continue;
@@ -179,9 +178,7 @@ fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Attribute
cx.adcx().expected_not_literal(single.span());
return None;
};
if let Err(err) = mi.args().no_args() {
cx.adcx().expected_no_args(err);
}
let _ = cx.expect_no_args(mi.args());
let path = mi.path().word_sym();
let info = match path {
Some(sym::yes) => CollapseMacroDebuginfo::Yes,
@@ -258,9 +258,7 @@ impl<T: NoArgsAttributeParser> SingleAttributeParser for WithoutArgs<T> {
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
if let Err(span) = args.no_args() {
cx.adcx().expected_no_args(span);
}
let _ = cx.expect_no_args(args);
Some(T::CREATE(cx.attr_span))
}
}
@@ -59,7 +59,7 @@ fn parse_derive_like(
) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
let Some(list) = args.as_list() else {
// For #[rustc_builtin_macro], it is permitted to leave out the trait name
if args.no_args().is_ok() && !trait_name_mandatory {
if args.as_no_args().is_ok() && !trait_name_mandatory {
return Some((None, ThinVec::new()));
}
let attr_span = cx.attr_span;
@@ -85,10 +85,7 @@ fn parse_derive_like(
cx.adcx().expected_identifier(trait_ident.span);
return None;
}
if let Err(e) = trait_attr.args().no_args() {
cx.adcx().expected_no_args(e);
return None;
};
cx.expect_no_args(trait_attr.args())?;
// Parse optional attributes
let mut attributes = ThinVec::new();
@@ -109,10 +106,7 @@ fn parse_derive_like(
cx.adcx().expected_identifier(attr.span());
return None;
};
if let Err(e) = attr.args().no_args() {
cx.adcx().expected_no_args(e);
return None;
};
cx.expect_no_args(attr.args())?;
let Some(ident) = attr.path().word() else {
cx.adcx().expected_identifier(attr.path().span());
return None;
@@ -36,10 +36,7 @@ impl SingleAttributeParser for RustcDumpDefPathParser {
]);
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
if let Err(span) = args.no_args() {
cx.adcx().expected_no_args(span);
return None;
}
cx.expect_no_args(args)?;
Some(AttributeKind::RustcDumpDefPath(cx.attr_span))
}
}
@@ -203,10 +200,7 @@ impl SingleAttributeParser for RustcDumpSymbolNameParser {
]);
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
if let Err(span) = args.no_args() {
cx.adcx().expected_no_args(span);
return None;
}
cx.expect_no_args(args)?;
Some(AttributeKind::RustcDumpSymbolName(cx.attr_span))
}
}
@@ -555,7 +555,7 @@ impl SingleAttributeParser for RustcScalableVectorParser {
const TEMPLATE: AttributeTemplate = template!(Word, List: &["count"]);
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
if args.no_args().is_ok() {
if args.as_no_args().is_ok() {
return Some(AttributeKind::RustcScalableVector {
element_count: None,
span: cx.attr_span,
@@ -30,7 +30,7 @@ fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Attribute
ArgParser::List(list) => {
let help =
list.as_single().and_then(|item| item.meta_item()).and_then(|item| {
item.args().no_args().ok()?;
item.args().as_no_args().ok()?;
Some(item.path().to_string())
});
cx.adcx().warn_ill_formed_attribute_input_with_help(
@@ -27,9 +27,7 @@ fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Attribute
cx.adcx().expected_not_literal(arg.span());
continue;
};
if let Err(span) = arg.args().no_args() {
cx.adcx().expected_no_args(span);
}
let _ = cx.expect_no_args(arg.args());
let path = arg.path();
let (key, skip): (Symbol, &mut bool) = match path.word_sym() {
Some(key @ sym::array) => (key, &mut array),
@@ -570,6 +570,16 @@ pub(crate) fn expect_name_value<'arg, Arg>(
{
arg.expect_name_value(self, span, name)
}
/// Assert that an [`ArgParser`] has no args, or emits an error and return `None`.
pub(crate) fn expect_no_args<'arg>(&mut self, arg: &'arg ArgParser) -> Option<()> {
if let Err(span) = arg.as_no_args() {
self.adcx().expected_no_args(span);
return None;
}
Some(())
}
}
pub(crate) trait ExpectNameValue {
+1 -1
View File
@@ -202,7 +202,7 @@ pub fn as_name_value(&self) -> Option<&NameValueParser> {
/// Assert that there were no args.
/// If there were, get a span to the arguments
/// (to pass to [`AttributeDiagnosticContext::expected_no_args`](crate::context::AttributeDiagnosticContext::expected_no_args)).
pub fn no_args(&self) -> Result<(), Span> {
pub fn as_no_args(&self) -> Result<(), Span> {
match self {
Self::NoArgs => Ok(()),
Self::List(args) => Err(args.span),