Skip to content

Ambiguous "unsafe" Error WASM JS alert #5412

@dgb23

Description

@dgb23

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    S-unactionableIssue requires feedback, design decisions or is blocked on other work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions