π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Reduce Rust approval false positives
Manav Rathic387bc3bf235
diff --git a/.github/checks/change-approval/report.mjs b/.github/checks/change-approval/report.mjsindex 74de32c281..c0a0031d73 100644--- a/.github/checks/change-approval/report.mjs+++ b/.github/checks/change-approval/report.mjs@@ -82,7 +82,7 @@ export function writeReport({);if (rust.length)sections.push(- `## Rust lint declarations and files containing unsafe\n\n${list(rust.map(code))}`,+ `## Rust lint declarations and files containing unsafe\n\n${list(rust.map(({ path, reasons }) => `${code(path)}: ${reasons.map(code).join("; ")}`))}`,);if (web.length)sections.push(`## Web lint directives\n\n${list(web.map(code))}`);@@ -110,7 +110,10 @@ function list(items) {}function code(path) {- return `\`${path}\``;+ const fence = "`".repeat(+ Math.max(0, ...(path.match(/`+/g) ?? []).map((run) => run.length)) + 1,+ );+ return `${fence}${path.startsWith("`") || path.endsWith("`") ? ` ${path} ` : path}${fence}`;}function withSize({ path, size }) {diff --git a/.github/checks/change-approval/rust.mjs b/.github/checks/change-approval/rust.mjsindex 072b38fd17..285fcffa8c 100644--- a/.github/checks/change-approval/rust.mjs+++ b/.github/checks/change-approval/rust.mjs@@ -55,12 +55,14 @@ function rustPolicy(source) {if (closing) throw new Error("Unclosed Rust delimiter");return nodes;};- const lint = (nodes) =>+ const lint = (+ nodes,+ levels = /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/,+ ) =>nodes.some((node, i) =>Array.isArray(node)- ? lint(node)- : /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/.test(node) &&- nodes[i + 1]?.[0] === "(",+ ? lint(node, levels)+ : levels.test(node) && nodes[i + 1]?.[0] === "(",);const declarations = [];const visit = (nodes) => {@@ -89,7 +91,23 @@ function rustPolicy(source) {)stop++;}- declarations.push(JSON.stringify(nodes.slice(i, stop)));+ const attributes = nodes.slice(i, end);+ const target = nodes+ .slice(end, stop)+ .map((node) =>+ Array.isArray(node) ? [node[0], node.at(-1)] : node,+ );+ declarations.push({+ key: JSON.stringify([attributes, target]),+ restrictive: lint(+ attributes,+ /^(?:r#)?(?:warn|deny|forbid)$/,+ ),+ text: attributes+ .flat(Infinity)+ .join("")+ .replace(/\s+/g, " "),+ });}if (end > i) {i = end - 1;@@ -100,29 +118,40 @@ function rustPolicy(source) {}};visit(group());- return {- unsafe: tokens.includes("unsafe"),- declarations: JSON.stringify(declarations.sort()),- };+ return { unsafe: tokens.includes("unsafe"), declarations };}export function checkRust({ files, readVersions }) {- return files- .filter(- ({ path }) =>- path.endsWith(".rs") || basename(path) === "Cargo.toml",- )- .filter(({ path }) => {- const { before, after } = readVersions(path);- if (!path.endsWith(".rs"))- return cargoLints(before) !== cargoLints(after);+ const findings = [];+ for (const { path } of files) {+ if (!path.endsWith(".rs") && basename(path) !== "Cargo.toml") continue;+ const { before, after } = readVersions(path);+ const reasons = [];+ if (!path.endsWith(".rs")) {+ if (cargoLints(before) !== cargoLints(after))+ reasons.push(+ "Cargo lint settings or workspace selection changed",+ );+ } else {const oldPolicy = rustPolicy(before);const newPolicy = rustPolicy(after);- return (- oldPolicy.unsafe ||- newPolicy.unsafe ||- oldPolicy.declarations !== newPolicy.declarations- );- })- .map(({ path }) => path);+ if (oldPolicy.unsafe || newPolicy.unsafe)+ reasons.push(+ "File contains unsafe before or after this change",+ );+ const remaining = oldPolicy.declarations;+ for (const declaration of newPolicy.declarations) {+ const index = remaining.findIndex(+ ({ key }) => key === declaration.key,+ );+ if (index !== -1) remaining.splice(index, 1);+ else reasons.push(`Added or changed: ${declaration.text}`);+ }+ for (const declaration of remaining)+ if (declaration.restrictive)+ reasons.push(`Removed or changed: ${declaration.text}`);+ }+ if (reasons.length) findings.push({ path, reasons });+ }+ return findings;}diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjsindex 5f455ab918..2f41276885 100644--- a/.github/checks/change-approval/test.mjs+++ b/.github/checks/change-approval/test.mjs@@ -824,13 +824,14 @@ test("reordering Cargo workspace selection lists needs no approval", (t) => {}});-test("Rust lint declarations, reasons and conditions need approval", (t) => {+test("Rust lint changes need approval except for removed suppressions", (t) => {const expect = '#[expect(dead_code, reason = "Shared helper")]';const body = "fn helper() {}";- for (const [before, after] of [+ for (const [before, after, approval = true] of [[body, `${expect}\n${body}`],- [`${expect}\n${body}`, body],- [`${expect}\n${body}`, null],+ [`${expect}\n${body}`, body, false],+ [`${expect}\n${body}`, null, false],+ [`#[allow(dead_code)]\n${body}`, body, false],[`${expect}\n${body}`,`${expect.replace("Shared helper", "New reason")}\n${body}`,@@ -844,7 +845,11 @@ test("Rust lint declarations, reasons and conditions need approval", (t) => {`#[cfg_attr(unix, expect(dead_code))]\n${body}`,`#[cfg_attr(test, expect(dead_code))]\n${body}`,],- [`#[cfg_attr(unix, cfg_attr(test, expect(dead_code)))]\n${body}`, body],+ [+ `#[cfg_attr(unix, cfg_attr(test, expect(dead_code)))]\n${body}`,+ body,+ false,+ ],[`#[path = "a.rs"]\n${expect}\nmod support;`,`#[path = "b.rs"]\n${expect}\nmod support;`,@@ -855,6 +860,12 @@ test("Rust lint declarations, reasons and conditions need approval", (t) => {],[body, `#[allow(dead_code, reason = "Shared helper")]\n${body}`],[`#[deny(dead_code)]\nmod guarded {}`, "mod guarded {}"],+ [`#[cfg_attr(unix, warn(dead_code))]\n${body}`, body],+ [`#![forbid(unsafe_code)]\n${body}`, body],+ [+ `${expect} ${body}`,+ `${expect} ${body} mod other { ${expect} ${body} }`,+ ],[body, `#[r#expect(dead_code, reason = "Shared helper")]\n${body}`],[`${expect}\nmod support {}`,@@ -865,10 +876,17 @@ test("Rust lint declarations, reasons and conditions need approval", (t) => {`${body}\n${expect}\nfn other() {}`,],]) {- assert.match(- scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }),- /^1 Rust lint policy file\n/,+ const output = scan(+ t,+ { "src/lib.rs": before },+ { "src/lib.rs": after },);+ if (approval)+ assert.match(+ output,+ /^1 Rust lint policy file\n[\s\S]*(?:Added|Removed) or changed: #!?\[/,+ );+ else assert.equal(output, "");}});@@ -878,6 +896,7 @@ test("ordinary code under existing lint declarations needs no approval", (t) =>'#![expect(dead_code, reason = "Shared helpers")] fn helper() { first(); }',"#![forbid(unsafe_code)] fn helper() { first(); }",'fn helper() { #[expect(unused_variables, reason = "Temporary binding")] let value = first(); }',+ '#[expect(clippy::expect_used, reason = "Valid catalog")] Asset::file(AssetFile { url: first() }).expect("valid")',]) {assert.equal(scan(