ReSharper - Possible Null Assignment when using Microsoft.Contracts
Posted
by HVS
on Stack Overflow
See other posts from Stack Overflow
or by HVS
Published on 2009-05-30T14:52:13Z
Indexed on
2010/06/11
21:02 UTC
Read the original article
Hit count: 791
Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning (Possible 'null' assignment to entity marked with 'NotNull' attribute
) in ReSharper on lines 7 and 8:
private Dictionary<string, string> _Lookup = new Dictionary<string, string>();
public void Foo(string s)
{
Contract.Requires(!String.IsNullOrEmpty(s));
if (_Lookup.ContainsKey(s))
_Lookup.Remove(s);
}
What is really odd is that if you remove the Contract.Requires(...)
line, the ReSharper message goes away.
Update
I found the solution through ExternalAnnotations which was also mentioned by Mike below. Here's an example of how to do it for a function in Microsoft.Contracts:
- Create a directory called
Microsoft.Contracts
under theExternalAnnotations
ReSharper directory. - Next, Create a file called
Microsoft.Contracts.xml
and populate like so:
<assembly name="Microsoft.Contracts">
<member name="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean)">
<attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
<parameter name="condition">
<attribute ctor="M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)">
<argument>0</argument>
</attribute>
</parameter>
</member>
</assembly>
- Restart Visual Studio, and the message goes away!
© Stack Overflow or respective owner