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

Added rule CanBeNonNullable #4379

Merged
merged 8 commits into from Dec 26, 2021

Conversation

severn-everett
Copy link
Contributor

This addresses issue #4376

@codecov
Copy link

codecov bot commented Dec 17, 2021

Codecov Report

Merging #4379 (9202e76) into main (7d8acae) will decrease coverage by 0.04%.
The diff coverage is 73.52%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ
.../arturbosch/detekt/rules/style/CanBeNonNullable.kt 73.13% <73.13%> (ø)
...rturbosch/detekt/rules/style/StyleGuideProvider.kt 100.00% <100.00%> (ø)
...etekt/rules/style/optional/MandatoryBracesLoops.kt 73.33% <0.00%> (-2.53%) ⬇️
...ab/arturbosch/detekt/core/config/ValidateConfig.kt 98.59% <0.00%> (-1.41%) ⬇️
...n/kotlin/io/gitlab/arturbosch/detekt/api/Entity.kt 75.00% <0.00%> (-1.20%) ⬇️
.../detekt/rules/exceptions/ObjectExtendsThrowable.kt 80.95% <0.00%> (ø)
...t/generator/collection/RuleSetProviderCollector.kt 75.94% <0.00%> (+1.26%) ⬆️
...tlab/arturbosch/detekt/rules/style/UseDataClass.kt 80.28% <0.00%> (+1.40%) ⬆️
...osch/detekt/rules/exceptions/SwallowedException.kt 77.04% <0.00%> (+1.63%) ⬆️
...b/arturbosch/detekt/core/reporting/OutputFacade.kt 90.90% <0.00%> (+4.54%) ⬆️
... and 1 more

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 7d8acae...9202e76. Read the comment docs.

@severn-everett
Copy link
Contributor Author

@BraisGabin The latest Detekt run on the branch had CanBeNonNullableProperty flag this call in DetektExtension.kt:

    @Deprecated("Use reportsDir which is equivalent", ReplaceWith("reportsDir"))
    val customReportsDir: File?
        get() = reportsDir

The problem is that Kotlin determines that reportsDir is non-nullable, even though it is Java code that could return a nullable. I'd say to have CanBeNonNullableProperty disqualify any property that derives its value from Java source code, but I'm not sure how I'd be able to test that in the Spec files. Any suggestions for how to compile Java code alongside Kotlin code in the tests, or should I just suppress the warning on customReportsDir?

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.

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) {
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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."
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

@cortinico cortinico added this to the 1.20.0 milestone Dec 17, 2021
@BraisGabin
Copy link
Member

but I'm not sure how I'd be able to test that in the Spec files. Any suggestions for how to compile Java code alongside Kotlin code in the tests, or should I just suppress the warning on customReportsDir?

@t-kameyama implemented the option to do that in #4203. I think that you can use the same pattern.

Copy link
Member

@BraisGabin BraisGabin left a 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".

@@ -143,6 +143,8 @@ potential-bugs:
active: true

style:
CanBeNonNullableProperty:
active: false
Copy link
Member

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
@severn-everett
Copy link
Contributor Author

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.

@BraisGabin BraisGabin merged commit f5d2a17 into detekt:main Dec 26, 2021
@severn-everett severn-everett deleted the can_be_non_nullable_property branch December 27, 2021 08:38
@cortinico cortinico added the notable changes Marker for notable changes in the changelog label Jan 3, 2022
@DHosseiny
Copy link

DHosseiny commented Nov 20, 2022

It seems No doc for this in detekt.dev

@cortinico cortinico changed the title Added rule CanBeNonNullableProperty Added rule CanBeNonNullable Nov 20, 2022
@cortinico
Copy link
Member

It seems No doc for this in detekt.dev

Doc is here: https://detekt.dev/docs/rules/style/#canbenonnullable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notable changes Marker for notable changes in the changelog rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants