Search code examples
visual-studio-2010compiler-warningscode-contracts

Turn off code contracts warning


I want to turn off a code contract warning, but only for specific code lines. How do I do that?

For instance, I get:

Warning 87  CodeContracts: requires unproven: key != null   

for:

return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];

which will never happen in our applications.


Solution

  • Well, one option would be:

    string key = typeof(T).AssemblyQualifiedName;
    Contract.Assume(key != null);
    return HttpContext.Current.Items[key];
    

    It's a bit ugly, but I believe it should work.