Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UnnecessaryNotNullCheck: fix false negative with smart casted argumen…
…ts (#5380) * UnnecessaryNotNullCheck: fix false negative with smart casted arguments * Remove unnecessary BindingContext empty check * Add a comment for KtExpression.getDataFlowAwareTypes
- Loading branch information
1 parent
cdb5928
commit 2e229ec
Showing
5 changed files
with
109 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
detekt-psi-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/TypeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,54 @@ | ||
package io.gitlab.arturbosch.detekt.rules | ||
|
||
import org.jetbrains.kotlin.config.LanguageVersionSettings | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.psi.KtExpression | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory | ||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull | ||
import org.jetbrains.kotlin.types.KotlinType | ||
import org.jetbrains.kotlin.types.TypeUtils | ||
import org.jetbrains.kotlin.util.containingNonLocalDeclaration | ||
|
||
fun KotlinType.fqNameOrNull(): FqName? { | ||
return TypeUtils.getClassDescriptor(this)?.fqNameOrNull() | ||
} | ||
|
||
/** | ||
* Returns types considering data flow. | ||
* | ||
* For Example, for `s` in `print(s)` below, [BindingContext.getType] returns String?, but this function returns String. | ||
* | ||
* ```kotlin | ||
* fun foo(s: String?) { | ||
* if (s != null) { | ||
* println(s) // s is String (smart cast from String?) | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@Suppress("ReturnCount") | ||
fun KtExpression.getDataFlowAwareTypes( | ||
bindingContext: BindingContext, | ||
languageVersionSettings: LanguageVersionSettings, | ||
dataFlowValueFactory: DataFlowValueFactory, | ||
originalType: KotlinType? = bindingContext.getType(this), | ||
): Set<KotlinType> { | ||
require(bindingContext != BindingContext.EMPTY) { "The bindingContext must not be empty" } | ||
|
||
if (originalType == null) return emptySet() | ||
|
||
val dataFlowInfo = bindingContext[BindingContext.EXPRESSION_TYPE_INFO, this] | ||
?.dataFlowInfo | ||
?: return setOf(originalType) | ||
|
||
val containingDeclaration = containingNonLocalDeclaration() | ||
?.let { bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, it] } | ||
?: return setOf(originalType) | ||
|
||
val dataFlowValue = dataFlowValueFactory | ||
.createDataFlowValue(this, originalType, bindingContext, containingDeclaration) | ||
|
||
return dataFlowInfo.getStableTypes(dataFlowValue, languageVersionSettings) | ||
.ifEmpty { setOf(originalType) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters