8000 valueOrDefaultCommaSeparated throws a ClassCastException by BraisGabin · Pull Request #2566 · detekt/detekt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

valueOrDefaultCommaSeparated throws a ClassCastException #2566

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

Merged
merged 2 commits into from
Apr 1, 2020
Merged

valueOrDefaultCommaSeparated throws a ClassCastException #2566

merged 2 commits into from
Apr 1, 2020

Conversation

BraisGabin
Copy link
Member
@BraisGabin BraisGabin commented Mar 31, 2020

Fix #2561

Ok bear with me, this is complex. This is a bug added in #2474. The reason of that PR is described at #2463.

When ForbiddenImport executes the extension function ConfigAware.valueOrDefaultCommaSeparated the actual implementation that we are using is DisabledAutoCorrectConfig. But DisabledAutoCorrectConfig does nearly nothing here. It just delegate the work to CompositeConfig.

This is the code that is executed there:
https://github.com/arturbosch/detekt/blob/91fdc64fecb858045059bf544fa9bfb6017c8c1d/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/CompositeConfig.kt#L15-L16

The problem is that lookFirst.valueOrNull(key) returns a String but T is a List<String>. So we and with a function that should return a List returning "". This is not flagged by the compiler because we are doing a lot of casting in this part of the code.

The type is checked in the line 46 after the function is called
https://github.com/arturbosch/detekt/blob/91fdc64fecb858045059bf544fa9bfb6017c8c1d/detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/Junk.kt#L40-L52

and, of course, it crash with a ClassCastException.

For this reason it's safe to catch the ClassCastException too and try again as String. Does it have sense?

Tests pending... I'm figuring out how to test this...

Copy link
Member
@arturbosch arturbosch left a comment

Choose a reason for hiding this comment

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

Yep, this is basically the fault of valueOrNull as it can't verify/evaluate the actual type.
This can happen everywhere we use valueOrNull.
A pity that interfaces can't have inline functions.

@arturbosch arturbosch added this to the 1.8.0 milestone Mar 31, 2020
@arturbosch arturbosch merged commit adff475 into detekt:master Apr 1, 2020
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.

1.6.0 -> 1.7.2: java.lang.String cannot be cast to java.util.List
3 participants
0