dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

Port FxCop rule CA2204: LiteralsShouldBeSpelledCorrectly #529

Open ghost opened 8 years ago

ghost commented 8 years ago

Title: Literals should be spelled correctly

Description:

A literal string in a method body contains one or more words that are not recognized by the Microsoft spelling checker library.

Dependency: Dataflow

Notes:

mavasani commented 6 years ago

This rule uses StringContent dataflow analysis and should be implementable once we merge https://github.com/dotnet/roslyn-analyzers/pull/1541

mavasani commented 6 years ago

1541 is merged.

mavasani commented 6 years ago

This rule is blocked by absence of Naming service.

daiplusplus commented 5 years ago

I'd like to draw attention to a bug in the current (pre-Roslyn) CA2204 rule where compound-words in literals are flagged by the rule because it doesn't break the compound-words up despite documentation claiming otherwise ("This rule parses the literal string into words, tokenizing compound words, and checks the spelling of each word or token. "). This also impacts use of nameof() in C#.

For example in my project I have this line of code:

throw new InvalidOperationException( nameof(this.ReadMessages) + " invoked before method has returned." );

This currently gives me this code analysis build warning:

Warning - CA2204 - Literals should be spelled correctly - Correct the spelling of the unrecognized token 'ReadMessages' in the literal '"ReadMessages invoked before method has returned."'.

A workaround is to add the word "readmessages" to my Code Analysis Dictionary file's <Recognized><Word> list, however this is not a correct solution because it then makes "readmessages" (case-insensitive) a recognized word (which it isn't, it's a compound word) which then then causes this analysis warning to appear on my ReadMessages method:

Warning - CA1702 - Compound words should be cased correctly - The compound word 'ReadMessages' in member name 'MyClass.ReadMessagesAsync(NetworkStream, byte[])' exists as a discrete term. If your usage is intended to be single word, case it as 'Readmessages' or strip the first token entirely if it represents any sort of Hungarian notation.

(Note it instructs me to use Readmessages instead of ReadMessages).

I'd also like to request that the code-analysis rules automatically add any nameof() value (or better yet: any C# identifier in the project, regardless of access modifier (public, internal, etc)) be automatically added to the list of recognized words when checking string literals (with correct compound word handling, of course).