Files
photocraft/lint/ast-grep/rules/args-os.yml
T
IvanandClaude Opus 5.5 695590c106 ci(cli): check the command-line code with ast-grep rules
lint/ast-grep holds four rules from RFC-0012 for the CLI code
(apps/photocraft-cli, the shared launch options in
crates/automation/src/args.rs, and the desktop app's main.rs and
launch_args.rs, which read its arguments):

- clap-error-message: no string literal, format!, .to_string() or
  SCREAMING_CASE const passed to Command::error or Error::raw.
- clap-error-message-variable: the same, through a variable built with
  format!, .to_string(), String::from or a literal's .to_owned() in the
  block around the call.
- string-error: no Result<_, String>, Err(format!(..)), Err("..") or
  Err(x.to_string()).
- args-os: no std::env::args() (also when imported or renamed), which
  panics on a non-Unicode argument.

Each rule has valid and invalid cases in rule-tests/. The workflow calls
craft-common-actions' ast-grep.yml, which runs the rule tests and then
the scan. It takes seconds and builds nothing.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-28 15:33:36 +02:00

32 lines
1.1 KiB
YAML

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;