S1854 C# false positive with value only used in an exception handler

SonarLint C# 8.16.0.25740

In the class method Foo:

        void Bar() => throw new InvalidOperationException();

        public void Foo()
        {
            string step = "one";
            int x = 1;
            try
            {
                Bar();
                step = "two";
                x = 2;
                Bar();
            }
            catch (Exception)
            {
                Console.WriteLine($"Failed in step {step} {x}");
            }
        }

A false positive S1854 occurs on the initialization of β€œstep”. Curiously, there is no false positive on the initialization of β€œx”.

1 Like

Hi @paulhickman

thanks for your feedback. This seems to have the same root cause as https://github.com/SonarSource/sonar-dotnet/issues/2600.

Best,
Costin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.