mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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]>
36 lines
1.3 KiB
YAML
36 lines
1.3 KiB
YAML
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
|