c# - Superfluous Code Contracts suggestions -


i have following method:

static bool textequals(string text, char[] array, int start, int length) {     contract.requires(text != null);     contract.requires(text.length != length                       || text.length == 0                       || (array != null && start >= 0 && start < array.length));      if (text.length != length)     {         return false;     }      (var = 0; < text.length; ++i)     {         if (text[i] != array[start + i])         {             return false;         }     }      return true; } 

however, code contracts suggesting add following:

contracts.requires(text.length == 0 || array != null || start < array.length); contracts.requires(text.length == 0 || start < 0 || start < array.length); 

i not seeing added benefit of these 2 additional requirements. path(s) covered existing requirements not cover?

in particular, not see case array == null && start < array.length, allowed first suggestion.

is there way remove such suggestions?

i'm not quite sure cases new suggestions trying cover, seems result of static analyser having hard time ored clause.

it's idea keep contract.requires expressions simple possible, both static analyser , make easier human readers.

in example, following set of requires clauses doesn't cause additional suggestions raised:

contract.requires(text != null); contract.requires(array != null); contract.requires(start >= 0); contract.requires(start <= array.length - length); contract.requires(length >= 0); 

these stricter originals, don't seem unreasonable callers.

in particular, notice start <= array.length - length needed prevent array overflow on array[start + i]; original condition start < array.length not sufficient, , might root of problem.

as aside, if you're willing use linq, rewrite method more succinctly follows:

static bool textequals2(string text, char[] array, int start, int length) {     contract.requires(text != null);     contract.requires(array != null);     contract.requires(start >= 0);     contract.requires(length >= 0);      return text.sequenceequal(array.skip(start).take(length)); } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -