kwiri
kwiri

Reputation: 1419

Suppressing Possible null reference exception message from Resharper on a asp.net mvc view model

consider code like this:

[HttpPost]   
     public ActionResult Index(TestModel testModel)
    {
        //ReSharper PossibleNullReferenceException message appears on the testModel
        var x = testModel.Name;
        return View(testModel);
    }

Is there a better way to tell Resharper that the testModel can never be null.I want to avoid placing Debug.Assert,If null throw etc all over my action methods it seems too verbose. This also applies to my service variable( I am using dependecy injection,_myService.blah etc) .I love the green Resharper tick(no errors or warings) on the class that tells me I have handled all null references and code correctness.But if I ignore the messages I do not get the green tick.

How do you guys deal with this, do you suppress the message with a comment, place if null throw,add debug assert(this just makes code a bit too verbose for things that you can assume can never be null.

NOTE: I do not want to switch off null reference checking( I have set them to warnings).

Upvotes: 2

Views: 1725

Answers (1)

Igal Tabachnik
Igal Tabachnik

Reputation: 31548

Hmm, it sounds like you have turned on "pessimistic null checking" in ReSharper, where it assumes everything that is not explicitly checked for null, or marked with [NotNull] is null. By default, ReSharper uses the "optimistic" approach, where if you're not explicitly checking for null or marking the entity with [CanBeNull] attribute, ReSharper won't flag it.

You want to be using the optimistic mode, make sure it's enabled by going to Code Inspection → Settings in ReSharper Options, and making sure the highlighted entry is selected:

This should reduce the number of possible null reference warnings significantly!

Upvotes: 4

Related Questions