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

False-positive for S1871:Two branches in a switch-case when one branches use different overloads of methods #5587

Closed
ikkentim opened this issue Apr 24, 2022 · 0 comments · Fixed by #5898
Assignees
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@ikkentim
Copy link

Description

False positive when two branches in a switch-case use different overloads of the same method.

Rule:
S1871:Two branches in a conditional structure should not have exactly the same implementation

Repro steps

Span<int> result;
switch (array)
{
    case int[] a:
        return new Span<int>(a, 0, length);
    case float[] a:
        result = new int[length];
        for (var i = 0; i < length; i++)
            result[i] = ValueConverter.ToInt32(a[i]);
        return result;
    case bool[] a: // S1871
        result = new int[length];
        for (var i = 0; i < length; i++)
            result[i] = ValueConverter.ToInt32(a[i]);
        return result;
    default:
        throw new InvalidOperationException("Unsupported array type");
}

// ...

public static class ValueConverter {
    public static int ToInt32(float f) => 0;
    public static int ToInt32(bool b) => 0;
}

Expected behavior

No S1871

Actual behavior

S1871

Known workarounds

Workaround: Use a different name for one of the parameters in the case (e.g. bool[] b)

Related information

  • .NET 6, SDK 6.0.202 x64, latest C# version
  • Visual Studio 2022
  • <PackageReference Include="SonarAnalyzer.CSharp" Version="8.37.0.45539">
@costin-zaharia-sonarsource costin-zaharia-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Jun 8, 2022
@github-actions github-actions bot added this to False Positive in Backlog Jun 8, 2022
costin-zaharia-sonarsource added a commit that referenced this issue Jun 8, 2022
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to In progress in Best Kanban Jul 20, 2022
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Jul 22, 2022
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Jul 25, 2022
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Jul 25, 2022
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Jul 25, 2022
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Jul 25, 2022
Backlog automation moved this from False Positive to Done Jul 26, 2022
Best Kanban automation moved this from Review in progress to Validate Peach Jul 26, 2022
@mary-georgiou-sonarsource mary-georgiou-sonarsource moved this from Validate Peach to Done in Best Kanban Jul 28, 2022
@costin-zaharia-sonarsource costin-zaharia-sonarsource added this to the 8.43 milestone Aug 3, 2022
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: VB.NET VB.NET rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Backlog
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants