Skip to content

Commit

Permalink
Skip parsing attributes from rustfmt or clippy (serenity-rs#2473)
Browse files Browse the repository at this point in the history
This alters attribute parse code in `command_attr` to not parse attributes like `#[rustfmt::skip]` as its own, and instead should emit them in the output of the macros.
  • Loading branch information
arqunis authored and mkrasnitski committed Sep 21, 2023
1 parent ee74fc2 commit 1bc8d70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions command_attr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut options = Options::new();

for attribute in &fun.attributes {
if is_rustfmt_or_clippy_attr(&attribute.path) {
continue;
}

let span = attribute.span();
let values = propagate_err!(parse_values(attribute));

Expand Down Expand Up @@ -311,6 +315,10 @@ pub fn help(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut options = HelpOptions::default();

for attribute in &fun.attributes {
if is_rustfmt_or_clippy_attr(&attribute.path) {
continue;
}

let span = attribute.span();
let values = propagate_err!(parse_values(attribute));

Expand Down Expand Up @@ -615,6 +623,10 @@ pub fn group(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut options = GroupOptions::new();

for attribute in &group.attributes {
if is_rustfmt_or_clippy_attr(&attribute.path) {
continue;
}

let span = attribute.span();
let values = propagate_err!(parse_values(attribute));

Expand Down Expand Up @@ -741,6 +753,10 @@ pub fn check(_attr: TokenStream, input: TokenStream) -> TokenStream {
let mut check_in_help = true;

for attribute in &fun.attributes {
if is_rustfmt_or_clippy_attr(&attribute.path) {
continue;
}

let span = attribute.span();
let values = propagate_err!(parse_values(attribute));

Expand Down
7 changes: 6 additions & 1 deletion command_attr/src/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use syn::{
FnArg,
Ident,
Pat,
Path,
ReturnType,
Stmt,
Token,
Expand Down Expand Up @@ -99,6 +100,10 @@ fn is_cooked(attr: &Attribute) -> bool {
COOKED_ATTRIBUTE_NAMES.iter().any(|n| attr.path.is_ident(n))
}

pub fn is_rustfmt_or_clippy_attr(path: &Path) -> bool {
path.segments.first().map_or(false, |s| s.ident == "rustfmt" || s.ident == "clippy")
}

/// Removes cooked attributes from a vector of attributes. Uncooked attributes are left in the
/// vector.
///
Expand All @@ -111,7 +116,7 @@ fn remove_cooked(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
// FIXME: Replace with `Vec::drain_filter` once it is stable.
let mut i = 0;
while i < attrs.len() {
if !is_cooked(&attrs[i]) {
if !is_cooked(&attrs[i]) && !is_rustfmt_or_clippy_attr(&attrs[i].path) {
i += 1;
continue;
}
Expand Down

0 comments on commit 1bc8d70

Please sign in to comment.