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 is mscorlib\mscorlib.[version].Contracts.xml. I just commented this annotation:
<member name="M:System.Enum.GetName(System.Type,System.Object)">
<attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>
And restarted Visual Studio, and now the warning is gone :)
Just an update to Thomas's answer, they seem to have moved things around in the last couple of years.
For Resharper 8.2.3, the above file now resides in:
C:\Users\YOUR_USER_NAME\AppData\Local\JetBrains\ReSharper\vAny\packages\ReSharper.ExternalAnnotations.8.2.3001\ReSharper\vAny\annotations\.NETFramework\mscorlib\...
so, if you're trying to do that, you can search that path (or one up, for non mscorlib files).
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