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

CA1861 reporting for inline arrays in constructors of static properties #7365

Open
johnthcall opened this issue Oct 26, 2023 · 5 comments · May be fixed by #7381
Open

CA1861 reporting for inline arrays in constructors of static properties #7365

johnthcall opened this issue Oct 26, 2023 · 5 comments · May be fixed by #7381

Comments

@johnthcall
Copy link

johnthcall commented Oct 26, 2023

Description

When setting a property outside of a constructor in a static properties setup it will report CA1861, if the same array is set via a constructor the analyzer will not report. It's possible that when the array is set via a constructor parameter an optimization is made by the compiler that it can't make by setting the property "after" the constructor.

Reproduction Steps

The following will report CA1861 on the array creation.

        public class TestClass
        {
            public List<IList<string>> ListEntry
            {
                get; set;
            }
        }

        public static TestClass TestClass1 = new TestClass { ListEntry = new List<IList<string>> { new[] { "test" } } };

If the array is a constructor parameter it will not report like in the following

public class TestClass
 {
  public TestClass(List<IList<string>> ListEntry) { }
}

public static TestClass TestClass1 = new TestClass(new List<IList<string>> { new[] { "test" } });

Expected behavior

As this is a static property and the category is performance I believe it should not fire as it is only allocating the array once.

Actual behavior

The analyzer should not detect an issue

Regression?

No response

Known Workarounds

Add the initialization to a constructor or invoke the auto refactor in VS to make a cosnt array.

Configuration

Windows 11 x64 on .Net 8 RC2 (8.0.0-rc.2.23479.6)

Other information

No response

@ghost
Copy link

ghost commented Oct 27, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

When setting a property outside of a constructor in a static properties setup it will report CA1861, if the same array is set via a constructor the analyzer will not report. It's possible that when the array is set via a constructor parameter an optimization is made by the compiler that it can't make by setting the property "after" the constructor.

Reproduction Steps

The following will report CA1861 on the array creation.

        public class TestClass
        {
            public List<IList<string>> ListEntry
            {
                get; set;
            }
        }

        public static TestClass TestClass1 = new TestClass { ListEntry = new List<IList<string>> { new[] { "test" } } };

If the array is a constructor parameter it will not report like in the following

public class TestClass
 {
  public TestClass(List<IList<string>> ListEntry) { }
}

public static TestClass TestClass1 = new TestClass(new List<IList<string>> { new[] { "test" } });

Expected behavior

As this is a static property and the category is performance I believe it should not fire as it is only allocating the array once.

Actual behavior

The analyzer should not detect an issue

Regression?

No response

Known Workarounds

Add the initialization to a constructor or invoke the auto refactor in VS to make a cosnt array.

Configuration

Windows 11 x64 on .Net 8 RC2 (8.0.0-rc.2.23479.6)

Other information

No response

Author: johnthcall
Assignees: -
Labels:

area-System.Runtime, untriaged, needs-area-label

Milestone: -

@steveberdy
Copy link
Contributor

Once this is approved, I'd be happy to attempt a fix

@steveberdy
Copy link
Contributor

steveberdy commented Jul 3, 2024

@johnthcall this issue most likely belongs in dotnet/roslyn-analyzers

@jeffhandley
Copy link
Member

@steveberdy If you're still interested in tackling this, I can assign it to you.

@steveberdy
Copy link
Contributor

@jeffhandley sure, I'll take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants