I love Resharper, but sometimes it gives incorrect warnings, probably because the built-in annotations for BCL types are wrong. For instance, in this code:
private static string GetDescription(T value)
{
Type type = typeof(T);
string name = Enum.GetName(type, value);
if (name != null)
{
...
It gives me a warning on the if statement: “Expression is always true”. But Enum.GetName can return null:
string name = Enum.GetName(typeof(DayOfWeek), (DayOfWeek)42); // null
I assume this is because there is a [NotNull] annotation for Enum.GetName. Is there a way to fix that so I don’t get the warning?
Note: I’m using Resharper 5.1; perhaps that issue is fixed in version 6, but I’m not willing to upgrade right now.
OK, I got it. The built-in annotations are defined in XML files in the Resharper installation directory (
C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\on my machine). The solution is to edit the appropriate file to remove or fix the incorrect annotations.In the case of
Enum.GetName, the file to change ismscorlib\mscorlib.[version].Contracts.xml. I just commented this annotation:And restarted Visual Studio, and now the warning is gone 🙂