Skip to content

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

Merged
BraisGabin merged 3 commits into
detekt:mainfrom
Whathecode:bugfix-4918
Jul 6, 2022
Merged

Only recommend using index accessors for Java classes that are known collections#4994
BraisGabin merged 3 commits into
detekt:mainfrom
Whathecode:bugfix-4918

Conversation

@Whathecode

@Whathecode Whathecode commented Jun 24, 2022

Copy link
Copy Markdown
Contributor

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
Comment thread detekt-rules-style/src/test/resources/java/com/example/fromjava/ByteBuffer.java Outdated
Comment thread detekt-rules-style/src/test/resources/java/com/example/fromjava/ByteBuffer.java Outdated
@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

5 participants