rust-analyzer: False positive ``unnecessary 'unsafe' block`` warning?

I’m not sure if this is a part of GenericDetour::call being generated by a macro, I thought this was fixed with expanding macros though. Removing the unsafe block just causes a missing-unsafe error

use detour::GenericDetour;

type Add5 = extern "Rust" fn(i32) -> i32;
extern "Rust" fn add_5(n: i32) -> i32 { n + 5 }
extern "Rust" fn hookfn(n: i32) -> i32 { n + 50 }

fn main() -> Result<(), detour::Error> {
	let hook = unsafe { GenericDetour::<Add5>::new(add_5, hookfn)? };
	
	// This unsafe is needed (afaik) yet causes a ``unnecessary `unsafe` block`` diagnostic?
	let _res = unsafe { hook.call(50) }; 

	Ok(())
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@rossmacarthur try enabling rust-analyzer.experimental.procAttrMacros.

It appears that using the nightly channel via "rust-analyzer.updates.channel": "nightly" solves the problem.

I’m still getting this error, even with the nightly rust-analyzer (with stable rust and 2021 profile) nevermind this might be an error with my vim config. VScode seems to work fine.

It appears that using the nightly channel via "rust-analyzer.updates.channel": "nightly" solves the problem.

Encountered this when using wasm-bindgen

#[wasm_bindgen(module = "/index.js")]
extern "C" {
    #[wasm_bindgen]
    pub fn my_function() -> JsValue
}

my_function is safe here but ra thinks it is unsafe.