Linter: revive, Rule: string-format
- Allows you to configure a list of regular expressions that string literals in certain function calls are checked against. Should we enable it?
#15832
Labels
Description
This issue starts a discussion about enabling:
This is geared towards user facing applications where string literals are often used for messages that will be presented to users, so it may be desirable to enforce consistent formatting.
Configuration:
Each argument is a slice containing 2-3 strings: a scope, a regex, and an optional error message.
The first string defines a scope. This controls which string literals the regex will apply to, and is defined as a function argument. It must contain at least a function name (
core.WriteError
). Scopes may optionally contain a number specifying which argument in the function to check (core.WriteError[1]
), as well as a struct field (core.WriteError[1].Message
, only works for top level fields). Function arguments are counted starting at 0, so[0]
would refer to the first argument,[1]
would refer to the second, etc. If no argument number is provided, the first argument will be used (same as[0]
). You can use multiple scopes to one regex. Split them by,
(core.WriteError,fmt.Errorf
).The second string is a regular expression (beginning and ending with a
/
character), which will be used to check the string literals in the scope. The default semantics is "strings matching the regular expression are OK". If you need to inverse the semantics you can add a!
just before the first/
. Examples:"/^[A-Z].*$/"
the rule will accept strings starting with capital letters"!/^[A-Z].*$/"
the rule will a fail on strings starting with capital lettersThe third string (optional) is a message containing the purpose for the regex, which will be used in lint errors.
Example configuration:
Expected output
Decision about enabling or not enabling this rule.
Findings
For this rule (with above configuration), the following findings were found in the current codebase:
The text was updated successfully, but these errors were encountered: