Add analyzer: Warn on illegal format specifiers #45960
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Runtime
code-analyzer
Marks an issue that suggests a Roslyn analyzer
Milestone
Based on feedback from issue dotnet/roslyn#49471
Format specifiers are often compile-time constants that can be checked by an analyzer. This could be done in roslyn itself or as an SDK analyzer. For some specifiers a fix can be provided:
dt.ToString("h")
->dt.ToString("%h")
.Sample on sharplab.io
The analyzer should detect format specifiers and check whether the specifier is valid. One possibility could be to simulate the behavior at runtime by passing
default
values:Parsed code:
In the analyzer the logic could be something like this:
If an custom format provider is used, the analyzer should do nothing.
The text was updated successfully, but these errors were encountered: