-
-
Notifications
You must be signed in to change notification settings - Fork 767
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
Added rule CanBeNonNullable #4379
Added rule CanBeNonNullable #4379
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4379 +/- ##
============================================
- Coverage 84.30% 84.26% -0.05%
+ Complexity 3273 3272 -1
============================================
Files 473 474 +1
Lines 10340 10408 +68
Branches 1830 1857 +27
============================================
+ Hits 8717 8770 +53
- Misses 667 670 +3
- Partials 956 968 +12
Continue to review full report at Codecov.
|
@BraisGabin The latest Detekt run on the branch had
The problem is that Kotlin determines that |
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.
I'd say to have CanBeNonNullableProperty disqualify any property that derives its value from Java source code
+1 on this
...es-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableProperty.kt
Outdated
Show resolved
Hide resolved
...es-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableProperty.kt
Outdated
Show resolved
Hide resolved
Debt.TEN_MINS | ||
) | ||
|
||
override fun visitKtFile(file: KtFile) { |
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.
You're missing a super.visitKtFile(file)
call here
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.
Why would this be necessary? The NonNullableCheckVisitor
instance conducts the actual check of the file for the rule, not CanBeNonNullableProperty
itself.
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.
Why would this be necessary?
That's part of the Rule
contract and in general follows the visitor design pattern implementation. If by any chance we decide to implement a new feature on detekt that relies on the visitKtFile
and lives inside the Rule
top level class (or any of its ancestors), this rule will break this behavior.
CodeSmell( | ||
issue, | ||
Entity.from(property), | ||
"A nullable property can be made non-nullable." |
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.
nit: You could improve this to mention the name of the property as you have it
} | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLintWithContext(env, code)).hasSize(5) |
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.
Can you split into smaller tests?
} | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLintWithContext(env, code)).hasSize(5) |
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.
Same as above
…s; Reorganized tests and split up basic var/val evaluation tests
@t-kameyama implemented the option to do that in #4203. I think that you can use the same pattern. |
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.
Run the tests with -Pcompile-test-snippets=true
in the gradle command. That will make the tests run slower but it will ensure that your snippet codes compile (It is necessary to merge the PR)
This rule is really good! But I'm wondering if this same rule should check for the same pattern in local variables or should we use other rule. I think that this rule is ready to merge now but, if we want to extend it later with the local variables we should change its name before merging it. I mean, it's not needed to implement the local variable thing now but if we want both things in the same ruel it can't be called "property".
config/detekt/detekt.yml
Outdated
@@ -143,6 +143,8 @@ potential-bugs: | |||
active: true | |||
|
|||
style: | |||
CanBeNonNullableProperty: | |||
active: false |
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.
I think that it's safe to enable this. Or, if it isn't, remove these lines
…bles in the future; Re-enabled usage of rule for Detekt codebase; Fixed test code snippets that weren't compiling
Should be good to go, but something happened on the Windows Java 8 build. It looks like Gradle crashed for whatever reason, not sure if it's got anything to do with the code. |
It seems No doc for this in detekt.dev |
Doc is here: https://detekt.dev/docs/rules/style/#canbenonnullable |
This addresses issue #4376