Skip to content

Fix S4158/S2583 FP: When elements are added to a list in List.ForEach the CollectionEmpty constraint is not removed #9425

Open
@mary-georgiou-sonarsource

Description

FP reported in the community forum.

Repro steps

public class Repro
{
    public int Id { get; set; }
    public bool Flag { get; set; }
    
    public static void Test(List<Repro> aList)
    {
        List<int> collectionOfIds = new List<int>();
        aList.FindAll(x => x.Flag).ForEach(x => collectionOfIds.Add(x.Id));

        collectionOfIds.Clear();    // FP S4158
        if (collectionOfIds.Count > 0)  // FP S2583
        { }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Area: CFG/SECFG and SE related issues.Type: CFG/SE FPsRule IS triggered when it shouldn't be for CFG and SE rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions