3

[Note: This is similar to Compare string to null - Why does Resharper think this is always false?, but from the source, it appears there is no [NotNull] attribute on MailMessage.From.]

Consider this method:

public void Send(MailMessage mailMessage)
{
    if (mailMessage.From == null)
        mailMessage.From = new MailAddress(Settings.SmtpSettings.From);
    _smtpClient.Send(mailMessage);
}

ReSharper 7.1.1 is warning me that mailMessage.From cannot be null. I'm completely baffled by this.

mailMessage.From is a MailAddress which is a class (not a struct), so I would think it could definitely be null (although I concede it certainly shouldn't be at the time the message is sent).

Here is an image showing the ReSharper tooltip I'm getting:

ReSharper indicating that mailMessage.From == null is always false

Any explanation why ReSharper 7.1 thinks mailMessage.From cannot be null, or is this a bug?

Update

So the plot thickens...

I wrote a couple tests and got unexpected results.

This test fails:

[Test]
public void FromPropertyOfMailMessageCannotBeNull()
{
    var message = new MailMessage();
    Assert.IsNotNull(message.From);
}

And this one passes:

[Test]
public void FromPropertyOfMailMessageIsNullIfDefaultConstructorIsUsed()
{
    var message = new MailMessage();
    Assert.IsNull(message.From);
}

So, it looks like ReSharper is just plain wrong that MailMessage.From cannot be null.

Community
  • 1
  • 1
devuxer
  • 41,681
  • 47
  • 180
  • 292
  • 3
    If you ask JetBrains, you'll get an almost immediate answer (if they are awake). They try hard to help you and are happy to fix bugs as well. – alzaimar Dec 30 '12 at 23:39
  • I agree with alzaimar. JetBrains would be the best people to ask. – Ash Burlaczenko Dec 30 '12 at 23:41
  • @DanM, the last line of the default constructor of `MailMessage` is: `this.message.From = new MailAddress(from);`. The previous lines are, `string from = SmtpClient.MailConfiguration.Smtp.From; if (from == null || from.Length <= 0) return;`. So it does look like it can be null. – Kirk Woll Dec 31 '12 at 00:59
  • @KirkWoll, Interesting. It does seem like this is a bug in ReSharper then. (Or at least a mis-configuration.) – devuxer Dec 31 '12 at 02:22
  • @alzaimar, I did email them soon after asking this question. As of yet, I have not heard back, but they may have had a backlog of emails due to the holidays. – devuxer Jan 02 '13 at 19:46
  • @DanM: That is very unsual then. Did you contact them through VS and the Resharper Menu? I used the feedback form and simply said: 'Nice product, except for this bug...' answer on the same (or following?) day and an immediate conversation via mail... Well, they reside in Russia and besides the fact that russian vodka is excellent, they have public holidays throughout the 5th (most likely to cure the hangover or continue partying) – alzaimar Jan 02 '13 at 21:03
  • @alzaimar, I contact them via the JetBrains/ReSharper website. Maybe that was my problem? I did fill in my license number and all the requested info. – devuxer Jan 02 '13 at 23:30

2 Answers2

3

According to the documentation, ReSharper allows NotNullAttribute to be applied to external APIs (such as the .NET framework itself.)

In \Program Files (x86)\JetBrains\ReSharper\v7.1\Bin\ExternalAnnotations\.NETFramework\System.Net\4.0.0.0.Nullness.Gen.xml

You have:

<member name="M:System.Net.PeerToPeer.Collaboration.PeerNearMe.AddToContactManager(System.String,System.String,System.Net.Mail.MailAddress)">
    <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>

As you can see, it's adding the NotNullAttribute to the MailAddress class.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • 1
    Kirk, thanks for your answer. Are you saying that ReSharper has determined by looking at the *implementation* of `MailMessage` that `From` cannot be null and is then applying *it's own* `NotNull` attribute? What would happen if the implementation changes in the future, wouldn't that break my code? – devuxer Dec 30 '12 at 23:50
  • @DanM, yes, I believe that's exactly what they're doing. (though it's a guess) – Kirk Woll Dec 30 '12 at 23:57
  • Also, MS doesn't make breaking changes like that except (possibly!) between major framework releases. – Kirk Woll Dec 31 '12 at 00:03
  • Kirk, I sent out a message to ReSharper, referring back to this question and your answer, so hopefully we'll get some confirmation on their philosophy. Meanwhile, thanks again and +1. – devuxer Dec 31 '12 at 00:04
  • Hmmm...well this is interesting, see my update to my question. Now I'm really confused. – devuxer Dec 31 '12 at 00:56
3

It is a bug in ReSharper annotations. In .netFramework 4.0 this property has check for null value in it's setter but non-null value is not gauranted in the default constructor of MailMessage class. I've logged an issue for that: http://youtrack.jetbrains.com/issue/RSRP-337152

Shkredov S.
  • 2,097
  • 16
  • 8