-
Notifications
You must be signed in to change notification settings - Fork 467
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
Use property instead of count method when available #2736
Use property instead of count method when available #2736
Conversation
📝 The review here will be simpler once #2735 is merged. |
Done. @paulomorgado - can you please merge latest? |
src/Microsoft.NetCore.Analyzers/Core/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/Microsoft.NetCore.Analyzers/Core/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
2652dbc
to
7ef6036
Compare
I think the analyzer is not quite right. Should we explicitly go just for arrays, OR should we go for any type that implicitly implements |
a36b41e
to
97c14d0
Compare
2c4440c
to
f3be974
Compare
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, this implementation looks lot more cleaner!
src/Microsoft.NetCore.Analyzers/Core/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Outdated
Show resolved
Hide resolved
15e9559
to
fe44f86
Compare
{ | ||
} | ||
|
||
protected override ITypeSymbol GetEnumerableCountInvocationTargetType(IInvocationOperation invocationOperation) |
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 presume the language specific code is needed due to dotnet/roslyn#23625 :(
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.
Yes!
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 the logic here can be significantly simplified as follows:
- Base analyzer defines an abstract method
IOperation GetReceiverForExtensionMethodInvocation(IInvocationOperation)
. - All the logic that you have in
GetEnumerableCountInvocationTargetType
overrides can be moved to the base type which uses the above helper to just find the IOperation on which to operate. - C# and VB specific implementations should ideally then be just a single method override with a trivial one line implementation to address the core difference from Difference in Operation tree shape between VB and C# for invocations to extension methods roslyn#23625
fe44f86
to
7f79902
Compare
src/Microsoft.NetCore.Analyzers/CSharp/Performance/CSharpDoNotUseCountWhenAnyCanBeUsed.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.NetCore.Analyzers/Core/Performance/DoNotUseCountWhenAnyCanBeUsed.cs
Outdated
Show resolved
Hide resolved
…of the target type with the same semantics already exists.
added diagnostic property key constant. split analyzers int language-specific analyzers.
7f79902
to
aa4f8da
Compare
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Show resolved
Hide resolved
...Analyzers/VisualBasic/Performance/BasicUsePropertyInsteadOfCountMethodWhenAvailable.Fixer.vb
Show resolved
Hide resolved
...NetCore.Analyzers/UnitTests/Performance/UsePropertyInsteadOfCountMethodWhenAvailableTests.cs
Show resolved
Hide resolved
...NetCore.Analyzers/UnitTests/Performance/UsePropertyInsteadOfCountMethodWhenAvailableTests.cs
Show resolved
Hide resolved
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.
Overall, looking good to me. I have couple of suggestions: 1. simplifying the language specific code in the analyzers and 2. simplifying and loosening the GetReplacementProperty
implementation.
Thanks for the exhaustive unit tests!
...Microsoft.NetCore.Analyzers/Core/Performance/UsePropertyInsteadOfCountMethodWhenAvailable.cs
Outdated
Show resolved
Hide resolved
simplified detection of Enumerable.Count() invoction. split VB test for clarity
…rtyInsteadOfCountMethodWhenAvailable
@paulomorgado Can you please resolve the merge conflicts? Thanks! |
…rtyInsteadOfCountMethodWhenAvailable
@paulomorgado Can you please a docs bug for this rule at https://docs.microsoft.com/visualstudio/code-quality/performance-warnings? |
Analyzer/fixer for usage of
Enumerable.Count()
on types that properties with the same semantics (Length
,Count
).