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

Extend SA1129 to cover parameterless struct constructors #3854

Closed
Arthri opened this issue May 29, 2024 · 1 comment
Closed

Extend SA1129 to cover parameterless struct constructors #3854

Arthri opened this issue May 29, 2024 · 1 comment

Comments

@Arthri
Copy link

Arthri commented May 29, 2024

C#10 introduced the ability to declare and use parameterless struct constructors, which means new Struct() and default(Struct) can be different. Consider the following code sample,

using System;

X.L(); // 12
X.K(); // 0

internal struct X
{
    public int I;

    public X()
    {
        I = 12;
    }

    public static void L()
    {
        Console.WriteLine(new X().I);
    }
    
    public static void K()
    {
        Console.WriteLine(default(X).I);
    }
}

I believe that SA1129 "A value type was constructed using the syntax new T()." should not be removed, rather, it should check whether the struct defines a parameterless constructor before raising any warnings

@bjornhellander
Copy link
Contributor

Duplicate of #3430

@Arthri Arthri closed this as completed May 30, 2024
@sharwell sharwell closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants