Is there a way to tell ReSharper that a null reference will not occur due to contract construction Requires verification? For example, the following code raises a 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 strange is that if you delete the Contract.Requires(...) , the ReSharper message will disappear.
Update
I found a solution through ExternalAnnotations, which Mike also mentioned below. Here is an example of how to do this for a function in Microsoft.Contracts:
- Create a directory under
Microsoft.Contracts in the ExternalAnnotations ReSharper directory. - Next, create a file called
Microsoft.Contracts.xml and write it like this:
<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 will disappear!
c # design-by-contract resharper code-contracts
HVS May 30 '09 at 14:52 2009-05-30 14:52
source share