diff --git a/litellm-rust/ast-grep/rule-tests/no-format-in-error-variant-test.yml b/litellm-rust/ast-grep/rule-tests/no-format-in-error-variant-test.yml new file mode 100644 index 00000000000..45f8e03d800 --- /dev/null +++ b/litellm-rust/ast-grep/rule-tests/no-format-in-error-variant-test.yml @@ -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}"))) + } diff --git a/litellm-rust/ast-grep/rules/no-format-in-error-variant.yml b/litellm-rust/ast-grep/rules/no-format-in-error-variant.yml new file mode 100644 index 00000000000..3669b2e6a6e --- /dev/null +++ b/litellm-rust/ast-grep/rules/no-format-in-error-variant.yml @@ -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$' diff --git a/litellm-rust/sgconfig.yml b/litellm-rust/sgconfig.yml new file mode 100644 index 00000000000..e726b3bd149 --- /dev/null +++ b/litellm-rust/sgconfig.yml @@ -0,0 +1,4 @@ +ruleDirs: + - ast-grep/rules +testConfigs: + - testDir: ast-grep/rule-tests