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 S2259: FP on switch statements #2338

Closed
Corniel opened this issue Mar 12, 2019 · 2 comments · Fixed by #2515
Closed

Fix S2259: FP on switch statements #2338

Corniel opened this issue Mar 12, 2019 · 2 comments · Fixed by #2515
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@Corniel
Copy link
Contributor

Corniel commented Mar 12, 2019

Description

The rule does not recognize which cases of a switch statement are safe (all except the one that contains the null case).

Repro steps

using System;

namespace SonarAnalyzer.Experiments.CSharp
{
    public class S2259
    {
        public ConsoleColor Color { get; set; }

        public void Method1(S2259 obj)
        {
            switch (obj?.Color)
            {
                case null:
                    Console.ForegroundColor = obj.Color; //not compliant
                    break;
                case ConsoleColor.Red:
                    Console.ForegroundColor = obj.Color; //compliant
                    break;
                default:
                    Console.WriteLine($"Color {obj.Color} is not supported."); //compliant
                    break;
            }
        }

        public void Method2(S2259 obj)
        {
            switch (obj?.Color)
            {
                case ConsoleColor.Red:
                    Console.ForegroundColor = obj.Color; //compliant
                    break;
                default:
                    Console.WriteLine($"Color {obj.Color} is not supported."); //not compliant
                    break;
            }
        }
    }
}

Expected Result

Only the null cases should report S2259.

Related information

  • SonarC# Version 7.11.0.8083
@Evangelink Evangelink added Area: Rules Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Mar 27, 2019
@Evangelink Evangelink added this to the CFG + Symbolic Execution milestone Mar 27, 2019
@andrei-epure-sonarsource
Copy link
Contributor

Another example from the community

This raises S2259 - "myObject is null on at least one execution path" in the default case (for the myObject.EnumValue dereferencing). But if myObject was null, then myObject?.EnumValue would also be null and the switch would hit the null case, throwing an exception. So default is unreachable if myObject is null.

void Foo()
{
    var myObject = GetObjectFromOutside();

    switch (myObject?.EnumValue)
    {
        case MyEnum.One:
            throw new MyException("Invalid value.");
        case MyEnum.Two:
        case null:
            throw new MyException("Invalid or null value.");
        case MyEnum.Three:
            break;
        default:
            throw new ArgumentOutOfRangeException(
                nameof(myObject.EnumValue),
                $"Unexpected value ${myObject.EnumValue}");
    }
}

@andrei-epure-sonarsource andrei-epure-sonarsource modified the milestones: CFG + Symbolic Execution, 7.16 Jul 22, 2019
@andrei-epure-sonarsource
Copy link
Contributor

andrei-epure-sonarsource commented Jul 22, 2019

This may be related to #1183

@AbbasNS AbbasNS moved this from To do to In progress in Best Kanban Jul 25, 2019
@AbbasNS AbbasNS self-assigned this Jul 25, 2019
@AbbasNS AbbasNS moved this from In progress to Review in progress in Best Kanban Jul 29, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource moved this from Review in progress to In progress in Best Kanban Jul 30, 2019
@AbbasNS AbbasNS moved this from In progress to Review in progress in Best Kanban Jul 30, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource moved this from Review in progress to Reviewer approved in Best Kanban Jul 30, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource removed their assignment Jul 30, 2019
Best Kanban automation moved this from Reviewer approved to Done Jul 30, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title Rule S2259: False positive on switch statements Fix S2259: FP on switch statements Aug 12, 2019
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. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants