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