rspec/rules/S2692/vbnet/rule.adoc

78 lines
1.8 KiB
Plaintext
Raw Normal View History

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
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]