SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
755 stars 223 forks source link

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

Open mary-georgiou-sonarsource opened 1 week ago

mary-georgiou-sonarsource commented 1 week ago

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';
  }