61 lines
1.5 KiB
Plaintext
61 lines
1.5 KiB
Plaintext
Most checks against an ``++IndexOf++`` value compare it with -1 because 0 is a valid index. Any checks which look for values ``++> 0++`` ignore the first element, which is likely a bug. If the intent is merely to check inclusion of a value in a ``++string++``, ``++List++``, or an array, consider using the ``++Contains++`` method instead.
|
|
|
|
This rule raises an issue when an ``++IndexOf++`` value retrieved from a ``++string++``, ``++List++`` or array is tested against ``++> 0++``.
|
|
This rule also raises an issue when ``++IndexOfAny++``, ``++LastIndexOf++`` or ``++LastIndexOfAny++`` from a ``++string++`` is tested against ``++> 0++``
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
Dim Color As String = "blue"
|
|
Dim Name As String = "ishmael"
|
|
|
|
Dim Strings As New List(Of String)
|
|
Strings.Add(Color)
|
|
Strings.Add(Name)
|
|
Dim StringArray As String() = Strings.ToArray()
|
|
|
|
If Strings.IndexOf(Color) > 0 Then ' Noncompliant
|
|
' ...
|
|
End If
|
|
|
|
If Name.IndexOf("ish") > 0 Then ' Noncompliant
|
|
' ...
|
|
End If
|
|
|
|
If Name.IndexOf("ae") > 0 Then ' Noncompliant
|
|
' ...
|
|
End If
|
|
|
|
If Array.IndexOf(StringArray, Color) > 0 Then ' Noncompliant
|
|
' ...
|
|
End If
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
Dim Color As String = "blue"
|
|
Dim Name As String = "ishmael"
|
|
|
|
Dim Strings As New List(Of String)
|
|
Strings.Add(Color)
|
|
Strings.Add(Name)
|
|
Dim StringArray As String() = Strings.ToArray()
|
|
|
|
If Strings.IndexOf(Color) > -1 Then
|
|
' ...
|
|
End If
|
|
|
|
If Name.IndexOf("ish") >= 0 Then
|
|
' ...
|
|
End If
|
|
|
|
If Name.Contains("ae") Then
|
|
' ...
|
|
End If
|
|
|
|
If Array.IndexOf(StringArray, Color) >= 0 Then
|
|
' ...
|
|
End If
|
|
----
|