-
Notifications
You must be signed in to change notification settings - Fork 469
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
Prefer readonlyspan properties to array fields #5548
base: main
Are you sure you want to change the base?
Prefer readonlyspan properties to array fields #5548
Conversation
- Fix crash for fields with initializer like `new byte[123];`
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.
Thanks, @NewellClark, left some suggestions/questions overall looks it is quite close to get merged.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Resx = Microsoft.NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources; |
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.
NIT: normally we do a static import
...e/Microsoft.NetCore.Analyzers/Runtime/PreferReadOnlySpanPropertiesOverReadOnlyArrayFields.cs
Outdated
Show resolved
Hide resolved
...e/Microsoft.NetCore.Analyzers/Runtime/PreferReadOnlySpanPropertiesOverReadOnlyArrayFields.cs
Outdated
Show resolved
Hide resolved
...e/Microsoft.NetCore.Analyzers/Runtime/PreferReadOnlySpanPropertiesOverReadOnlyArrayFields.cs
Outdated
Show resolved
Hide resolved
...e/Microsoft.NetCore.Analyzers/Runtime/PreferReadOnlySpanPropertiesOverReadOnlyArrayFields.cs
Outdated
Show resolved
Hide resolved
}; | ||
return test.RunAsync(); | ||
} | ||
|
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.
The overall test coverage looks great, only suggestions: please add a test with a comment
// reference to the array field) and then remove the first argument from the argument list. | ||
if (invocationSyntax.ArgumentList.Arguments.Count == invocation.Arguments.Length) | ||
{ | ||
var newArgumentList = invocationSyntax.ArgumentList.WithArguments(invocationSyntax.ArgumentList.Arguments.RemoveAt(0)); |
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.
What if the arguments doesn't match the parameters order?
@NewellClark I believe this analyzer should be written to only operate on |
var field = (IFieldSymbol)context.Symbol; | ||
if (field.IsStatic && field.IsReadOnly && field.Type is IArrayTypeSymbol arrayType && cache.IsSupportedArrayElementType(arrayType.ElementType)) | ||
{ | ||
cache.AddCandidate(field); |
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.
Only private fields should be candidates.
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.
Needs to be written as a SymbolStart/SymbolEnd analyzer.
Ping @NewellClark - are you able to do the rewrite as a SymbolStart/End analyzer? |
@NewellClark, are you still working on this, or should it be closed? |
@NewellClark are you still planning to work on this? |
The school year just ended for me, so I will be working on this ASAP. Sorry for the delay. |
@mavasani I will convert the analyzer to use a symbol start/end analyzer. |
7818fd2
to
5ad4dd9
Compare
- Set isReportedAtCompilationEnd to false
…roperties-to-array-fields
I've modified the analyzer to use SymbolStart/SymbolEnd as requested. I'd like to take a moment to apologize for taking so long to get this done. When I became swamped with school work this past year, I should have indicated that I wouldn't be able to work on it so somebody else could have picked it up. For that, I apologize, and I will make sure it doesn't happen going forward. I am interested in contributing to this repository in the future, and I will ensure to finish assignments I pick up quickly. |
No need to apologize! We appreciate your efforts. And school absolutely takes priority. |
Oh dear, looks like I forgot to do something. I remember there was some command I'm supposed to run on the command line before submitting, and I can't remember what it is and I'm unable to find it. All I remember is that when I forget to run it, all the CI tests fail. Edit: I found it. I forgot to run MSBUILD. |
After this goes through, I'm submitting an issue to add the msbuild /t:pack /v:m instruction to a salient place in contributor documentation, because it took me over an hour to find these instructions (I had to create a dummy branch and pull request to get them to show up in the pull-request comment box).
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #5548 +/- ##
========================================
Coverage 96.40% 96.40%
========================================
Files 1379 1383 +4
Lines 322253 322971 +718
Branches 10460 10494 +34
========================================
+ Hits 310657 311356 +699
- Misses 9103 9112 +9
- Partials 2493 2503 +10 |
@NewellClark found a false positive while testing in runtime repo, I believe it should no flag below case: public class Tests
{
private static readonly byte[] s_protocolMismatch13 = new byte[] { (byte)TlsContentType.Alert, 3, 4, 0, 2, 2, 70 };
private static readonly byte[] s_protocolMismatch12 = new byte[] { (byte)TlsContentType.Alert, 3, 3, 0, 2, 2, 70 };
private static byte[] CreateProtocolVersionAlert(SslProtocols version) =>
version switch
{
SslProtocols.Tls13 => s_protocolMismatch13,
SslProtocols.Tls12 => s_protocolMismatch12,
_ => Array.Empty<byte>(),
};
} |
private void OnSymbolStart(SymbolStartAnalysisContext context) | ||
{ | ||
// Bail if we're missing required symbols. | ||
if (!RequiredSymbols.TryGetRequiredSymbols(context.Compilation, out RequiredSymbols? symbols)) | ||
return; | ||
|
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.
I think this part should go to the CompilationStartAction as it will not be different for each Symbol.
private void OnSymbolStart(SymbolStartAnalysisContext context) | |
{ | |
// Bail if we're missing required symbols. | |
if (!RequiredSymbols.TryGetRequiredSymbols(context.Compilation, out RequiredSymbols? symbols)) | |
return; | |
private void OnSymbolStart(SymbolStartAnalysisContext context, RequiredSymbols symbols) | |
{ |
i.e public override void Initialize(AnalysisContext context)
would look like this:
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(context =>
{
// Bail if we're missing required symbols.
if (!RequiredSymbols.TryGetRequiredSymbols(context.Compilation, out RequiredSymbols? symbols))
return;
context.RegisterSymbolStartAction(context => OnSymbolStart(context, symbols), SymbolKind.NamedType);
});
}
Was about to file an issue for this. Is there anything that is blocking this PR? |
Fix dotnet/runtime#33780.
I've found and fixed all violations in dotnet/runtime (already merged) and dotnet/roslyn (PR).
I've gone ahead and implemented a fixer, however because of this issue the fixer will not show up in visual studio. The testing framework is still able to run the fixer, however, so I was able to test it.
Edit: I've implemented it as a SymbolStart/SymbolEnd analyzer as requested, so it will work normally.