Skip to content

Commit

Permalink
Work around even more spurious unneeded parenthesis warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
CensoredUsername committed Sep 27, 2024
1 parent d7a8299 commit 6796c0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
39 changes: 24 additions & 15 deletions plugin/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub fn delimited<T: ToTokens>(expr: T) -> TokenTree {
TokenTree::Group(group)
}

/// Checks if the given tokenstream is a parenthesized expression to work around rustc giving
/// Checks if the given `Group` is a parenthesized expression to work around rustc giving
/// Unnecessary parenthesis warnings in macro-generated code, if this tokentree were to be used
/// as the argument to a single argument function
///
Expand All @@ -282,24 +282,33 @@ pub fn delimited<T: ToTokens>(expr: T) -> TokenTree {
/// To check if this is valid, we should a: test that this tokentree node is a parenthesis delimited
/// node and b: there are no commas in its internal tokentree, because then it'd be a tuple, and
/// this transform would be invalid
pub fn is_parenthesized(expr: &TokenTree) -> bool {
match expr {
TokenTree::Group(group) => {
if group.delimiter() != Delimiter::Parenthesis {
pub fn is_parenthesized(group: &Group) -> bool {
if group.delimiter() != Delimiter::Parenthesis {
return false
}

for item in group.stream() {
if let TokenTree::Punct(punct) = item {
if punct.as_char() == ',' {
return false
}
}
}

for item in group.stream() {
if let TokenTree::Punct(punct) = item {
if punct.as_char() == ',' {
return false
}
}
}
true
}

true
},
_ => false
/// Returns the given `TokenTree`, but if it's a parenthesized group, it will change this
/// to a None-delimited group, if `is_parenthesized` deems this to be a valid transform
///
/// this is intended to work around unneeded parenthesis around function arguments warnings
pub fn strip_parenthesis(expr: &mut TokenTree) {
if let TokenTree::Group(group) = &*expr {
if is_parenthesized(group) {
let mut stripped = TokenTree::Group(Group::new(Delimiter::None, group.stream()));
stripped.set_span(group.span());
*expr = stripped;
}
}
}

Expand Down
22 changes: 8 additions & 14 deletions plugin/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens};

use byteorder::{ByteOrder, LittleEndian};

use crate::common::{Size, Stmt, delimited, is_parenthesized, Relocation};
use crate::common::{Size, Stmt, delimited, strip_parenthesis, Relocation};

use std::convert::TryInto;

Expand Down Expand Up @@ -103,19 +103,13 @@ pub fn serialize(name: &TokenTree, stmts: Vec<Stmt>) -> TokenStream {
// and construct the appropriate method call
let method = syn::Ident::new(method, Span::mixed_site());

if args.len() == 1 && is_parenthesized(&args[0]) {
// special case for not emitting redundant parenthesis to work around an annoying error
let mut args = args;
let arg = args.pop().unwrap();
output.extend(quote! {
#name . #method #arg ;
})

} else {
output.extend(quote! {
#name . #method ( #( #args ),* ) ;
})
}
// work around rustc giving code style warnings about unneeded parenthesis in method calls
let mut args = args;
args.iter_mut().for_each(strip_parenthesis);

output.extend(quote! {
#name . #method ( #( #args ),* ) ;
})
}

// if we have nothing to emit, expand to nothing. Else, wrap it into a block.
Expand Down

0 comments on commit 6796c0b

Please sign in to comment.