-2

I am using VS 2010 and testing something real basic :

class Program
{
    static void Main(string[] args)
    {
        var numbers = new int[2];
        numbers[3] = 0;
    }
}

I have gone to properties > Code Contracts and have enabled the static checking. No errors / warnings/squiggly underlines are showing on compile / build.

enter image description here

EDIT:

When turning the warning level to max I get this warning, which is not the warning I am after :

Warning 1 CodeContracts: Invoking method 'Main' will always lead to an error. If this is wanted, consider adding Contract.Requires(false) to document it

sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • Try putting at the start of the method `Contract.Requires(args != null)`; – AgentFire Mar 21 '14 at 13:02
  • @AgentFire tried that but it did not work. – sprocket12 Mar 21 '14 at 15:00
  • 1
    You say "I get this warning, which is not the warning I am after", but what *is* the warning you're after? If you're expecting it to complain that you're accessing numbers[3] when the array is defined as int[2], how is that not covered by the error you've quoted? The error says "Invoking method 'Main' will always lead to an error", which is perfectly accurate and caused solely by that out-of-bounds access. – Chris Mar 21 '14 at 15:52
  • @Chris so you are saying that's as accurate a warning I will get? The squiggly line is even under the wrong line, its under the top one instead of the bottom one. – sprocket12 Mar 21 '14 at 15:57
  • This simply isn't the kind of programming error the Code Contracts is supposed to be looking for. It's clearly doing enough to know that this method will always throw an exception, but I'd say that yes - this is the most descriptive error message you're likely to get from this kind of programming error. – Chris Mar 21 '14 at 16:06

2 Answers2

2

It's not clear what warning you're expecting (you state "I get this warning, which is not the warning I am after" without actually saying what warning you are after), but perhaps this will help.

First up:

var numbers = new int[2];
numbers[3] = 0;

This is an out-of-bounds access that will fail at runtime. This is the cause of the error you're getting, which states that "Invoking method 'Main' will always lead to an error." - that's perfectly accurate, it will always lead to an error because that out-of-bounds array access will always throw a runtime exception.

Since you state that this isn't the warning you're expecting, though, I've had to employ a bit of guesswork as to what you were expecting. My best guess was that due to having ticked the 'Implicit Non-Null Obligations' checkbox, and also having tried adding Contract.Requires(args != null) to your code, you're expecting to get a warning that your Main method could potentially be called with a null argument.

The thing is, Code Contracts will only inspect your own code to make sure that you always provide a non-null argument when calling Main. The thing is, you never call Main at all - the operating system will call Main, and Code Contracts is not going to inspect the operating system's code!

There's no way to provide compile-time checking of the arguments provided to Main - you have to check these at runtime, manually. Again, Code Contracts works by checking that calls you make to a function meet the requirements you set - if you're not actually making the calls yourself, Code Contracts has no compile-time say in the matter.

Chris
  • 4,661
  • 1
  • 23
  • 25
  • 1
    To be honest its not hard to guess what I am expecting, simply "array out of bounds" would have been fine. – sprocket12 Mar 21 '14 at 18:46
  • Code Contracts is designed to look for violations of the preconditions and postconditions of your methods - it's focused on that and is not intended to provide warnings for all types of coding errors that could be made inside your methods. The error you've added in has nothing to do with preconditions and nothing to do with postconditions *except* the implicit postcondition that a method probably shouldn't fail every single time you call it. That's why the error message is worded how it is. If you're looking for a code-analysis tool that will explicitly catch this, Code Contracts isn't it. – Chris Mar 24 '14 at 08:58
0

I have tried this (albeit with Visual Studio 2013 + Code Contracts) and I found the following:

  • With the Warning Level set to "low" (like you have), I do not get a warning.
  • With the Warning Level set to "hi", I do get a warning.

So I suggest increasing your warning level slider.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276