Skip to content

Fix S2583 FP: Code wrongly considered unreachable #8264

Description

@drieseng

Description

S2583 is raised in a while(true) loop with nested conditions.

Repro steps

Compile and run the following code:

namespace SonarRepo
{
    using System;
    using System.Net;

    public static class Program
    {
        private static void Main()
        {
            Run();
        }

        public static void Run()
        {
            HttpStatusCode? statusCode = null;
            var contentLength = 0;

            while (true)
            {
                var response = "5";
                if (response is null)
                {
                    // server shut down socket
                    break;
                }

                if (statusCode is null)
                {
                    statusCode = HttpStatusCode.OK;
                    continue;
                }

                // continue on parsing message headers coming from the server
                if (response == "5" && contentLength == 0)
                {
                    contentLength = 7;
                    continue;
                }

                if (response.Length > 0)
                {
                    if (contentLength > 0)
                    {
                        Console.WriteLine("And you thought we were unreachable??");
                    }

                    break;
                }
            }
        }
    }
}

Expected behavior

S2583 is not reported, and the application writes the following to standard output:

And you thought we were unreachable??

Actual behavior

S2583 is reported, and the application writes the following to standard output:

And you thought we were unreachable??

Known workarounds

None.

Related information

  • SonarAnalyzer.CSharp version: 9.12.0.78982
  • Visual Studio version: 17.8.0 Preview 2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions