Report the contextual keywords in MA0154 - #1516
Merged
Merged
Conversation
The keyword list of MA0154 only contained the 77 reserved keywords, so <c>await</c>, <c>nint</c> or <c>record</c> were never converted to <see langword="..."/>, while <c>int</c> was. Add the contextual keywords and the type keywords that are unlikely to be used as an identifier: async, await, dynamic, init, nameof, nint, nuint, partial, record, required, scoped and var. The list stays hard-coded rather than derived from SyntaxFacts: nint and nuint are not keyword kinds, as they are parsed as identifiers, so the main gap would remain; the contextual keyword kinds differ between the supported Roslyn versions, which would make the rule behave differently from one version to another; and the contextual keywords that are also ordinary identifiers (value, from, select, add, remove, get, set) would have to be excluded by hand anyway to avoid false positives on <c>value</c>. Also remove the unused System.Linq.Expressions using directive.
meziantou
enabled auto-merge (squash)
September 12, 2026 20:18
This was referenced Sep 12, 2026
Closed
This was referenced Sep 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
MA0154 (Use langword in XML comment) matched its
<c>/<code>content against a hand-maintained list of the 77 C# reserved keywords. Keywords added to the language since C# 1.0 were missing, so<c>await</c>,<c>nint</c>or<c>record</c>were never reported, while<c>int</c>was.The following are now reported:
async,await,dynamic,init,nameof,nint,nuint,partial,record,required,scoped,var.The unused
using System.Linq.Expressions;in the analyzer is also removed.Why the list stays hard-coded
Deriving the set from
SyntaxFacts.GetKeywordKinds()/GetContextualKeywordKinds()looks tempting, but:nintandnuinthave no keyword kind — they are parsed as identifiers — so the main reported gap would remain.value,from,select,add,remove,get,set) would have to be subtracted by hand anyway to avoid false positives on things like<c>value</c>, so the maintenance burden does not actually go away.Those ambiguous ones are therefore deliberately excluded; a comment in the analyzer says so.
Tests
ValidateSummary_Invalidcase per new keyword, checking both the diagnostic and the code fix output.MissingLanguageAttributecases asserting<c>value</c>,<c>from</c>,<c>get</c>and<c>set</c>still report MA0219 and not MA0154.UseLangwordInXmlCommentAnalyzerTestsruns 42 tests (up from 25), all passing onroslyn5.9androslyn4.8.dotnet run --project src/DocumentationGeneratorexits 0, so no generated markdown changed;docs/Rules/MA0154.mdis updated by hand to document which keywords are recognized and which are not.