Skip to content

Visual Studio Code, rebuilt in Rust

Rust

build: deny undocumented unsafe blocks, forbid unsafe in pure crates

rokartur6391283c35e7
diff --git a/native/Cargo.toml b/native/Cargo.toml
index 64031f261ef..5804ddb1542 100644
--- a/native/Cargo.toml
+++ b/native/Cargo.toml
@@ -8,6 +8,14 @@ exclude = ["vendor"]
edition = "2024"
license = "MIT"
+# The memory safety gates of docs/memory-safety.md. rustc checks the first on every build; the second is a clippy lint:
+# cargo clippy --workspace --all-targets --locked -- -A clippy::all
+[workspace.lints.rust]
+unsafe_op_in_unsafe_fn = "deny"
+
+[workspace.lints.clippy]
+undocumented_unsafe_blocks = "deny"
+
[workspace.dependencies]
anyhow = "1"
serde = { version = "1", features = ["derive"] }
diff --git a/native/crates/app/Cargo.toml b/native/crates/app/Cargo.toml
index 8214eefce41..997ac72f500 100644
--- a/native/crates/app/Cargo.toml
+++ b/native/crates/app/Cargo.toml
@@ -8,6 +8,9 @@ license.workspace = true
name = "vsrustium"
path = "src/main.rs"
+[lints]
+workspace = true
+
[dependencies]
alacritty_terminal = "0.25"
libc = "0.2"
diff --git a/native/crates/config/Cargo.toml b/native/crates/config/Cargo.toml
index b98d5eb8a38..afa4316893f 100644
--- a/native/crates/config/Cargo.toml
+++ b/native/crates/config/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
serde.workspace = true
serde_json.workspace = true
diff --git a/native/crates/config/src/lib.rs b/native/crates/config/src/lib.rs
index 23973cbe761..a26cd321735 100644
--- a/native/crates/config/src/lib.rs
+++ b/native/crates/config/src/lib.rs
@@ -2,6 +2,8 @@
//! defaults, the user and workspace `settings.json` files parsed with the JSONC rules of base/common/json.ts,
//! and the merged view extensions and the workbench read.
+#![forbid(unsafe_code)]
+
pub mod configuration;
pub mod debuggers;
pub mod indentation;
diff --git a/native/crates/diff/Cargo.toml b/native/crates/diff/Cargo.toml
index bd51ce9e9c6..3926777683d 100644
--- a/native/crates/diff/Cargo.toml
+++ b/native/crates/diff/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
[dev-dependencies]
diff --git a/native/crates/diff/src/lib.rs b/native/crates/diff/src/lib.rs
index 8889fb82539..fc5790a78bb 100644
--- a/native/crates/diff/src/lib.rs
+++ b/native/crates/diff/src/lib.rs
@@ -1,6 +1,8 @@
//! The line diff of `editor/common/diff` (VS Code 1.147.0): `DefaultLinesDiffComputer` ("advanced") with its
//! sequence algorithms, heuristics and moved-line detection. Columns count chars where upstream counts UTF-16 units.
+#![forbid(unsafe_code)]
+
mod algorithms;
mod computer;
mod heuristics;
diff --git a/native/crates/exthost/Cargo.toml b/native/crates/exthost/Cargo.toml
index 4e332da29e9..58628841f84 100644
--- a/native/crates/exthost/Cargo.toml
+++ b/native/crates/exthost/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
anyhow.workspace = true
base64 = "0.22"
diff --git a/native/crates/files/Cargo.toml b/native/crates/files/Cargo.toml
index fcc264e6494..5591354fec3 100644
--- a/native/crates/files/Cargo.toml
+++ b/native/crates/files/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
config = { path = "../config" }
# iconv-lite's role: the encodings of files.encoding.
diff --git a/native/crates/files/src/lib.rs b/native/crates/files/src/lib.rs
index fc7bdbd7af7..fbb20f728b6 100644
--- a/native/crates/files/src/lib.rs
+++ b/native/crates/files/src/lib.rs
@@ -2,6 +2,8 @@
//! whatever the caller adds through `FileSystem` (extension providers). Errors keep the JavaScript error names of each
//! layer; `handle_error` renames them as `MainThreadFileSystem._handleError` does before they reach the host.
+#![forbid(unsafe_code)]
+
pub mod disk;
pub mod encoding;
pub mod glob;
diff --git a/native/crates/pixeldiff/Cargo.toml b/native/crates/pixeldiff/Cargo.toml
index 947a86c30d4..8988b6070c7 100644
--- a/native/crates/pixeldiff/Cargo.toml
+++ b/native/crates/pixeldiff/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
anyhow.workspace = true
image = { version = "0.25", default-features = false, features = ["png"] }
diff --git a/native/crates/pixeldiff/src/lib.rs b/native/crates/pixeldiff/src/lib.rs
index 0381630be1c..424c6cde5f6 100644
--- a/native/crates/pixeldiff/src/lib.rs
+++ b/native/crates/pixeldiff/src/lib.rs
@@ -1,5 +1,7 @@
//! Screenshot comparison: a port of pixelmatch (ISC, mapbox/pixelmatch v5) that skips anti-aliased pixels.
+#![forbid(unsafe_code)]
+
use image::RgbaImage;
pub struct DiffResult {
diff --git a/native/crates/pixeldiff/src/main.rs b/native/crates/pixeldiff/src/main.rs
index 3aa8fea6460..84fb0085d69 100644
--- a/native/crates/pixeldiff/src/main.rs
+++ b/native/crates/pixeldiff/src/main.rs
@@ -1,5 +1,7 @@
//! pixeldiff <expected.png> <actual.png> [diff.png]: prints the counts, exits 1 when non-anti-aliased pixels differ.
+#![forbid(unsafe_code)]
+
fn main() -> anyhow::Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
anyhow::ensure!(args.len() == 2 || args.len() == 3, "usage: pixeldiff <expected.png> <actual.png> [diff.png]");
diff --git a/native/crates/search/Cargo.toml b/native/crates/search/Cargo.toml
index 2ef3a71cb85..3b8647838cd 100644
--- a/native/crates/search/Cargo.toml
+++ b/native/crates/search/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
files = { path = "../files" }
regex = "1"
diff --git a/native/crates/search/src/lib.rs b/native/crates/search/src/lib.rs
index 80c82b31006..864b6ba166f 100644
--- a/native/crates/search/src/lib.rs
+++ b/native/crates/search/src/lib.rs
@@ -1,3 +1,5 @@
+#![forbid(unsafe_code)]
+
pub mod file_search;
pub mod query;
pub mod ripgrep;
diff --git a/native/crates/storage/Cargo.toml b/native/crates/storage/Cargo.toml
index c78fb6e48f6..2e46b92f9b2 100644
--- a/native/crates/storage/Cargo.toml
+++ b/native/crates/storage/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
getrandom = "0.3"
# SQLite as macOS ships it, like upstream's @vscode/sqlite3 behind base/parts/storage.
diff --git a/native/crates/storage/src/lib.rs b/native/crates/storage/src/lib.rs
index 9ba7f83fd3d..cf8c507e0d5 100644
--- a/native/crates/storage/src/lib.rs
+++ b/native/crates/storage/src/lib.rs
@@ -2,6 +2,8 @@
//! `state.vscdb` files of the global and workspace storage scopes. Writes go to disk on every change, where
//! upstream batches them for 100 ms; the cache is the source of truth and rebuilds a database that broke at runtime.
+#![forbid(unsafe_code)]
+
use std::collections::BTreeMap;
use std::error::Error;
use std::fs;
diff --git a/native/crates/textmate/Cargo.toml b/native/crates/textmate/Cargo.toml
index 30474eb6b8c..92140ba28a4 100644
--- a/native/crates/textmate/Cargo.toml
+++ b/native/crates/textmate/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
edition.workspace = true
license.workspace = true
+[lints]
+workspace = true
+
[dependencies]
# Oniguruma 6.9, the engine vscode-oniguruma compiles to WASM; built from the bundled C sources, no bindgen.
onig = { version = "6.5", default-features = false }
diff --git a/native/crates/textmate/src/lib.rs b/native/crates/textmate/src/lib.rs
index e8044809bfe..e49cb824f8b 100644
--- a/native/crates/textmate/src/lib.rs
+++ b/native/crates/textmate/src/lib.rs
@@ -1,6 +1,8 @@
//! TextMate tokenization, ported from vscode-textmate 9.3.2 on Oniguruma, the regex engine
//! vscode-oniguruma runs.
+#![forbid(unsafe_code)]
+
mod grammar;
mod matcher;
pub mod plist;