Only recommend using index accessors for Java classes that are known collections#4994
Merged
Conversation
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.
BraisGabin
reviewed
Jun 25, 2022
TWiStErRob
reviewed
Jun 25, 2022
BraisGabin
approved these changes
Jun 28, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4918
The
ExplicitCollectionElementAccessMethodrule 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, theFunctionDescriptor.isOperatorcall seems to default totrue.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