Consider the following C# code:
if (atr == null) throw new InvalidOperationException("No ContentProperty attribute found on type.");
When building the project, a "CA2204
: Literals should be spelled correctly" warning is issued because of unrecognized word "ContentProperty".
I am aware that I could disable the rule (either globally or for the containing method only) or create a custom Code Analysis dictionary and add "ContentProperty" in it as a recognized word. However, none of these solutions sounds appealing to me. Referring to a type or class member name in an exception message is bound to happen quite a lot in my project, which is an application framework.
Does Code Analysis has a way to tell that a word / group of words isn't meant to be spell-checked, like when surrounded by quotation marks or something? Or is disabling the warning the only way around this?
With Visual Studio 2017 †, I have demonstrated that Code Analysis warning CA2204: Literals should be spelled correctly can be avoided by using the following additions to C# version 6:
if (atr == null)
{
throw new InvalidOperationException(
$"No {nameof(ContentProperty)} attribute found on type.");
}
You may also find my answer to String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305) for avoiding CA1305: Specify IFormatProvider to be helpful.
† Note that C# version 6 was delivered with Visual Studio 2013. A newer version of Visual Studio (with a newer version of Code Analysis) might also be necessary to avoid this warning.
I would use a different approach - as maintaining the Custom Dictionary might become a maintenance issue: there's no link to the actual class (in your example the ContentPropertyAttribute
). What happens if somebody renames or removes that class? The entries in the Custom Attributes must be synchronized manually which is error-prone.
Instead, I suggest using a bit of (dare I say it) reflection to inject the corresponding part of the string (instead of Resources that might end in having CA1703). Your example might be rewritten as:
throw new InvalidOperationException(string.Format("No {0} found on type.", typeof(ContentPropertyAttribute).Name);
Now you even have compile time safety for your message.
Does Code Analysis have a way to tell that a word isn't meant to be spell-checked, like when surrounded by quotation marks or something?
CA2204 only applies to string literals, i.e. strings that are hard-coded (surrounded by quotation marks). Disabling this code analysis rule will not prevent CA from checking the spelling on your class names, public members, or other code properties.
If your project is an application framework, where most/all string literals will be targeted at developers (like exception messages), I would recommend disabling this rule. To me, that makes more sense than coming up with a complicated method of excluding every unrecognized string in exception messages.
Another option would be to move the "misspelled" strings into a Resource.resx
file. However, you'll have the same problem if CA1703 is enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With