Improve suggestions for cfg_attr

Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
This commit is contained in:
Jonathan Brouwer
2025-10-22 21:57:13 +02:00
parent 9735524533
commit 24502dd2d2
4 changed files with 24 additions and 10 deletions
@@ -389,6 +389,7 @@ fn parse_cfg_attr_internal<'a>(
let cfg_predicate = AttributeParser::parse_single_args(
sess,
attribute.span,
attribute.get_normal_item().span(),
attribute.style,
AttrPath {
segments: attribute
+13 -2
View File
@@ -337,8 +337,16 @@ pub struct Early {
/// Gives [`AttributeParser`]s enough information to create errors, for example.
pub struct AcceptContext<'f, 'sess, S: Stage> {
pub(crate) shared: SharedContext<'f, 'sess, S>,
/// The span of the attribute currently being parsed
/// The outer span of the attribute currently being parsed
/// #[attribute(...)]
/// ^^^^^^^^^^^^^^^^^ outer span
/// For attributes in `cfg_attr`, the outer span and inner spans are equal.
pub(crate) attr_span: Span,
/// The inner span of the attribute currently being parsed
/// #[attribute(...)]
/// ^^^^^^^^^^^^^^ inner span
pub(crate) inner_span: Span,
/// Whether it is an inner or outer attribute
pub(crate) attr_style: AttrStyle,
@@ -607,7 +615,10 @@ pub(crate) fn warn_empty_attribute(&mut self, span: Span) {
}
pub(crate) fn suggestions(&self) -> Vec<String> {
self.template.suggestions(Some(self.attr_style), &self.attr_path)
// If the outer and inner spans are equal, we are parsing an attribute from `cfg_attr`,
// So don't display an attribute style in the suggestions
let style = (self.attr_span != self.inner_span).then_some(self.attr_style);
self.template.suggestions(style, &self.attr_path)
}
}
@@ -142,6 +142,7 @@ pub fn parse_single<T>(
Self::parse_single_args(
sess,
attr.span,
normal_attr.item.span(),
attr.style,
path.get_attribute_path(),
target_span,
@@ -159,6 +160,7 @@ pub fn parse_single<T>(
pub fn parse_single_args<T, I>(
sess: &'sess Session,
attr_span: Span,
inner_span: Span,
attr_style: AttrStyle,
attr_path: AttrPath,
target_span: Span,
@@ -186,6 +188,7 @@ pub fn parse_single_args<T, I>(
},
},
attr_span,
inner_span,
attr_style,
template,
attr_path,
@@ -305,6 +308,7 @@ pub fn parse_attribute_list(
emit_lint: &mut emit_lint,
},
attr_span: lower_span(attr.span),
inner_span: lower_span(attr.get_normal_item().span()),
attr_style: attr.style,
template: &accept.template,
attr_path: path.get_attribute_path(),
@@ -113,7 +113,7 @@ error[E0539]: malformed `link_section` attribute input
--> $DIR/cfg_attr-attr-syntax-validation.rs:44:18
|
LL | #[cfg_attr(true, link_section)]
| ^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]`
| ^^^^^^^^^^^^ help: must be of the form: `link_section = "name"`
|
= note: for more information, visit <https://doc.rust-lang.org/reference/abi.html#the-link_section-attribute>
@@ -129,14 +129,12 @@ LL | #[cfg_attr(true, inline())]
help: try changing it to one of the following valid forms of the attribute
|
LL - #[cfg_attr(true, inline())]
LL + #[cfg_attr(true, #[inline(always)])]
|
LL - #[cfg_attr(true, inline())]
LL + #[cfg_attr(true, #[inline(never)])]
|
LL - #[cfg_attr(true, inline())]
LL + #[cfg_attr(true, #[inline])]
LL + #[cfg_attr(true, inline)]
|
LL | #[cfg_attr(true, inline(always))]
| ++++++
LL | #[cfg_attr(true, inline(never))]
| +++++
warning: `#[link_section]` attribute cannot be used on structs
--> $DIR/cfg_attr-attr-syntax-validation.rs:44:18