This commit is contained in:
yujonglee 2026-09-30 10:30:32 -04:00 • committed by GitHub
commit 33d4abf75d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,20 @@
id: no-format-in-error-variant
valid:
- |
fn f(route: &str) -> Result<(), Error> {
let msg = format!("bad route: {route}");
Err(Error::InvalidProvider(msg))
}
- |
fn f() -> String {
format!("just a string")
}
invalid:
- |
fn f(route: &str) -> Result<(), Error> {
Err(Error::InvalidProvider(format!("bad route: {route}")))
}
- |
fn f(name: &str) -> PyResult<()> {
Err(PyValueError::new_err(format!("missing argument: {name}")))
}

View file

@ -0,0 +1,12 @@
id: no-format-in-error-variant
language: rust
severity: warning
message: >-
Don't build an error variant straight from a format!() call; rustfmt wraps it into an
unreadable multi-line call. Bind the message to a local first, e.g.
`let msg = format!(...); $PATH::$VARIANT(msg)`.
rule:
pattern: $PATH::$VARIANT(format!($$$ARGS))
constraints:
PATH:
regex: 'Error$'

View file

@ -0,0 +1,4 @@
ruleDirs:
- ast-grep/rules
testConfigs:
- testDir: ast-grep/rule-tests