Prevent Resharper "Possible Null Reference Exception" warnings
Asked Answered
D

2

6

Let's say I have an interface with a read-only property and and a concrete class where the property is instantiated in the constructor and marked as read-only.

internal interface IExample
{
    ObservableCollection<string> Items { get; }
}


internal class Example : IExample
{ 
    private readonly ObservableCollection<string> _items;

    public Example()
    {
       _items = new ObservableCollection<string>();
    }

    public ObservableCollection<string> Items
    {
       get { return _items; }
    }
}

When I use the interface Resharper warns me that I might have a possible null reference in calling code.

public class ExampleWithWarnings
{
    public void Show()
    {
       IExample example = new Example();

       // resharper warns about null reference
       example.Items.Add( "test" );
    }
}

I realize that by definition the interface doesn't guarantee that the property will have a value. (I also recognize that properties on interfaces aren't ideal). But I know this property will always have a value.

Is there any magic attribute that I can put on the interface that would prevent Resharper from showing a warning? I'd rather not have to decorate all usages of the class with a disable pragma warning.

Decoration answered 2/3, 2011 at 19:58 Comment(7)
I don't suppose you'd consider uninstalling Resharper? You can possibly shut it up by initializing the readonly member directly rather than doing it in the constructor. It's the same thing.Coltin
Which version of ReSharper are you using?Smithy
@Hans, the issue isn't the concrete class.Decoration
@Kalman, I'm using Resharper 5.1Decoration
What version of R# are you using? I'm on 5.1.3 here and it is not warning on the .Add() line that your comment points to.Boyse
@bryanbcook, I am using R# 5.1.3000.12, no warnings here.Smithy
@Kalman, @48klocks: it depends on your settings. In the betas these were called the "pessimistic" and "optimistic" mode, but that was confusing and now the options are spelled out. You're probably running on optimistic, and bryan on pessimistic.Only
O
7

Yes, there is an attribute you can use: JetBrains.Annotations.NotNullAttribute. But you don't need to add a reference to ReSharper in your project. You can use your own implementation: open the ReSharper options, and under Code Inspection > Code Annotations you will find a "Copy default implementation to clipboard". Now just paste that into a code file in your project. You can even change the namespace.

The Code Annotations settings

And then slap the attribute in the interface property.

You should also have a look under Code Inspection > Settings and pick "Assume entity can be null... when entity is explictly marked with CanBeNull attribute, or checked for null". This way you only get warnings in the members you explicit mark as troublesome.

The Code Inspection settings

Only answered 2/3, 2011 at 20:6 Comment(0)
J
3

You can reduce this warning to a suggestion. You could also edit the external annotation files to create custom rules or behavior: http://msmvps.com/blogs/peterritchie/archive/2008/07/21/working-with-resharper-s-external-annotation-xml-files.aspx

Jacobsohn answered 2/3, 2011 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.