All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S4158.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

When a collection is empty, iterating it has no effect. Doing so anyway is likely a bug; either population was accidentally omitted, or the iteration needs to be revised.

How to fix it

Code examples

Noncompliant code example

public void Method()
{
    var values = new List<string>();
    values.Remove("bar");              // Noncompliant
    if (values.Contains("foo")) { }    // Noncompliant
    foreach (var str in values) { }    // Noncompliant
}

Compliant solution

public void Method()
{
    var values = LoadValues();
    values.Remove("bar");
    if (values.Contains("foo")) { }
    foreach (var str in values) { }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy