-
Notifications
You must be signed in to change notification settings - Fork 468
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 Avoid constant arrays as arguments #5383
Conversation
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs
Outdated
Show resolved
Hide resolved
@Youssef1313 I finished work on all feedback, and even included more tests to cover various scenarios. Do you have any more feedback for me? Is this mergeable? |
@steveberdy You will need to run
We will then need to wait for a review from someone at MS. Thanks for the hardwork in the meantime :) |
I went ahead and ran |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Show resolved
Hide resolved
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #5383 +/- ##
==========================================
- Coverage 96.42% 96.40% -0.02%
==========================================
Files 1372 1377 +5
Lines 319881 321935 +2054
Branches 10271 10434 +163
==========================================
+ Hits 308438 310364 +1926
- Misses 8985 9082 +97
- Partials 2458 2489 +31 |
@stephentoub Is this ready for merging? |
I haven't reviewed it (other than glancing at it earlier to check the span thing I commented on). I expect one or more folks from @jeffhandley's team need to. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs
Outdated
Show resolved
Hide resolved
@steveberdy please retarget to release/7.0.1xx branch, release |
@buyaa-n thank you so much for your patience regarding this PR. Could I get a review for this? |
Sorry for the delay, I thought you fixed the fixer issue and tested it with runtime repo again, the fixer still throws when try to apply the codefix in VS:
I will try to look later this week |
I have reproduced that error. Looking into it now. |
The fixer works for bulk code fixes, but not for individual ones. I'll debug this, but if you have any ideas please let me know. |
I've identified the error and I've spent many hours trying to remedy it, but to no avail. The problem is that the roslyn-analyzers/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.Fixer.cs Lines 94 to 102 in fec9a2f
As I've said, I've spent hours debugging and trying other potential ways to insert the node without getting this error, but I can't get much success. Can any of you provide better insight? Is there something I'm overlooking? Thanks in advance. |
Thank you for your effort and sorry for delayed response @steveberdy, it sounded like some kind of race condition happening with editor while inserting/replacing the nodes. So, I moved the editor creation: roslyn-analyzers/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.Fixer.cs Line 40 in fec9a2f
into the ExtractConstArrayAsync and the issue is fixed.
After the fixer issue is solved, further I evaluated the findings and found following issues:
public class A
{
public static readonly A Connection = new A(new string[] { "close" }); // Exclude array initializations in readonly fields/properties
private static readonly HashSet<string> s_acceptedDays = new HashSet<string>(
new string[] { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" },
StringComparer.OrdinalIgnoreCase
);
public static List<string> Property { get; }
public A(string[] arr) { }
static A() // Exclude array initializations in static constructors
{
Property = GetValues(new string[] { "close" });
}
private static List<string> GetValues(string[] arr) => null;
}
The code snippet looks like this: if (!de.Properties.Contains("nTSecurityDescriptor"))
de.RefreshCache(new string[] { "nTSecurityDescriptor" }); |
Thanks @steveberdy I pushed a fix for this fixer issue that happening runtime and the issue that causing Now the analyzer is ready for the final review. @mavasani @Youssef1313 @Evangelink @stephentoub PTAL! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
CA1861: Avoid constant arrays as arguments
This analyzer is for performance improvements requested in dotnet/runtime#33794. The purpose of the analyzer and code fix is to extract constant arrays, that are passed on method invocation, as
static readonly
fields.Documentation: dotnet/docs#25659
Fixes dotnet/runtime#33794