
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
97 lines
2.0 KiB
Plaintext
97 lines
2.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,csharp]
|
|
----
|
|
string color = "blue";
|
|
string name = "ishmael";
|
|
|
|
List<string> strings = new List<string>();
|
|
strings.Add(color);
|
|
strings.Add(name);
|
|
string[] stringArray = strings.ToArray();
|
|
|
|
if (strings.IndexOf(color) > 0) // Noncompliant
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (name.IndexOf("ish") > 0) // Noncompliant
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (name.IndexOf("ae") > 0) // Noncompliant
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (Array.IndexOf(stringArray, color) > 0) // Noncompliant
|
|
{
|
|
// ...
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp]
|
|
----
|
|
string color = "blue";
|
|
string name = "ishmael";
|
|
|
|
List<string> strings = new List<string> ();
|
|
strings.Add(color);
|
|
strings.Add(name);
|
|
string[] stringArray = strings.ToArray();
|
|
|
|
if (strings.IndexOf(color) > -1)
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (name.IndexOf("ish") >= 0)
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (name.Contains("ae"))
|
|
{
|
|
// ...
|
|
}
|
|
|
|
if (Array.IndexOf(stringArray, color) >= 0)
|
|
{
|
|
// ...
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 26 Jun 2015, 13:45:07 Tamas Vajk wrote:
|
|
\[~ann.campbell.2] I've added an additional C# specific option: ``++Array.IndexOf++``
|
|
|
|
=== on 26 Jun 2015, 15:29:07 Ann Campbell wrote:
|
|
looks good [~tamas.vajk]
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|