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

Use property instead of count method when available #2736

Conversation

paulomorgado
Copy link
Contributor

Analyzer/fixer for usage of Enumerable.Count() on types that properties with the same semantics (Length, Count).

@sharwell
Copy link
Member

sharwell commented Aug 1, 2019

📝 The review here will be simpler once #2735 is merged.

@mavasani
Copy link
Member

mavasani commented Aug 1, 2019

The review here will be simpler once #2735 is merged.

Done. @paulomorgado - can you please merge latest?

@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch from 2652dbc to 7ef6036 Compare August 1, 2019 22:32
@paulomorgado
Copy link
Contributor Author

I think the analyzer is not quite right.

Should we explicitly go just for arrays, ImmutableArray and IColection<T> (and interfaces that extend it)?

OR should we go for any type that implicitly implements ICollection<T> and any type that has or implements an interface that has either a Length or Count property?

@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch 2 times, most recently from a36b41e to 97c14d0 Compare August 10, 2019 16:10
@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch from 2c4440c to f3be974 Compare August 13, 2019 15:57
Copy link
Member

@mavasani mavasani left a 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!

@paulomorgado paulomorgado marked this pull request as ready for review August 19, 2019 20:48
@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch 2 times, most recently from 15e9559 to fe44f86 Compare August 20, 2019 07:09
{
}

protected override ITypeSymbol GetEnumerableCountInvocationTargetType(IInvocationOperation invocationOperation)
Copy link
Member

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 :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

Copy link
Member

@mavasani mavasani Sep 3, 2019

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:

  1. Base analyzer defines an abstract method IOperation GetReceiverForExtensionMethodInvocation(IInvocationOperation).
  2. 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.
  3. 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

@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch from fe44f86 to 7f79902 Compare August 21, 2019 08:15
@paulomorgado paulomorgado force-pushed the features/UsePropertyInsteadOfCountMethodWhenAvailable branch from 7f79902 to aa4f8da Compare August 28, 2019 23:17
Copy link
Member

@mavasani mavasani left a 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!

simplified detection of Enumerable.Count() invoction.
split VB test for clarity
@mavasani
Copy link
Member

@paulomorgado Can you please resolve the merge conflicts? Thanks!

@mavasani mavasani merged commit 6c0fad0 into dotnet:master Sep 12, 2019
@mavasani
Copy link
Member

@paulomorgado Can you please a docs bug for this rule at https://docs.microsoft.com/visualstudio/code-quality/performance-warnings?

@paulomorgado
Copy link
Contributor Author

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

Successfully merging this pull request may close these issues.

None yet

3 participants