sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Fix S1854 FP: Raises when variable is assigned in switch statement and not use in the first case

Open mary-georgiou-sonarsource opened this issue 1 year ago • 0 comments

Repro steps

  void Repro()
  {
      char ch;
            switch (ch = GetAChar())   // Noncompliant FP
            {
                case 'A':
                    break;
                case 'B':
                    Console.WriteLine(ch);
                    break;
                default:
                    Console.WriteLine("Something");
                    break;
            }

      switch (ch = GetAChar())   // Does not raise
      {
          case 'A':
              Console.WriteLine(ch);
              break;
          default:
              Console.WriteLine("Something");
              break;
      }
      char GetAChar() => 'c';
  }