Sunday, 10 February 2013

Using ContractAttributes to let ReSharper engine understand null checking methods

One of my favorite Extension Methods in FluentSharp is the .isNull() which will return null if the object it is used on is null.

The problem is that ReSharper's analysis engine doesn't understand it, so we would get an error/warning like this:


Which is obviously not correct, since aString.isNull() will return true in this case.

To deal with this, I added a couple JetBrains ContractAnnotation to those Extension Methods:


And now ReSharper’s engine will detect that those code paths (that previously flagged the null exception) will never be taken:


Note that I also added Contract Annotations to the isFalse and isTrue extension methods


The only annoying thing is that it seems that the ContractAnnotation class needs to be in the JetBrains.Annotations namespace, which I didn’t really want to include in the FluentSharp.CoreLib.dll (I’m sure there is a a workaround, which I’ll find later)