π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Reject broad Rust lint exceptions
Manav Rathie5fddd149a87
diff --git a/rust/checks/lint-exceptions/check.mjs b/rust/checks/lint-exceptions/check.mjsnew file mode 100644index 0000000000..5ee1c9bba7--- /dev/null+++ b/rust/checks/lint-exceptions/check.mjs@@ -0,0 +1,134 @@+import { execFileSync } from "node:child_process";+import { existsSync, readFileSync } from "node:fs";+import { relative, resolve, sep } from "node:path";++const root = resolve(process.argv[2]);++for (const path of files()) {+ const source = readFileSync(resolve(root, path), "utf8");+ for (const attribute of attributes(source)) {+ if (!/\bexpect\s*\(/.test(attribute.text)) continue;+ const lints = expectedLints(attribute.text);+ if (attribute.inner) {+ if (+ path !== "rust/crates/ml/examples/vecdb_bench.rs" ||+ lints?.length !== 1 ||+ lints[0] !== "clippy::expect_used"+ ) {+ reject(path, source, attribute.start);+ }+ continue;+ }+ const module = followingModule(source, attribute.end);+ if (!module) continue;+ if (module === "frb_generated") continue;+ if (lints?.length === 1 && lints[0] === "dead_code") continue;+ reject(path, source, attribute.start);+ }+}++function expectedLints(attribute) {+ const match = attribute.match(/^#!?\[\s*expect\s*\(([\s\S]*)\)\s*\]$/);+ if (!match) return undefined;+ const reason = match[1].match(/\breason\s*=\s*"(?:\\.|[^"\\])+"\s*,?\s*$/);+ if (!reason) return undefined;+ const lints = match[1]+ .slice(0, reason.index)+ .split(",")+ .map((lint) => lint.trim())+ .filter(Boolean);+ return lints.every((lint) => /^[A-Za-z_][A-Za-z0-9_:]*$/.test(lint))+ ? lints+ : undefined;+}++function files() {+ return execFileSync(+ "git",+ [+ "ls-files",+ "--cached",+ "--others",+ "--exclude-standard",+ "-z",+ "--",+ "rust",+ ],+ { cwd: root, encoding: "utf8" },+ )+ .split("\0")+ .filter(+ (path) => path.endsWith(".rs") && existsSync(resolve(root, path)),+ )+ .map((path) =>+ relative(root, resolve(root, path)).split(sep).join("/"),+ );+}++function* attributes(source) {+ const pattern = /^[ \t]*#(!?)\[/gm;+ for (const match of source.matchAll(pattern)) {+ const start = match.index + match[0].indexOf("#");+ const end = attributeEnd(source, start);+ if (end === undefined) continue;+ yield {+ end,+ inner: match[1] === "!",+ start,+ text: source.slice(start, end),+ };+ }+}++function attributeEnd(source, start) {+ let quoted = false;+ let escaped = false;+ for (let i = source.indexOf("[", start) + 1; i < source.length; i++) {+ const character = source[i];+ if (quoted) {+ if (escaped) escaped = false;+ else if (character === "\\") escaped = true;+ else if (character === '"') quoted = false;+ } else if (character === '"') {+ quoted = true;+ } else if (character === "]") {+ return i + 1;+ }+ }+}++function followingModule(source, offset) {+ let position = skipTrivia(source, offset);+ while (source.startsWith("#[", position)) {+ const end = attributeEnd(source, position);+ if (end === undefined) return undefined;+ position = skipTrivia(source, end);+ }+ return source+ .slice(position)+ .match(+ /^(?:pub(?:\s*\([^)]*\))?\s+)?mod\s+([A-Za-z_][A-Za-z0-9_]*)\b/,+ )?.[1];+}++function skipTrivia(source, offset) {+ let position = offset;+ while (true) {+ const whitespace = source.slice(position).match(/^\s+/)?.[0];+ if (whitespace) {+ position += whitespace.length;+ continue;+ }+ const comment = source+ .slice(position)+ .match(/^\/\/[^\n]*(?:\n|$)/)?.[0];+ if (!comment) return position;+ position += comment.length;+ }+}++function reject(path, source, start) {+ const line = source.slice(0, start).split("\n").length;+ console.error(`${path}:${line}: unapproved crate or module lint exception`);+ process.exitCode = 1;+}diff --git a/rust/checks/lint-exceptions/test.mjs b/rust/checks/lint-exceptions/test.mjsnew file mode 100644index 0000000000..9301ed2130--- /dev/null+++ b/rust/checks/lint-exceptions/test.mjs@@ -0,0 +1,82 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";++const check = join(import.meta.dirname, "check.mjs");++for (const source of [+ '#![expect(clippy::unwrap_used, reason = "fixture")]\nfn helper() {}\n',+ '#[expect(dead_code, clippy::unwrap_used, reason = "fixture")]\nmod tests {}\n',+ '#[expect(dead_code, reason = "")]\nmod tests {}\n',+]) {+ const result = run("rust/crates/fixture/src/lib.rs", source);+ assert.equal(result.status, 1, result.stderr);+ assert.match(result.stderr, /unapproved crate or module lint exception/);+}++let result = run(+ "rust/crates/fixture/src/lib.rs",+ '#[expect(dead_code, reason = "fixture")]\nfn helper() {}\n',+);+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++result = run(+ "rust/crates/fixture/src/lib.rs",+ "#![allow(dead_code)]\nfn helper() {}\n",+);+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++result = run(+ "rust/crates/fixture/src/lib.rs",+ '#[expect(dead_code, reason = "fixture")]\n#[cfg(test)]\nmod support {}\n',+);+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++result = run(+ "rust/crates/ml/examples/vecdb_bench.rs",+ '#![expect(clippy::expect_used, reason = "fixture")]\nfn main() {}\n',+);+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++result = run(+ "rust/crates/ml/examples/vecdb_bench.rs",+ '#![expect(clippy::expect_used, clippy::unwrap_used, reason = "fixture")]\nfn main() {}\n',+);+assert.equal(result.status, 1, result.stderr);++result = run(+ "rust/crates/fixture/src/lib.rs",+ '#[expect(clippy::unwrap_used, reason = "fixture")]\nmod frb_generated;\n',+);+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++function run(path, source) {+ const root = mkdtempSync(join(tmpdir(), "ente-rust-lint-exceptions-"));+ try {+ spawn("git", ["init", "-q"], root);+ write(root, path, source);+ spawn("git", ["add", "."], root);+ return spawnSync(process.execPath, [check, root], { encoding: "utf8" });+ } finally {+ rmSync(root, { recursive: true });+ }+}++function write(root, path, content) {+ const target = join(root, path);+ mkdirSync(dirname(target), { recursive: true });+ writeFileSync(target, content);+}++function spawn(command, args, cwd) {+ const result = spawnSync(command, args, { cwd, encoding: "utf8" });+ assert.equal(result.error, undefined);+ assert.equal(result.status, 0, result.stderr);+}diff --git a/rust/scripts/lint.sh b/rust/scripts/lint.shindex b72185626e..456a3c864c 100755--- a/rust/scripts/lint.sh+++ b/rust/scripts/lint.sh@@ -10,6 +10,8 @@ python3 checks/cargo-workspace-dependencies/check.py ..python3 checks/cargo-workspace-dependencies/test.pynode checks/dependency-boundaries/check.mjs ..node checks/dependency-boundaries/test.mjs+node checks/lint-exceptions/check.mjs ..+node checks/lint-exceptions/test.mjscargo fmt --checkcargo clippy --locked --all-targets --features museum,ente-ml/ml-assetscargo audit