-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add warning to suggest to use inline classes when a class has single property #716
Conversation
### What's done: * Logic made * Added warn tests
Codecov Report
@@ Coverage Diff @@
## master #716 +/- ##
============================================
- Coverage 80.15% 80.12% -0.03%
- Complexity 1922 1944 +22
============================================
Files 91 92 +1
Lines 4958 4982 +24
Branches 1584 1595 +11
============================================
+ Hits 3974 3992 +18
Misses 241 241
- Partials 743 749 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
### What's done: * Fixed bugs
### What's done: * Fixed bugs
### What's done: * Fixed bugs
### What's done: * Fixed bugs
### What's done: * Fixed bugs
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/InlineClassesWarnTest.kt
Outdated
Show resolved
Hide resolved
|
||
@Test | ||
@Tag(WarningNames.INLINE_CLASS_CAN_BE_USED) | ||
fun `should not trigger on class with internal constructor`() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure it's prohibited? IDEA doesn't seem to highlight it in red.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As IDEA highlighted, constructor should be public, otherwise class can't be inline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, do you try this code in diktat? I would expect results to be the same with same kotlin version. Which version of kotlin plugin do you have in idea? My is 203-1.4.30-RC-IJ6682.9 for idea 2020.3.2. Let's check if this code compiles with kotlin 1.4.21.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I have 1.4.21 and this code doesn't compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that is a bug in IDEA plugin. Maybe, it'll be allowed in 1.4.30, but let's allow only public for now.
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/InlineClassesRule.kt
Outdated
Show resolved
Hide resolved
### What's done: * Fixed bugs
### What's done: * Fixed bugs
} else if (classPsi.getProperties().isEmpty() && classPsi.hasExplicitPrimaryConstructor()) { | ||
return classPsi.primaryConstructorParameters.size == 1 && | ||
!classPsi.primaryConstructorParameters.first().node.hasChildOfType(VAR_KEYWORD) && | ||
classPsi.primaryConstructorModifierList == null | ||
classPsi.primaryConstructor?.visibilityModifierType()?.value ?: "public" == "public" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classPsi.primaryConstructor?.visibilityModifierType()?.value ?: "public" == "public" | |
classPsi.primaryConstructor?.visibilityModifierType()?.value?.let { it == "public" } ?: true |
Maybe, something like this?
### What's done: * Fixed bugs
### What's done: * Fixed bugs
# Conflicts: # diktat-rules/src/main/kotlin/generated/WarningNames.kt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go go go
### What's done: * Fixed bugs
### What's done: * Fixed bugs
### What's done: * Fixed bugs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgfm
@@ -357,6 +357,9 @@ fun main() { | |||
} | |||
``` | |||
|
|||
### <a name="r6.1.12"></a> 6.1.12 Prefer Inline classes when a class has a single property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example?
Bad example - Good example
and more info about inline classes
@@ -17,6 +17,7 @@ import kotlinx.serialization.encodeToString | |||
* Special test that checks that developer has not forgotten to add his warning to a diktat-analysis.yml | |||
* This file is needed to be in tact with latest changes in Warnings.kt | |||
*/ | |||
@Suppress("INLINE_CLASS_CAN_BE_USED") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be inline this Test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please look at comments
### What's done: * Fixed bugs
### What's done: * Fixed bugs
# Conflicts: # diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt
### What's done: * Fixed bugs
### What's done: * Fixed bugs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
What's done:
This pull request closes #698
Actions checklist
Fixme
Since it is still experimental feature it shouldn't be fixed