2

Seems like Dialog results return a nullable boolean (bool?), so whenever using a return dialog, you should take care of the null option (which I'm yet waiting to see happening ... ).

The above code (VS2012, Resharper 8), suggests that the left operand is never null. Any ideas why?

bool? dlg_result = some_window.ShowDialog();
bool some_bool = !dlg_result ?? true; // Resharper suggests this won't happen.

Here's a screenshot, easier to see:

screenshot

Edit:
I've had a look in the resharper files, and it seems like ShowDialog won't be null when using it from CommonDialog and the Control, but it might be null when using it from Window. Attaching screenshot.
Will have to dig a bit deeper it seems, since I'm not sure which one I'm using.

screenshot2

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • 5
    Well, you may look at this answer to see that the cases where ShowDialog returns a null is really specific. In this case, I would say it won't happen : http://stackoverflow.com/questions/990109/when-would-showdialog-return-null – Raphaël Althaus Nov 13 '14 at 08:58
  • @SonerGönül well, a `bool?` :) – Noctis Nov 13 '14 at 20:46
  • @RaphaëlAlthaus cheers for the link. I'll use that in conjunction with the "fix" resharper annotation, and see what's up. – Noctis Nov 13 '14 at 20:47

1 Answers1

2

ReSharper includes a feature called Code Annotations which provides a mechanism for decorating source code with hints for ReSharper. For example:

[NotNull]
public object Foo()
{
    return null; // Warning: Possible 'null' assignment
}      

The NotNull attribute will instruct ReSharper to issue a Visual Studio warning in the method if it tries to return null. It will also cause ReSharper to issue the suggestion that a null coalescing operator (??) is not necessary because the method return value is never null:

Foo() ?? new object; //Causes '??' left operand is never null suggestion

ReSharper has additional support for adding External Annotations to core .Net code. This allows these hints to be added to code that ReSharper doesn't control. (You can also use this if you wanted to add annotation to a 3rd party library).

So to wrap up, it appears ReSharper has annotated Window.ShowDialog with [NotNull] so even though the return type is nullable, ReSharper "knows" it will never be null and makes the suggestion you are seeing.

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • 1
    And see [Can I “fix” wrong Resharper annotations?](http://stackoverflow.com/questions/10254845/can-i-fix-wrong-resharper-annotations) for what to do when JetBrains' shipped annotations are wrong. – AakashM Nov 13 '14 at 10:09
  • Cheers for that. I'll have a look at the annotations "fixing" as well, and update – Noctis Nov 13 '14 at 20:45