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

Add UseRequireNotNull/UseCheckNotNull rules #3003

Merged
merged 3 commits into from
Aug 23, 2020

Conversation

t-kameyama
Copy link
Contributor

Fixes #2998

@codecov
Copy link

codecov bot commented Aug 22, 2020

Codecov Report

Merging #3003 into master will decrease coverage by 0.01%.
The diff coverage is 77.41%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3003      +/-   ##
============================================
- Coverage     79.76%   79.75%   -0.02%     
- Complexity     2493     2503      +10     
============================================
  Files           423      427       +4     
  Lines          7488     7518      +30     
  Branches       1409     1418       +9     
============================================
+ Hits           5973     5996      +23     
  Misses          767      767              
- Partials        748      755       +7     
Impacted Files Coverage Δ Complexity Δ
...tlab/arturbosch/detekt/rules/KtBinaryExpression.kt 0.00% <0.00%> (ø) 0.00 <0.00> (?)
...gitlab/arturbosch/detekt/rules/KtCallExpression.kt 20.00% <20.00%> (ø) 0.00 <0.00> (?)
...b/arturbosch/detekt/rules/style/UseCheckNotNull.kt 90.90% <90.90%> (ø) 5.00 <5.00> (?)
...arturbosch/detekt/rules/style/UseRequireNotNull.kt 90.90% <90.90%> (ø) 5.00 <5.00> (?)
...rturbosch/detekt/rules/style/StyleGuideProvider.kt 100.00% <100.00%> (ø) 2.00 <0.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 a7fc88b...26ec242. Read the comment docs.

*/
class UseCheckNotNull(config: Config = Config.empty) : Rule(config) {
companion object {
private val requireFunctionFqName = FqName("kotlin.check")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private val requireFunctionFqName = FqName("kotlin.check")
private val checkFunctionFqName = FqName("kotlin.check")


override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
if (expression.isCallingWithNonNullCheckArgument(requireFunctionFqName, bindingContext)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (expression.isCallingWithNonNullCheckArgument(requireFunctionFqName, bindingContext)) {
if (expression.isCallingWithNonNullCheckArgument(checkFunctionFqName, bindingContext)) {

)

override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a
if (bindingContext == BindingContext.EMPTY) return
just after this statement

)

override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a
if (bindingContext == BindingContext.EMPTY) return
just after this statement

it("reports a `require` call with a non-null check") {
val code = """
fun test(i: Int?) {
require(i != null)
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 write a test also for require(null != i)?

it("reports a `check` call with a non-null check") {
val code = """
fun test(i: Int?) {
check(i != null)
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 write a test also for check(null != i)?

@cortinico
Copy link
Member

Thanks for the great addition @t-kameyama 🙏

@t-kameyama t-kameyama requested a review from cortinico August 23, 2020 06:46
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.

LGTM 👍 Just a small nit

val code = """
fun test(i: Int?, j: Int?) {
require(i != null)
require(null != j)
Copy link
Member

Choose a reason for hiding this comment

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

nit: please split this in two tests. It gives us more granularity in understanding what breaks once a test fails

@arturbosch
Copy link
Member

Cool! One rule less on my todo list xD, thanks

@arturbosch arturbosch merged commit e828746 into detekt:master Aug 23, 2020
@arturbosch arturbosch added this to the 1.12.0 milestone Aug 23, 2020
@t-kameyama t-kameyama deleted the issue_2998 branch August 23, 2020 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace require(x != null) with requireNotNull(x)
3 participants