Why is ReSharper telling me that “User.Identity == null” will always be false?

做~自己de王妃 提交于 2019-12-23 06:55:58

问题


I have a simple property inside one of my ASP.NET MVC Controller classes.

I've seen this many times before, so understand what the message means, but usually it makes perfect sense. This, however, doesn't. To get to the underlined statement, User would have to NOT be null, so the check for User.Identity is fine.

The Identity property is part of the IPrincipal interface, and returns an object that inherits IIdentity.

To inherit this interface, or any interface for that matter, this property must be a reference type, and therefore can potentially be null, right?

So why is my beloved ReSharper moaning?


回答1:


You said you are using the GenericPrinciple as the implementation of IPrincipal. For this class the Identity property can indeed never be null. It is easy to see if you look at the source code (e.g. using JetBrains dotPeek).

You can thank ReSharper's code annotations for the .NET framework class libraries for this.

In my ReSharper 6.1 annotations there is this single code annotation related to this (in file ExternalAnnotations\mscorlib\mscorlib.4.0.0.0.Nullness.Generated.xml):

  <member name="M:System.Security.Principal.GenericPrincipal.#ctor(System.Security.Principal.IIdentity,System.String[])">
    <parameter name="identity">
      <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
    </parameter>
  </member>

This is just for the contructor though, I haven't found one for the Identity property. So either you are using a ReSharper version that has annotation for that property too or ReSharper is doing some additional analysis.

I any case, it is ReSharper being clever (and right!).



来源:https://stackoverflow.com/questions/13053865/why-is-resharper-telling-me-that-user-identity-null-will-always-be-false

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