Support a custom severity in the banned syntax entries of MA0240

Merged
ME
2mo ago
+383-22

What

The entries of a BannedSyntaxes.txt file (MA0240) now accept an optional severity:

query
query;message
query;severity;message
query;severity;

The severities are the names of the dotnet_diagnostic.<id>.severity option, and they are case-insensitive:

SeverityEffect
errorReported as an error
warningReported as a warning. This is the default
suggestion / infoReported as a suggestion
silent / hiddenReported as a hidden diagnostic
noneThe entry reports nothing. Its query is still validated by MA0241

Why

An entry could only report a warning, so a file could not ban some syntax as an error while reporting the rest as a warning or as a suggestion. none also makes it possible to turn off an entry that comes from a BannedSyntaxes.txt shared by several projects, without editing the shared file.

Notes for the reviewer

  • A DiagnosticDescriptor carries a single severity, so there is now one descriptor per severity sharing the MA0240 id, all listed in SupportedDiagnostics. The warning one is first, so it stays the default severity of the rule and the markup-based tests (MarkupOptions.UseFirstDescriptor) keep resolving to it.
  • Setting dotnet_diagnostic.MA0240.severity in an .editorconfig file still overrides the severity of all the entries, as this option is about the whole rule. This is documented.
  • A message can contain a ;, so a line only has a severity when the field between the two separators is a single word made of letters. GotoStatement; Do not use goto; use a loop is therefore still a plain message. A single word that is not a known severity, such as GotoStatement;Avoid;it is not readable, is reported by MA0241 ('Avoid' is not a valid severity), so a typo does not silently change the message.
  • An entry whose severity is none is still parsed and validated, so an invalid query in it is reported by MA0241.

Tests

16 new tests. The full test project passes (4887/4887) on Roslyn 5.9, and the MA0240 tests pass (90/90) on Roslyn 4.8, 4.14, 5.0 and 5.6. dotnet run --project src/DocumentationGenerator reports no further changes.