2021-01-27 13:42:22 +01:00
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.
2021-01-22 04:06:24 +00:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when an ``++IndexOf++`` value retrieved from a ``++string++``, ``++List++`` or array is tested against ``++> 0++``.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule also raises an issue when ``++IndexOfAny++``, ``++LastIndexOf++`` or ``++LastIndexOfAny++`` from a ``++string++`` is tested against ``++> 0++``
2021-01-22 04:06:24 +00:00
== Noncompliant Code Example
----
2021-01-23 04:07:47 +00:00
Dim Color As String = "blue"
Dim Name As String = "ishmael"
2021-01-22 04:06:24 +00:00
2021-01-23 04:07:47 +00:00
Dim Strings As New List(Of String)
Strings.Add(Color)
Strings.Add(Name)
Dim StringArray As String() = Strings.ToArray()
2021-01-22 04:06:24 +00:00
2021-01-23 04:07:47 +00:00
If Strings.IndexOf(Color) > 0 Then ' Noncompliant
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Name.IndexOf("ish") > 0 Then ' Noncompliant
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Name.IndexOf("ae") > 0 Then ' Noncompliant
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Array.IndexOf(StringArray, Color) > 0 Then ' Noncompliant
2021-01-22 04:06:24 +00:00
' ...
End If
----
== Compliant Solution
----
2021-01-23 04:07:47 +00:00
Dim Color As String = "blue"
Dim Name As String = "ishmael"
2021-01-22 04:06:24 +00:00
2021-01-23 04:07:47 +00:00
Dim Strings As New List(Of String)
Strings.Add(Color)
Strings.Add(Name)
Dim StringArray As String() = Strings.ToArray()
2021-01-22 04:06:24 +00:00
2021-01-23 04:07:47 +00:00
If Strings.IndexOf(Color) > -1 Then
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Name.IndexOf("ish") >= 0 Then
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Name.Contains("ae") Then
2021-01-22 04:06:24 +00:00
' ...
End If
2021-01-23 04:07:47 +00:00
If Array.IndexOf(StringArray, Color) >= 0 Then
2021-01-22 04:06:24 +00:00
' ...
End If
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]