rust-analyzer: Ambiguous "unsafe" Error WASM JS alert
I’m in the process of following the Game of Life Rust-WASM tutorial here:
https://rustwasm.github.io/docs/book/game-of-life/hello-world.html
My level of expertise in Rust: Beginner
OS: macOS Catalina 10.15.5 Editor: VSCode 1.47 rust-analyzer: v0.2.240
(rust-analyzer updated just a few minutes ago)
I’m getting this error message when looking at src/lib.rs:
{
"resource": "[...]/wasm-game-of-life/src/lib.rs",
"owner": "_generated_diagnostic_collection_name_#1",
"severity": 8,
"message": "This operation is unsafe and requires an unsafe function or block",
"source": "rust-analyzer",
"startLineNumber": 18,
"startColumn": 5,
"endLineNumber": 18,
"endColumn": 16
}
In this code block the second alert(...) is highlighted:
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(name);
}
However the code compiles regardless.
When changing to:
#[wasm_bindgen]
pub fn greet(name: &str) {
unsafe{alert(name);}
}
Then I’m getting this warning:
{
"resource": "[...]/wasm-game-of-life/src/lib.rs",
"owner": "_generated_diagnostic_collection_name_#1",
"code": "unused_unsafe",
"severity": 4,
"message": "unnecessary `unsafe` block\n`#[warn(unused_unsafe)]` on by default",
"source": "rustc",
"startLineNumber": 18,
"startColumn": 5,
"endLineNumber": 18,
"endColumn": 11
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 51
- Comments: 17 (10 by maintainers)
Commits related to this issue
- Merge #5682 5682: Add an option to disable diagnostics r=matklad a=popzxc As far as I know, currently it's not possible to disable a selected type of diagnostics provided by `rust-analyzer`. This... — committed to rust-lang/rust-analyzer by bors[bot] 4 years ago
- Ignore missing unsafe warnings until "https://github.com/rust-analyzer/rust-analyzer/issues/5412" — committed to jaburns/dotfiles by jaburns 3 years ago
- Merge #9128 9128: feat: expand procedural attribute macros r=jonas-schievink a=jonas-schievink This adds experimental support for attribute macros. They can be enabled by setting `rust-analyzer.expe... — committed to rust-lang/rust-analyzer by bors[bot] 3 years ago
- Merge #9128 9128: feat: expand procedural attribute macros r=jonas-schievink a=jonas-schievink This adds experimental support for attribute macros. They can be enabled by setting `rust-analyzer.expe... — committed to rust-lang/rust-analyzer by bors[bot] 3 years ago
- Remove workaround for RA issue Should be fixed or no longer applies - https://github.com/rust-analyzer/rust-analyzer/issues/5412 — committed to martin-t/rec-wars by martin-t 3 years ago
For those looking to disable the
missing-unsaferule until it’s fixed and are using VS Code, adding the following to yoursettings.jsonand reloading your editor will suppress these errors:credit to @popzxc for landing https://github.com/rust-analyzer/rust-analyzer/pull/5682, which makes the above possible
Just in case anyone doesn’t think to check the merged PR and runs into this, I can confirm that I was able to set this in VS Code and got the warning to go away:
You can get rid of this error / warning locally by putting an unsafe block and then surpressing the warning, but it is not a great way to handle that:
@OliverEvans96
cxxalso uses attribute proc macros, which are not supported.Recent
rust-analyzerversions should have both of those options enabled.You can enable proc-macro support using
I’d suggest listening to the rustc warning, not rust-analyzer. If the wrong diagnostics from rust-analyzer annoys you, you can also turn it off in the settings (but this currently turns all of them off).
That’s just because we don’t expand procedural macros yet. Presumably it does some magic to make these operations safe.
I just ran into this issue with cxx, which uses macros to generate safe C++ FFIs.
Would you rather recommend to get the “unsafe” error or the “unused unsafe” warning? Or in other words is my temporary ‘fix’ the right thing to do or should I just ignore it?