Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S3963: False-positive for initializing static field in static ctor that is present due to other reasons #9656

Open
witoldkaras opened this issue Aug 26, 2024 · 2 comments
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@witoldkaras
Copy link

Description

S3963 is fired when static field initialization is present in static constructor that contains also other code warranting its existence.
Inline initialization of those fields doesn't change anything regarding static constructor presence, thus making the change pointless in such case.

Repro steps

    internal static class S3963Test
    {
        private static readonly string Foo;
        private static readonly string Bar;
        private static readonly string Baz;

        static S3963Test()
        {
            (Foo, Bar) = GetFooBar();
            Baz = GetBaz();    // triggers S3963 despite static ctor being necessary anyways due to line above (which by itself doesn't trigger the rule)
        }

        private static (string, string) GetFooBar()
        {
            return ("Foo", "Bar");
        }

        private static string GetBaz()
        {
            return "Baz";
        }
    }

Expected behavior

Do not fire S3963 when static field initialization is not the only thing warranting existence of static ctor.

Actual behavior

S3963 is fired

Known workarounds

Suppress S3963.

Related information

SonarAnalyzer.CSharp 9.32.0.97167

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Aug 26, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

Thank you for reporting the issue. Confirmed as False Positive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

4 participants
@zsolt-kolbay-sonarsource @witoldkaras and others