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

Fix UseDataClass to accept classes that implement interfaces #2905

Merged
merged 5 commits into from
Jul 30, 2020

Conversation

zsmoore
Copy link
Contributor

@zsmoore zsmoore commented Jul 29, 2020

Addresses issue #2904

Root cause is that our doesNotExtendAnything() considers interfaces.

There is not a very easy way to fix this, the fix I have done to detect interfaces is

  • Look at the UserType
  • Go to the containing KT File
  • Find the declaration with the referenced name
  • See if that declaration is a KTClass and an interface

From what I searched PSI does not give us many tools to get class from superTypeEntry

@codecov
Copy link

codecov bot commented Jul 29, 2020

Codecov Report

Merging #2905 into master will decrease coverage by 0.02%.
The diff coverage is 50.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2905      +/-   ##
============================================
- Coverage     80.18%   80.15%   -0.03%     
- Complexity     2426     2429       +3     
============================================
  Files           421      421              
  Lines          7378     7384       +6     
  Branches       1350     1354       +4     
============================================
+ Hits           5916     5919       +3     
- Misses          762      764       +2     
- Partials        700      701       +1     
Impacted Files Coverage Δ Complexity Δ
...turbosch/detekt/rules/KtClassOrObjectExtensions.kt 40.00% <33.33%> (-26.67%) 0.00 <0.00> (ø)
...tlab/arturbosch/detekt/rules/style/UseDataClass.kt 76.36% <100.00%> (+1.81%) 38.00 <0.00> (+2.00)
...lab/arturbosch/detekt/core/FileProcessorLocator.kt 69.23% <0.00%> (-10.77%) 5.00% <0.00%> (ø%)
...ab/arturbosch/detekt/cli/DetektProgressListener.kt 83.33% <0.00%> (+3.33%) 4.00% <0.00%> (+1.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c457917...ea5136c. Read the comment docs.

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

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

Looks good to me. I've left a minor comment on the tests. If you could add a couple more it would be better 👍

assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does report a candidate class with an interface extension that overrides vals") {
Copy link
Member

Choose a reason for hiding this comment

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

Can I ask you to please write two other tests:

interface SimpleInterface {
    val i: Int
}

data class DataClass(override val i: Int): SimpleInterface

This should not report anything as you're correctly already using a data class.
And this other test:

interface SimpleInterface {
    val i: Int
}

open class BaseClass(open val j: Int)

class DataClass(override val i: Int, override val j: Int): SimpleInterface, BaseClass(j)

This should also report nothing as you're extending from another class (and an interface).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional tests and looks like they pass. Great test suggestions thanks!

@zsmoore
Copy link
Contributor Author

zsmoore commented Jul 29, 2020

Also as a note added myself to the readme for this one +
#2258
#2262
#2263

@arturbosch arturbosch merged commit 539a010 into detekt:master Jul 30, 2020
@arturbosch arturbosch added this to the 1.10.1 milestone Jul 30, 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.

3 participants