Skip to content

Fix false-negative for CanBeNonNullable#4993

Merged
BraisGabin merged 3 commits intodetekt:mainfrom
VitalyVPinchuk:null-check-returns-unit
Jun 24, 2022
Merged

Fix false-negative for CanBeNonNullable#4993
BraisGabin merged 3 commits intodetekt:mainfrom
VitalyVPinchuk:null-check-returns-unit

Conversation

@VitalyVPinchuk
Copy link
Copy Markdown
Contributor

For #4492
Report code smell when null-check expression returns 'Unit'

The rule will report code smell when null-check expression returns 'Unit'
# Conflicts:
#	detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullable.kt
@marschwar
Copy link
Copy Markdown
Contributor

How about this example? If there is any side effect before the guard statement it should not report an issue, right?

fun foo(a: Int?) {
    println("side effect")
    if (a == null) return
    println(a)
}

@VitalyVPinchuk
Copy link
Copy Markdown
Contributor Author

Hmm... I didn't think of it.
We'll lose part of the side effect if we make parameter non-nullable. Probably, it's better not to report such a case...
Give your thoughts, please

@marschwar
Copy link
Copy Markdown
Contributor

That was exactly my thought. I would not report if there is any side effect before the guard statement. Since detecting if it really is a side effect would be hard, I would not allow anything before the guard.

@VitalyVPinchuk
Copy link
Copy Markdown
Contributor Author

You're right. I'll fix it.

@BraisGabin BraisGabin merged commit 4742842 into detekt:main Jun 24, 2022
@cortinico cortinico added this to the 1.21.0 milestone Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants