Skip to content

Update the implementation of ClassOrdering to handle false negatives #3810

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 3 commits into from
Jun 13, 2021

Conversation

chao2zhang
Copy link
Member

Resolves #3809.

With this new implementation, one noticeable changes are that the number of violations is going to be increased:
For example, in a class layout like

class MyClass {
    val a: Int
    fun b(): String
    val c: Int
    val d: Int
    val e: Int
    val f: Int
}

Previous the violation will be reported only the boundary (a <-> b, b <-> c), now it will be reported on (c, d, e, f).
If the increase in the violations is a concern, we can implement grouping logic to reduce the number of violations.

@codecov
Copy link

codecov bot commented May 21, 2021

Codecov Report

Merging #3810 (0feafb3) into main (df4d921) will decrease coverage by 0.10%.
The diff coverage is 80.55%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3810      +/-   ##
============================================
- Coverage     83.48%   83.38%   -0.11%     
+ Complexity     3104     3102       -2     
============================================
  Files           456      456              
  Lines          8937     8943       +6     
  Branches       1751     1755       +4     
============================================
- Hits           7461     7457       -4     
- Misses          558      568      +10     
  Partials        918      918              
Impacted Files Coverage Δ Complexity Δ
...lab/arturbosch/detekt/rules/style/ClassOrdering.kt 81.81% <80.55%> (-10.29%) 6.00 <0.00> (-1.00)
...ain/kotlin/io/gitlab/arturbosch/detekt/api/Rule.kt 65.21% <0.00%> (-21.74%) 16.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 df4d921...0feafb3. Read the comment docs.

@chao2zhang chao2zhang added this to the 1.18.0 milestone May 21, 2021
}

@JvmInline
@Suppress("MagicNumber", "ModifierOrder") // TODO Remove ModifierOrder once value class is supported.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is pending #3719

@BraisGabin
Copy link
Member

Time ago I did some research regarging this rule and the number of issue that it raises. I found that we could use "longest increasing subsequence (LIS)" to reduce the number of issues. But I never implement it. I don't know even if we need to implement it. But as you talk about it I thought it could be usefull.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

this is KtSecondaryConstructor -> Section(1)
this is KtNamedFunction -> Section(2)
this is KtObjectDeclaration && isCompanion() -> Section(3)
else -> null
Copy link
Member

Choose a reason for hiding this comment

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

Is there any alternative to the null return? Then we could spare the null checks above?

Copy link
Member Author

Choose a reason for hiding this comment

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

Kotlin documentation does not specify where we should put nested classes or objects, so the ordering for nested classes shouldn't matter from a static analysis point of view, hence it should not have any priority order - null is a reasonable choice.

@chao2zhang chao2zhang merged commit 96511d1 into detekt:main Jun 13, 2021
@chao2zhang chao2zhang deleted the classordering branch June 13, 2021 22:18
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 negative for ClassOrdering
4 participants