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 |
cortinico
left a comment
There was a problem hiding this comment.
I'd say to have CanBeNonNullableProperty disqualify any property that derives its value from Java source code
+1 on this
| Debt.TEN_MINS | ||
| ) | ||
|
|
||
| override fun visitKtFile(file: KtFile) { |
There was a problem hiding this comment.
You're missing a super.visitKtFile(file) call here
There was a problem hiding this comment.
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.
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.
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.
Can you split into smaller tests?
| } | ||
| } | ||
| """ | ||
| Assertions.assertThat(subject.compileAndLintWithContext(env, code)).hasSize(5) |
…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. |
BraisGabin
left a comment
There was a problem hiding this comment.
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".
|
|
||
| style: | ||
| CanBeNonNullableProperty: | ||
| active: false |
There was a problem hiding this comment.
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