microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.23k stars 475 forks source link

New nightly clippy warning breaks build with false positive #3020

Closed kennykerr closed 3 months ago

kennykerr commented 3 months ago

https://github.com/microsoft/windows-rs/actions/runs/8933382409/job/24538679794

Any suggestions?

This is a false positive because the kind of HSTRING the macro create is always immutable, but I guess Clippy doesn't know that. The trouble is that I can't seem to find a way to suppress the Clippy warning from within the macro_rules macro.

riverar commented 3 months ago

How about something like this for now?

#[macro_export]
macro_rules! h {
    ($s:literal) => {{
        const INPUT: &[u8] = $s.as_bytes();
        const OUTPUT_LEN: usize = $crate::utf16_len(INPUT) + 1;
+       #[allow(clippy::declare_interior_mutable_const)]
        const RESULT: $crate::HSTRING = {
            if OUTPUT_LEN == 1 {
                unsafe { ::std::mem::transmute(::std::ptr::null::<u16>()) }
            } else {
                const OUTPUT: $crate::PCWSTR = $crate::w!($s);
                const HEADER: $crate::HSTRING_HEADER = $crate::HSTRING_HEADER { flags: 0x11, len: (OUTPUT_LEN - 1) as u32, padding1: 0, padding2: 0, ptr: OUTPUT.as_ptr() };
                // SAFETY: an `HSTRING` is exactly equivalent to a pointer to an `HSTRING_HEADER`
                unsafe { ::std::mem::transmute::<&$crate::HSTRING_HEADER, $crate::HSTRING>(&HEADER) }
            }
        };
+       #[allow(clippy::borrow_interior_mutable_const)]
        &RESULT
    }};
}
kennykerr commented 3 months ago

That doesn't seem to work. It seems Clippy doesn't suppress within a macro_rules macro.

kennykerr commented 3 months ago

I take that back - that fix seems to work - thanks!