tsify icon indicating copy to clipboard operation
tsify copied to clipboard

Rust-analyzer marks structs with snake-case warning

Open FredrikNoren opened this issue 1 year ago • 7 comments

I'm getting the following warning on all structs that have Tsify:

Function `__wbg_instanceof_JsType_24d65669860e1289` should have snake_case name, e.g. `__wbg_instanceof_js_type_24d65669860e1289`

FredrikNoren avatar Dec 18 '23 13:12 FredrikNoren

yup I get this too, would be good if we get a solution for this

rouzwelt avatar Dec 25 '23 14:12 rouzwelt

有一个办法,在文件第一行加上下面的代码,就可以不显示警告了。

#![allow(non_snake_case)]

zomem avatar Jan 03 '24 05:01 zomem

有一个办法,在文件第一行加上下面的代码,就可以不显示警告了。

#![allow(non_snake_case)]

this not a good approach and is overall discouraged by the community to change compiler settings

the solution is that the maintainers fix the underlying cause

rouzwelt avatar Jan 05 '24 01:01 rouzwelt

Last code update has been 8 months ago, there are PR stuck since July, is this crate dead?

nappa85 avatar Jan 08 '24 09:01 nappa85

This seems like a rust-analyzer issue. The ___wbg_instanceof is from wasm-bindgen for JsCast and the surrounding block has #[automatically_derived] which should suppress this kind of warnings

Pistonight avatar Feb 14 '24 04:02 Pistonight

This might be a workaround:

#[allow(non_snake_case)]
mod tsify_derive {
    use super::*;
    #[derive(Tsify, Serialize, Deserialize)]
    #[tsify(into_wasm_abi, from_wasm_abi)]
    pub struct MyStruct {
        ...
    }
}
pub use tsify_derive::MyStruct;

Or make a proc macro that does this for you (I just typed it ad-hoc so there might be a few errors, but the idea is there)

use quote::quote;
use proc_macro2::{Ident, Span};
use syn::parse_macro_input;

#[proc_macro_attr]
pub fn suppress_derive_case_warnings(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let parsed = parse_macro_input!(input as DeriveInput);
    let name = &parsed.ident;
    let vis = &parsed.vis;

    let mod_name = Ident::new(&format!("__tsify_mod_{}", name), Span::call_site());

    let expanded = quote! {
        #[allow(non_snake_case)]
        #[automatically_derived]
        mod #mod_name {
            use super::*;
			
            #parsed
        }
        #vis use #mod_name::#name;
    };
	
    expanded.into()
}

Then use it like this

#[suppress_derive_case_warnings]
#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct MyStruct {
    ...
}

Pistonight avatar Feb 14 '24 05:02 Pistonight

I am only getting this error with rust-analyzer using VS Code, the error itself is not coming from my Rust installation.

I was able to suppress the error with the VS Code setting:

"rust-analyzer.diagnostics.disabled": ["non_snake_case"]

Also needed to restart the rust-analyzer language server.

Sharpiro avatar May 01 '24 16:05 Sharpiro