Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types
MIT License
1.15k stars 115 forks source link

Useless use of `vec!` #158

Closed googleben closed 1 year ago

googleben commented 1 year ago

Clippy complains about this line in the codebase, saying it's a useless use of vec! (which it is). This can be pretty easily fixed like so:

fn to_tokens(&self, tokens: &mut TokenStream) {
    let dependencies = &self.0;
    let num_deps = dependencies.len();
    tokens.extend(quote! {
        {
            let mut dependencies = Vec::with_capacity(#num_deps);
            #( #dependencies )*
            dependencies
        }
    })
}

Just add a line and change vec! to a Vec::with_capacity call, fixing the warning and slightly improving performance. There may be a slightly better/cleaner way, but I'm not that well-versed in proc macros, so this at least works and is faster.

I found this issue through Lemmy, which uses ts_ts v6.2.1 - there are literally hundreds of these warnings in the codebase because of how many structs derive TS - and I would really appreciate if this fix was applied to both the main branch and a temporary one so we could get a v6.2.2 or something.

Please let me know if there's anything I can do to help make this happen.

NyxCode commented 1 year ago

fixed on master, and in 7.0 to be released soon