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

Fix S2583 FP: Wrong evaluation in switch-case assignment #8745

Closed
4865783a5d opened this issue Feb 12, 2024 · 1 comment
Closed

Fix S2583 FP: Wrong evaluation in switch-case assignment #8745

4865783a5d opened this issue Feb 12, 2024 · 1 comment

Comments

@4865783a5d
Copy link

4865783a5d commented Feb 12, 2024

Description

Please provide a succinct description of your issue. Also please provide the rule ID (e.g. S1234)

Repro steps

 private static void Foo()
 {
     string? one = null;
     string? two = null;
     var values = new List<string>
     {
         "one",
         "two"
     };

     foreach (var value in values)
     {
         switch (value)
         {
             case "one":
                 one = value;
                 break;
             case "two":
                 two = value;
                 break;
         }
     }

     if (string.IsNullOrEmpty(one))
     {
         Console.WriteLine("one");
         return;
     }

     // Change this condition so that it does not always evaluate to 'True'. Some code paths are unreachable.	
     if (string.IsNullOrEmpty(two))
     {
         Console.WriteLine("two");
     }
 }

Expected behavior

No false-positive as variable two can be null if it doesn't hit the switch case.

Actual behavior

A false-positive is reported.

Known workarounds

Instead of declaring the two variables as string?, use string.Empty.

 var one = string.Empty;
 var two = string.Empty;

Related information

SonarQube connected mode with VS2022

@antonioaversa antonioaversa changed the title Fix S2583 FP/FN: Wrong evaluation in switch-case assignment Fix S2583 FP: Wrong evaluation in switch-case assignment Feb 12, 2024
@Tim-Pohlmann
Copy link
Contributor

Hello @4865783a5d, I cannot reproduce the issue. Which version of the analyzer are you using?
Anyway, if you still have the issue, there is a good chance it is connected to #8474 which will be fixed in 9.20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants