Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S4158 FP: Collection filled in a for loop reported to be empty #6179

Closed
Keke71 opened this issue Oct 7, 2022 · 1 comment · Fixed by #7447
Closed

S4158 FP: Collection filled in a for loop reported to be empty #6179

Keke71 opened this issue Oct 7, 2022 · 1 comment · Fixed by #7447
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@Keke71
Copy link

Keke71 commented Oct 7, 2022

Description

Sonar Analyzer incorrectly reports S4158 for the code below. My original code is actually more complex. However, this simplified example also reports S4158.

Repro steps

public void FalsePositive()
{
    var data = new[] { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0 };
    var rising = new List<List<int>>();
    List<int> currentBin = null;
    for (var i = 0; i < data.Length; i++)
    {
        if (data[i] == 0)
        {
            if (currentBin != null)
            {
                rising.Add(currentBin);
                currentBin = null;
            }
    
            continue;
        }
    
        currentBin ??= new List<int>();
        currentBin.Add(i);
    }
    
    for (var i = 0; i < rising.Count; i++)
    {
        // SonarAnalyzer reports S4158 here:
        Console.WriteLine($"{rising[i].Count}");
    }
}

Expected behavior

As the collection is not empty, no issue should be reported.

Actual behavior

S4158 is reported.

Known workarounds

Using a foreach loop in the lower part, does not report the issue. However, in my original code this is not possible, as I actually have two collections that I have to access with indices.

Related information

Environment:
- Visual Studio Enterprise 17.3.0
- SonarAnalyzer.CSharp 8.46.0.54807
- .NET Framework 4.8
- Windows 10 x64

@pavel-mikula-sonarsource
Copy link
Contributor

Hi @Keke71,

Thank you for reporting this, as discussed on the other thread, I confirm it as a False Positive.

For archeology: MMF-2402 (nonpublic link) will migrate this rule to the new engine

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Oct 7, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S4158: Collection filled in a for loop reported to be empty Fix S4158 FP: Collection filled in a for loop reported to be empty Oct 7, 2022
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title Fix S4158 FP: Collection filled in a for loop reported to be empty S4158 FP: Collection filled in a for loop reported to be empty Oct 14, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 9.3 milestone May 30, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource modified the milestones: 9.3, 9.4 Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants