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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Connell
  • 13,925
  • 11
  • 59
  • 92
  • 1
    I think this question needs more context. How is `User` defined? How is it used in the class? – Will Vousden Oct 24 '12 at 16:52
  • @WillVousden it's a property on the Controller class. It's part of the ASP.NET Framework. – vcsjones Oct 24 '12 at 16:52
  • 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. – Paul Knopf Oct 24 '12 at 16:52
  • @WillVousden User is part of the Controller class that I linked to in the first line. It inherits `IPrincipal`. – Connell Oct 24 '12 at 16:53
  • @PaulKnopf but those special resharper comments are so damn ugly!! :( – Connell Oct 24 '12 at 16:54
  • 1
    Maybe it just knows that it is very unlikely that this property is going to be null. – Miguel Angelo Oct 24 '12 at 16:57

1 Answers1

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

Pang
  • 9,564
  • 146
  • 81
  • 122
bitbonk
  • 48,890
  • 37
  • 186
  • 278
  • 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? – Connell Oct 24 '12 at 16:59
  • Because you could obviously set `User = null` somewhere in your own custom controller class before. – poke Oct 24 '12 at 17:02
  • Because `User` actually *can* be `null`. – bitbonk Oct 24 '12 at 17:03
  • @ConnellWatkins http://www.jetbrains.com/resharper/webhelp/Code_Analysis__External_Annotations.html There is a `[NotNull]` attribute. – cadrell0 Oct 24 '12 at 17:03
  • 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. – Connell Oct 24 '12 at 17:06
  • @cadrell0, I mean for the `Identity` property. – Connell Oct 24 '12 at 17:08
  • 1
    @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. – cadrell0 Oct 24 '12 at 17:10
  • @cadrell0, so are you telling me the NotNull attribute **is** in those XML files? That would indeed answer the question. – Connell Oct 24 '12 at 17:17
  • @bitbonk, my IPrincipal implementation is the `System.Security.Principal.GenericPrincipal` class ;) – Connell Oct 24 '12 at 17:19
  • @cadrell0: +1 (in your comment) for pointing out `ReSharper_installation_dir\Bin\ExternalAnnotations` xml file. I didn't know that this xml existed. =) – Miguel Angelo Oct 24 '12 at 17:29
  • Came back this morning and suddenly, brilliant answer! Explained everything between you and @cadrell0. Thank you both :) – Connell Oct 25 '12 at 08:27