Why is ReSharper telling me that "User.Identity == null" will always be false?
Asked Answered
D

1

16

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

enter image description here

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?

Darkling answered 24/10, 2012 at 16:49 Comment(6)
I think this question needs more context. How is User defined? How is it used in the class?Disembarrass
@WillVousden it's a property on the Controller class. It's part of the ASP.NET Framework.Baste
I run into many cases like this. I either tell resharper to ignore this special case with special code comments, or I disable the notification completely if it is for something I would care less about. Everything can't be perfect, but I think resharper makes up for these slight issues.Filings
@WillVousden User is part of the Controller class that I linked to in the first line. It inherits IPrincipal.Darkling
@PaulKnopf but those special resharper comments are so damn ugly!! :(Darkling
Maybe it just knows that it is very unlikely that this property is going to be null.Exacting
S
17

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

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

Serbocroatian answered 24/10, 2012 at 16:56 Comment(11)
You'd think there'd be a [NotNull] attribute at least? And if this is the case, why wouldn't it moan about my check for User == null too?Darkling
Because you could obviously set User = null somewhere in your own custom controller class before.Cheston
Because User actually can be null.Serbocroatian
@ConnellWatkins jetbrains.com/resharper/webhelp/… There is a [NotNull] attribute.Contiguity
I don't think you can actually set the User property, but if I could, then I could set it to my own object that inherits IPrincipal. This object could have a null value for the Identity property.Darkling
@cadrell0, I mean for the Identity property.Darkling
@ConnellWatkins That article explains that these annotations are stored in XML files in "ReSharper_installation_dir\Bin\ExternalAnnotations.". ReSharper cannot modify the .NET Framework to add these.Contiguity
@cadrell0, so are you telling me the NotNull attribute is in those XML files? That would indeed answer the question.Darkling
@bitbonk, my IPrincipal implementation is the System.Security.Principal.GenericPrincipal class ;)Darkling
@cadrell0: +1 (in your comment) for pointing out ReSharper_installation_dir\Bin\ExternalAnnotations xml file. I didn't know that this xml existed. =)Exacting
Came back this morning and suddenly, brilliant answer! Explained everything between you and @cadrell0. Thank you both :)Darkling

© 2022 - 2024 — McMap. All rights reserved.