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

Only recommend using index accessors for Java classes that are known collections #4994

Merged
merged 3 commits into from Jul 6, 2022

Conversation

Whathecode
Copy link
Contributor

@Whathecode Whathecode commented Jun 24, 2022

Closes #4918

The ExplicitCollectionElementAccessMethod rule caught cases in which a get/set call could be replaced with indexed accessors, but it is nonsensical to do so. This happens for Java-defined classes, which don't have the concept of operator functions. Yet, the FunctionDescriptor.isOperator call seems to default to true.

Rather than recommending to replace all such method calls, a default of not reporting them is now assumed, but we can include known Java-defined collection types for which indexed accessors are preferred. For now I just added the necessary types to make the existing tests pass. This is a likely candidate for a rule configuration option.

This also includes a more general solution to a previous bugfix involving getters/setters with type parameters (#4803). The bug was when type inference was impossible, detekt would still recommend replacing with an indexer. Now, an attempt is done to see whether type inference is possible, rather than never recommending replacement in the case of type parameters.

Related and likely obsolete: #3609

This also provides a less strict and universal implementation for the previous type parameters bugfix: detekt#4803

As commented in the code, it does this by doing a lazy attempt at whether type inference is possible. If needed, this can be extended on later.
@github-actions github-actions bot added the rules label Jun 24, 2022
@cortinico cortinico added this to the 1.21.0 milestone Jun 28, 2022
@BraisGabin BraisGabin merged commit 46aa36f into detekt:main Jul 6, 2022
@Whathecode Whathecode deleted the bugfix-4918 branch July 7, 2022 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive ExplicitCollectionElementAccessMethod on java.lang.reflect.Field.get
4 participants