Most checks against an IndexOf value compare it with -1 because
0 is a valid index.
strings.IndexOf(someString) == -1 // Test for "index not found"
strings.IndexOf(someString) < 0 // Test for "index not found"
strings.IndexOf(someString) >= 0 // Test for "index found"
Any checks which look for values > 0
ignore the first element, which is likely a bug. If the intent is merely to check the
inclusion of a value in a string
, List
, or array, consider using the Contains method instead.
strings.Contains(someString) // bool result
This rule raises an issue when the output value of any of the following methods is tested against > 0
:
someArray.IndexOf(someItem) > 0 // Noncompliant: index 0 missing
someString.IndexOfAny(charsArray) > 0 // Noncompliant: index 0 missing
someList.LastIndexOf(someItem) > 0 // Noncompliant: index 0 missing
someString.LastIndexOf(charsArray) > 0 // Noncompliant: index 0 missing