Can I “fix” wrong Resharper annotations?

谁说胖子不能爱 提交于 2019-12-12 09:52:36

问题


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.


回答1:


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 :)




回答2:


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).



来源:https://stackoverflow.com/questions/10254845/can-i-fix-wrong-resharper-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!