2

I'm using Resharper 5.x to do compile-time analysis and it's usually pretty good about it, but it doesn't seem to be applying code contracts to its logic. I have something like the following, but I'm getting an issue on the marked line.

public void Method(int arg)
{
    Contract.Requires(this.NullableValueType != null);

    this.Method2(
        arg,
        this.NullableValueType.Value, // [1]
        this.ReferenceType);
}

[1] ends up getting highlighted with "Possible 'System.InvalidOperationException'". Is there a way to get rid of this error without turning off the check?

Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
  • 1
    Duplicate question, see: http://stackoverflow.com/questions/929859/resharper-possible-null-assignment-when-using-microsoft-contracts - you need to tell Resharper about the Code Contracts validation methods. – porges Sep 15 '10 at 22:01

1 Answers1

0

While admittedly Resharper could be more intelligent and take contracts into account, unfortunately currently it doesn’t.

I would recommend to make the line more explicit. Instead of

this.NullableValueType.Value

you could write

this.NullableValueType ?? <something>

where the “something” is, of course, something that doesn’t matter because it never happens (for example, new ThatValueType()).

Timwi
  • 65,159
  • 33
  • 165
  • 230