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

S2589 FP: Rule is raised on catch-all tuples in switch expressions #8008

Closed
rjgotten opened this issue Sep 14, 2023 · 2 comments · Fixed by #8485
Closed

S2589 FP: Rule is raised on catch-all tuples in switch expressions #8008

rjgotten opened this issue Sep 14, 2023 · 2 comments · Fixed by #8485
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

@rjgotten
Copy link

Description

S2589 is raised on catch-all tuples in switch expressions.
This is a common pattern when using truth-table like exhaustive switch expressions when aligning tuple members for a consistent condition style. (See the repro steps.)

Repro steps

class Person
{
  public int? Age { get; set; }
}

// ...

public int Compare(Person x, Person y) => (x.Age, y.Age) switch
{
  (null, null) =>  0,
  (null,    _) =>  1,
  (   _, null) => -1,
  (   _,    _) => Comparer<int>.Default.Compare(x.Age.Value, y.Age.Value)
};

Expected behavior

S2589 is not raised.

Actual behavior

S2589 is raised on the (_, _) case.

Known workarounds

Use a plain catch-all _ case instead - but this breaks the stylistic similarity and alignment of all the switch expression cases.

Related information

  • C#/VB.NET Plugins version: 9.8 (via SonarLint v7.3.0.77872)
  • Visual Studio version: VS2022 v17.7.4
  • MSBuild / dotnet version: .NET SDK 7.0.401
  • SonarScanner for .NET version (if used): N/A
  • Operating System: Windows 11
@gregory-paidis-sonarsource
Copy link
Contributor

Hey @rjgotten ,

Thanks for the information.
I added a reproducer you can find here: #8014

I also added to our dedicated backlog, where it will be taken care of sometimes in the future.

@gregory-paidis-sonarsource gregory-paidis-sonarsource added Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. Area: CFG/SE CFG and SE related issues. labels Sep 14, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource removed their assignment Sep 14, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource removed the Area: C# C# rules related issues. label Sep 14, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource changed the title Fix S2589 FP: Rule is raised on catch-all tuples in switch expressions S2589 FP: Rule is raised on catch-all tuples in switch expressions Sep 14, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

Another report related to the tuples: https://community.sonarsource.com/t/switch-expression-s2589-false-postive-or-documentation-gap/99560

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.

6 participants