a02d892f831f
clap-error-message-variable.yml
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 the function must hold a `let` of that same name.
all:
- any:
- pattern: $CMD.error($KIND, $MSG)
- pattern: Error::raw($KIND, $MSG)
- pattern: clap::Error::raw($KIND, $MSG)
- inside:
kind: function_item
stopBy: end
has:
stopBy: end
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
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.