Note that there are some explanatory texts on larger screens.

plurals
  1. POReSharper - Possible Null Assignment when using Microsoft.Contracts
    text
    copied!<p>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 (<code>Possible 'null' assignment to entity marked with 'NotNull' attribute</code>) in ReSharper on lines 7 and 8:</p> <pre><code>private Dictionary&lt;string, string&gt; _Lookup = new Dictionary&lt;string, string&gt;(); public void Foo(string s) { Contract.Requires(!String.IsNullOrEmpty(s)); if (_Lookup.ContainsKey(s)) _Lookup.Remove(s); } </code></pre> <p>What is really odd is that if you remove the <code>Contract.Requires(...)</code> line, the ReSharper message goes away. </p> <p><strong>Update</strong></p> <p>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:</p> <ul> <li>Create a directory called <code>Microsoft.Contracts</code> under the <code>ExternalAnnotations</code> ReSharper directory.</li> <li>Next, Create a file called <code>Microsoft.Contracts.xml</code> and populate like so:</li> </ul> <p></p> <pre><code>&lt;assembly name="Microsoft.Contracts"&gt; &lt;member name="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean)"&gt; &lt;attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/&gt; &lt;parameter name="condition"&gt; &lt;attribute ctor="M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)"&gt; &lt;argument&gt;0&lt;/argument&gt; &lt;/attribute&gt; &lt;/parameter&gt; &lt;/member&gt; &lt;/assembly&gt; </code></pre> <p></p> <ul> <li>Restart Visual Studio, and the message goes away!</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload