diff --git a/.github/workflows/ast-grep.yml b/.github/workflows/ast-grep.yml index 71e10920cb..bacee1f053 100644 --- a/.github/workflows/ast-grep.yml +++ b/.github/workflows/ast-grep.yml @@ -1,13 +1,13 @@ -# Structural rules for the command-line code (RFC-0012): lint/ast-grep's rule tests, then a scan. +# Structural rules for the command-line code (RFC-0018): the shared rule sets of craft-common-actions, then a scan. # Seconds, no Rust build. name: ast-grep on: push: branches: [main] - paths: ["lint/ast-grep/**", "apps/photocraft-cli/**", "crates/automation/src/args.rs", "apps/photocraft/src/launch_args.rs", "apps/photocraft/src/main.rs", ".github/workflows/ast-grep.yml"] + paths: ["apps/photocraft-cli/**", "crates/automation/src/args.rs", "apps/photocraft/src/launch_args.rs", "apps/photocraft/src/main.rs", ".github/workflows/ast-grep.yml"] pull_request: - paths: ["lint/ast-grep/**", "apps/photocraft-cli/**", "crates/automation/src/args.rs", "apps/photocraft/src/launch_args.rs", "apps/photocraft/src/main.rs", ".github/workflows/ast-grep.yml"] + paths: ["apps/photocraft-cli/**", "crates/automation/src/args.rs", "apps/photocraft/src/launch_args.rs", "apps/photocraft/src/main.rs", ".github/workflows/ast-grep.yml"] workflow_dispatch: permissions: @@ -22,5 +22,7 @@ jobs: # Tracked at @main like the other shared workflows. uses: storytold/craft-common-actions/.github/workflows/ast-grep.yml@main with: - config: lint/ast-grep/sgconfig.yml + # No rules of its own: the shared RFC-0018 sets (rfc-0018-string-error is on, as it was here). + config: "" + shared-rules: rfc-0018 rfc-0018-string-error paths: apps/photocraft-cli crates/automation/src/args.rs apps/photocraft/src/launch_args.rs apps/photocraft/src/main.rs diff --git a/AGENTS.md b/AGENTS.md index a92f36f12f..3d748f5f81 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -93,5 +93,6 @@ Conventions and paths: - Contributor credits: `docs/contributors.md`. - Dev log: `log/devlog.md` (local, gitignored). `plan/` is an optional local notebook. - Screenshots of a PR's change go in the PR description. `docs/images/` holds only images a doc shows. +- Before pushing a change to `apps/photocraft-cli` or its launch flags, run the command-line scan CI runs (RFC-0018 section 3): with a checkout of storytold/craft-common-actions next to this repository, `ast-grep scan --config ../craft-common-actions/actions/ast-grep/shared/rfc-0018/sgconfig.yml ` and the same with `rfc-0018-string-error`, where `` are the `paths` in `.github/workflows/ast-grep.yml`. Docs: `docs/contributing.md` (repository guide, document list, picking work, adding a command), `docs/development.md`, `docs/architecture.md`. diff --git a/lint/ast-grep/rule-tests/args-os-test.yml b/lint/ast-grep/rule-tests/args-os-test.yml deleted file mode 100644 index e3dea767f1..0000000000 --- a/lint/ast-grep/rule-tests/args-os-test.yml +++ /dev/null @@ -1,26 +0,0 @@ -id: args-os -valid: - - let code = photocraft_cli::run(std::env::args_os().skip(1), &mut out, &mut err); - - "let args: Vec = env::args_os().collect();" - - | - use std::env::args_os; - fn main() { let a: Vec<_> = args_os().collect(); } - - | - fn args() -> Vec { Vec::new() } - fn main() { let a = args(); } - - | - use std::env::args as os_args; - fn args() -> Vec { Vec::new() } - fn main() { let a = args(); } -invalid: - - "let args: Vec = std::env::args().collect();" - - for a in env::args().skip(1) {} - - | - use std::env::args; - fn main() { let a: Vec = args().collect(); } - - | - use std::env::{args, var}; - fn main() { for a in args() {} } - - | - use std::env::args as os_args; - fn main() { let a: Vec = os_args().collect(); } diff --git a/lint/ast-grep/rule-tests/clap-error-message-test.yml b/lint/ast-grep/rule-tests/clap-error-message-test.yml deleted file mode 100644 index b2b1b0a8d4..0000000000 --- a/lint/ast-grep/rule-tests/clap-error-message-test.yml +++ /dev/null @@ -1,15 +0,0 @@ -id: clap-error-message -valid: - - cmd.error(ErrorKind::ArgumentConflict, UsageError::BridgeRoots) - - cmd.error(ErrorKind::ArgumentConflict, Conflict { a, b }) - - sub.error(kind, self) - - log.error("not a clap error") - - cmd.error(ErrorKind::ValueValidation, e.to_owned()) -invalid: - - cmd.error(ErrorKind::ArgumentConflict, "configure automation roots on the desktop app") - - sub.error(ErrorKind::MissingRequiredArgument, format!("{flag} requires {transport}")) - - sub.error(ErrorKind::ArgumentConflict, BRIDGE_ROOTS) - - cmd.error(ErrorKind::InvalidValue, e.to_string()) - - cmd.error(ErrorKind::InvalidValue, String::from("bad value")) - - cmd.error(ErrorKind::InvalidValue, "bad value".to_owned()) - - clap::Error::raw(ErrorKind::InvalidValue, "bad value") diff --git a/lint/ast-grep/rule-tests/clap-error-message-variable-test.yml b/lint/ast-grep/rule-tests/clap-error-message-variable-test.yml deleted file mode 100644 index b7f3207e93..0000000000 --- a/lint/ast-grep/rule-tests/clap-error-message-variable-test.yml +++ /dev/null @@ -1,56 +0,0 @@ -id: clap-error-message-variable -valid: - - | - fn conflict(cmd: &mut Command, a: String, b: String) -> clap::Error { - cmd.error(ErrorKind::ArgumentConflict, Conflict { a, b }) - } - - | - fn wrap(cmd: &mut Command, e: UsageError) -> clap::Error { - cmd.error(ErrorKind::ValueValidation, e) - } - - | - fn unrelated(cmd: &mut Command, e: UsageError) -> clap::Error { - let shown = format!("{e}"); - eprintln!("{shown}"); - cmd.error(ErrorKind::ValueValidation, e) - } - - | - fn cloned(cmd: &mut Command, e: &UsageError) -> clap::Error { - let owned = e.to_owned(); - cmd.error(ErrorKind::ValueValidation, owned) - } - - | - fn shadowed(cmd: &mut Command, e: UsageError) -> clap::Error { - let message = e; - { - let message = format!("debug"); - eprintln!("{message}"); - } - cmd.error(ErrorKind::ValueValidation, message) - } -invalid: - - | - fn conflict(cmd: &mut Command, a: &str, b: &str) -> clap::Error { - let message = format!("the argument '{a}' cannot be used with '{b}'"); - cmd.error(ErrorKind::ArgumentConflict, message) - } - - | - fn bad(cmd: &mut Command, e: UsageError) -> clap::Error { - let message = e.to_string(); - cmd.error(ErrorKind::ValueValidation, message) - } - - | - fn typed(cmd: &mut Command, flag: &str) -> clap::Error { - let message: String = flag.into(); - clap::Error::raw(ErrorKind::InvalidValue, message) - } - - | - fn literal(cmd: &mut Command) -> clap::Error { - let message = "bad value".to_owned(); - cmd.error(ErrorKind::InvalidValue, message) - } - - | - fn from(cmd: &mut Command) -> clap::Error { - let message = String::from("bad value"); - cmd.error(ErrorKind::InvalidValue, message) - } diff --git a/lint/ast-grep/rule-tests/string-error-test.yml b/lint/ast-grep/rule-tests/string-error-test.yml deleted file mode 100644 index dfb1d89f38..0000000000 --- a/lint/ast-grep/rule-tests/string-error-test.yml +++ /dev/null @@ -1,15 +0,0 @@ -id: string-error -valid: - - "fn parse(s: &str) -> Result { todo!() }" - - "fn name() -> Option { None }" - - "fn ok() -> Result { Ok(String::new()) }" - - Err(PortError::Range) - - Err(e) -invalid: - - "fn parse(s: &str) -> Result { todo!() }" - - "type R = Result<(), String>;" - - "fn parse(s: &str) -> std::result::Result { todo!() }" - - Err(format!("bad port {s}")) - - Err(e.to_string()) - - Err(String::from("bad port")) - - Err("bad port") diff --git a/lint/ast-grep/rules/args-os.yml b/lint/ast-grep/rules/args-os.yml deleted file mode 100644 index b6b881ee18..0000000000 --- a/lint/ast-grep/rules/args-os.yml +++ /dev/null @@ -1,31 +0,0 @@ -id: args-os -language: rust -severity: error -message: std::env::args() panics on an argument that isn't valid Unicode -note: | - Read std::env::args_os() and let clap parse the OsStrings, so a Latin-1 file name reaches the - command (or is a usage error) instead of aborting before anything is printed (#1108). -rule: - any: - - pattern: std::env::args() - - pattern: env::args() - # A bare args() only where the file imports std::env::args under that name, not any function - # named args (`args as os_args` binds os_args, so a local args() stays valid). - - all: - - pattern: args() - - inside: - kind: source_file - stopBy: end - has: - kind: use_declaration - stopBy: end - regex: std::env::(args\s*;|\{[^}]*\bargs\s*[,}]) - # The renamed import: the call binds $F, then the file must import std::env::args as $F. - - all: - - pattern: $F() - - inside: - kind: source_file - stopBy: end - has: - stopBy: end - pattern: use std::env::args as $F; diff --git a/lint/ast-grep/rules/clap-error-message-variable.yml b/lint/ast-grep/rules/clap-error-message-variable.yml deleted file mode 100644 index 4cc95bc90d..0000000000 --- a/lint/ast-grep/rules/clap-error-message-variable.yml +++ /dev/null @@ -1,35 +0,0 @@ -id: clap-error-message-variable -language: rust -severity: error -message: clap error built from a string held in a variable -note: | - The message was built with format!, .to_string(), String::from or a literal's .to_owned() a - few lines up. Give it a variant on a - #[derive(thiserror::Error)] enum instead, with the parts as fields, and pass the variant. -rule: - # In this order: the call binds $MSG, then a block around the call must hold a `let` of that - # same name as a direct statement. A `let` in a sibling block is out of scope at the call. - all: - - any: - - pattern: $CMD.error($KIND, $MSG) - - pattern: Error::raw($KIND, $MSG) - - pattern: clap::Error::raw($KIND, $MSG) - - inside: - kind: block - stopBy: end - has: - any: - - pattern: let $MSG = format!($$$); - - pattern: "let $MSG: String = $VAL;" - - pattern: let $MSG = $SRC.to_string(); - - pattern: let $MSG = String::from($$$); - # .to_owned() only on a string literal: on anything else it may clone a typed error. - - all: - - kind: let_declaration - - regex: '=\s*"(?s:.*)"\s*\.to_owned\(\)\s*;?$' - - has: - field: pattern - pattern: $MSG -constraints: - MSG: - kind: identifier diff --git a/lint/ast-grep/rules/clap-error-message.yml b/lint/ast-grep/rules/clap-error-message.yml deleted file mode 100644 index 556a8f959d..0000000000 --- a/lint/ast-grep/rules/clap-error-message.yml +++ /dev/null @@ -1,27 +0,0 @@ -id: clap-error-message -language: rust -severity: error -message: clap error built from a string -note: | - Give the message a variant on a #[derive(thiserror::Error)] enum and pass the variant, e.g. - `cmd.error(ErrorKind::ArgumentConflict, UsageError::BridgeRoots)`. A string literal, format!, - .to_string() or a SCREAMING_CASE const keeps the message apart from the error's type. -rule: - any: - - pattern: $CMD.error($KIND, $MSG) - - pattern: Error::raw($KIND, $MSG) - - pattern: clap::Error::raw($KIND, $MSG) -constraints: - MSG: - any: - - kind: string_literal - - all: - - kind: macro_invocation - - regex: ^format! - - all: - - kind: identifier - - regex: ^[A-Z][A-Z0-9_]*$ - - all: - - kind: call_expression - # .to_owned() only on a string literal: on anything else it may clone a typed error. - - regex: \.to_string\(\)$|^String::from\(|^"(?s:.*)"\s*\.to_owned\(\)$ diff --git a/lint/ast-grep/rules/string-error.yml b/lint/ast-grep/rules/string-error.yml deleted file mode 100644 index fc61ff197d..0000000000 --- a/lint/ast-grep/rules/string-error.yml +++ /dev/null @@ -1,24 +0,0 @@ -id: string-error -language: rust -severity: error -message: String as an error type -note: | - Return a #[derive(thiserror::Error)] enum, so a caller can match on what failed and the - messages live with their variants. -rule: - any: - - pattern: - context: type T = Result<$OK, String>; - selector: generic_type - - pattern: - context: type T = std::result::Result<$OK, String>; - selector: generic_type - - pattern: Err(format!($$$)) - - pattern: Err($S.to_string()) - - pattern: Err(String::from($$$)) - - all: - - pattern: Err($S) - - has: - kind: arguments - has: - kind: string_literal diff --git a/lint/ast-grep/sgconfig.yml b/lint/ast-grep/sgconfig.yml deleted file mode 100644 index 9c94bd0ae6..0000000000 --- a/lint/ast-grep/sgconfig.yml +++ /dev/null @@ -1,6 +0,0 @@ -# Structural rules for the command-line code (RFC-0012), run by .github/workflows/ast-grep.yml. -# Each rule in rules/ has its cases in rule-tests/, which CI checks before the scan. -ruleDirs: - - rules -testConfigs: - - testDir: rule-tests