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 using delegates #4261

Closed
markusschaber opened this issue Apr 19, 2021 · 1 comment · Fixed by #7427
Closed

S4158 FP: Collection filled using delegates #4261

markusschaber opened this issue Apr 19, 2021 · 1 comment · Fixed by #7427
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@markusschaber
Copy link

Description

In one of our unit tests, S4158 triggers false positives, the collection clearly is not empty when S4158 claims it is.

Repro steps

This is a cut down snippet from one of our unit tests, requires FluentAssertions (or replace with your favorite assertion syntax).

namespace StaticAnalyzerS4158Bug
{
    using System;
    using System.Collections.Generic;
    using FluentAssertions;

    static class Program
    {
        static void Main(string[] args)
        {
            var collector = new List<int>();

            DoSomething(collector.Add);

            collector[0].Should().Be(42); // Triggers false positives on S4158.
            collector[1].Should().Be(23); // Triggers false positives on S4158.
        }

        private static void DoSomething(Action<int> callback)
        {
            callback(42);
            callback(23);
        }
    }
}

Expected behavior

The rule does not fire when delegates of any methods which can add members have been passed as callback somewhere.

Actual behavior

The rule fires

Known workarounds

Wrap the delegate in a lambda:

            Testee.SubscribeAnalyzerValues(tenant, plcId, i => collector.Add(i));

Related information

  • C#/VB.NET Plugins version: SonarLint for Visual Studio 4.33.1.28913
  • Visual Studio version: 2019
  • MSBuild / dotnet version: .NET Core 3.1
  • Operating System: Windows 10
costin-zaharia-sonarsource added a commit that referenced this issue Apr 22, 2021
@costin-zaharia-sonarsource
Copy link
Member

Hi @markusschaber,

thanks for your feedback. I can confirm the false positive.

@costin-zaharia-sonarsource costin-zaharia-sonarsource added Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Apr 22, 2021
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title False positive on S4158 when using delegates Fix S4158 FP: Collection filled using delegates May 28, 2021
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 25, 2021
@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title Fix S4158 FP: Collection filled using delegates S4158 FP: Collection filled using delegates 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. 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.

5 participants