A static readonly field of a primitive type or string is initialized with a literal and could be const
What it finds
A const is folded into the code that uses it; a static readonly field is read from memory on every use and keeps the JIT from treating the value as a constant. For a literal of a built-in type, const says the same and runs faster. Public fields are left alone, since changing a public const requires recompiling every caller.
From the rule's source, readonly_static_literal.rs.
class Limits{ private static readonly int MaxItems = 100;tx0 reports: MaxItems is static readonly with a literal value private const int MinItems = 1; public static readonly string Name = "limits";} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:readonly-static-literal" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:readonly-static-literal] reason="..."tx0 explain tx0/csharp:readonly-static-literal shows the same in the terminal. Found a false positive? Report it.