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 arguments
- Loading branch information
1 parent
f87a72d
commit e7a82aa
Showing
5 changed files
with
95 additions
and
32 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
33 changes: 33 additions & 0 deletions
33
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,43 @@ | ||
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() | ||
} | ||
|
||
@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" } | ||
requireNotNull(languageVersionSettings) { "The languageVersionSettings must not be null" } | ||
requireNotNull(dataFlowValueFactory) { "The dataFlowValueFactory must not be null" } | ||
|
||
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