SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
770 stars 226 forks source link

S4158 FP: Collection filled in a local function #4478

Open rent-a-developer opened 3 years ago

rent-a-developer commented 3 years ago

Description

A S4158 violation is incorrectly reported when elements to a collection are added in a local function.

Repro steps

public static class Program
{
    public static void Main()
    {
        var list = new List<String>();

        void AddElement()
        {
            list.Add("Item1");
        }

        AddElement();

        for (int i = 0; i < list.Count; i++)
        {
            var element = list[i]; // SonarLint reports violation of rule S4158 here.
            Console.Out.WriteLine(element);
        }
    }
}

Expected behavior

Since the collection is clearly not empty in the code sample, SonarLint should not report a violation of rule S4158.

Actual behavior

SonarLint reports a violation of rule S4158.

Known workarounds

None.

Related information

pavel-mikula-sonarsource commented 3 years ago

Hi @rent-a-developer ,

Thank you for reporting this, I can confirm it as False Positive, as our engine doesn't support local functions for this rule (yet).

Keke71 commented 1 year ago

Hi, Not sure if I should post it here, or create a new issue, as my false positive is not related to local functions. However, it looks quite similar. The following code (a little bit simplified from my original code) also reports a false positive S4158, although the collections are definitely not empty:

Environment:

pavel-mikula-sonarsource commented 1 year ago

Hi @Keke71,

Thank you for reporting it, this is a good place to gather this evidence. This rule will get migrated to the new engine under MMF-2402 (non-public link). After that, we should be able to add support for local functions.

Keke71 commented 1 year ago

Hi @pavel-mikula-sonarsource,

Thanks for your quick answer. However, please note that there are no local functions involved in my example. Maybe it is related though and will be fixed together with the local functions problem.

Best regards

pavel-mikula-sonarsource commented 1 year ago

@Keke71 indeed, I'm sorry, I've missread the "not" in "not related".

I've taken a closer look on the flow, it's quite complicated in terms of tracking values. There's a chance it will be fixed by the migration I mentioned (MMF-2402).

Please feel free to submit a new issue, as yours is related to tracking state in for loops rather than local function.

Keke71 commented 1 year ago

I created a new issue here

pavel-mikula-sonarsource commented 1 year ago

We cannot fix the original issue yet, as the new SE doesn't support local functions yet.

At the same time, the rule is migrated to the new SE, so the actual local function support is the only thing needed to fix this.

andrei-epure-sonarsource commented 5 months ago

@pavel-mikula-sonarsource , according to MMF-2228, we should support local functions.

Is this still relevant, or was it fixed?

pavel-mikula-sonarsource commented 5 months ago

Yes, this is still relevant. MMF-2228 analyzes local function in a stand-alone mode. This issue requires us to do a light version of cross-procedure analysis because the outer method has to analyze the inner local function on the way. It's the size of an MMF and should be tackled before our actual cross-procedure capabilities - the solution for this will be unrolling the content of the local function.