Skip to content

Commit

Permalink
Remove old bindingContext checks
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Sep 8, 2022
1 parent 7810ba8 commit 5c1a3b9
Show file tree
Hide file tree
Showing 58 changed files with 3 additions and 152 deletions.
Expand Up @@ -12,7 +12,6 @@ import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getParameterForArgument
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall

Expand Down Expand Up @@ -49,7 +48,6 @@ class NamedArguments(config: Config = Config.empty) : Rule(config) {
private val ignoreArgumentsMatchingNames: Boolean by config(defaultValue = false)

override fun visitCallExpression(expression: KtCallExpression) {
if (bindingContext == BindingContext.EMPTY) return
val valueArguments = expression.valueArguments.filterNot { it is KtLambdaArgument }
if (valueArguments.size > threshold && expression.canNameArguments()) {
val message = "This function call has ${valueArguments.size} arguments. To call a function with more " +
Expand Down
Expand Up @@ -16,7 +16,6 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall

/**
Expand Down Expand Up @@ -66,7 +65,6 @@ class NestedScopeFunctions(config: Config = Config.empty) : Rule(config) {
}

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
function.accept(FunctionDepthVisitor())
}

Expand Down
Expand Up @@ -9,7 +9,6 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.isNullable

Expand Down Expand Up @@ -52,8 +51,6 @@ class ReplaceSafeCallChainWithRun(config: Config = Config.empty) : Rule(config)
override fun visitSafeQualifiedExpression(expression: KtSafeQualifiedExpression) {
super.visitSafeQualifiedExpression(expression)

if (bindingContext == BindingContext.EMPTY) return

/* We want the last safe qualified expression in the chain, so if there are more in this chain then there's no
need to run checks on this one */
if (expression.parent is KtSafeQualifiedExpression) return
Expand Down
Expand Up @@ -4,7 +4,6 @@ import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -94,27 +93,10 @@ class NestedScopeFunctionsSpec(private val env: KotlinCoreEnvironment) {
expectNoFindings()
}

@Test
fun `should not report when binding context is empty`() {
givenCode = """
fun f() {
1.run {
1.run { }
}
}
""".trimIndent()
whenLintRunsWithoutContext()
expectNoFindings()
}

private fun whenLintRuns() {
actual = subject.compileAndLintWithContext(env, givenCode)
}

private fun whenLintRunsWithoutContext() {
actual = subject.compileAndLint(givenCode)
}

private fun expectSourceLocation(location: Pair<Int, Int>) {
assertThat(actual).hasStartSourceLocation(location.first, location.second)
}
Expand Down
Expand Up @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.psi.KtConstructorDelegationCall
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext.EMPTY
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.types.typeUtil.supertypes

Expand Down Expand Up @@ -57,7 +56,7 @@ class InjectDispatcher(config: Config) : Rule(config) {

override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
super.visitSimpleNameExpression(expression)
if (bindingContext == EMPTY || expression.getReferencedName() !in dispatcherNames) return
if (expression.getReferencedName() !in dispatcherNames) return
val type = expression.getType(bindingContext) ?: return
val isCoroutineDispatcher = type.fqNameOrNull() == COROUTINE_DISPATCHER_FQCN ||
type.supertypes().any { it.fqNameOrNull() == COROUTINE_DISPATCHER_FQCN }
Expand Down
Expand Up @@ -66,7 +66,6 @@ class RedundantSuspendModifier(config: Config) : Rule(config) {
)

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
val suspendModifier = function.modifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) ?: return
if (!function.hasBody()) return
if (function.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return
Expand Down
Expand Up @@ -16,7 +16,6 @@ import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.hasSuspendModifier
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull

Expand Down Expand Up @@ -50,7 +49,6 @@ class SleepInsteadOfDelay(config: Config = Config.empty) : Rule(config) {
)

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
if (function.modifierList?.hasSuspendModifier() == true) {
function.checkDescendants(SUSPEND_FUN_MESSAGE)
}
Expand Down
Expand Up @@ -60,7 +60,6 @@ class SuspendFunWithCoroutineScopeReceiver(config: Config) : Rule(config) {
)

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
checkReceiver(function)
}

Expand Down
Expand Up @@ -73,7 +73,6 @@ class SuspendFunWithFlowReturnType(config: Config) : Rule(config) {
)

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
val suspendModifier = function.modifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) ?: return
bindingContext[BindingContext.FUNCTION, function]
?.returnType
Expand Down
Expand Up @@ -16,7 +16,6 @@ import io.gitlab.arturbosch.detekt.rules.fqNameOrNull
import org.jetbrains.kotlin.lexer.KtTokens.EQEQEQ
import org.jetbrains.kotlin.lexer.KtTokens.EXCLEQEQEQ
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getType

/**
Expand Down Expand Up @@ -62,7 +61,6 @@ class AvoidReferentialEquality(config: Config) : Rule(config) {
}

private fun checkBinaryExpression(expression: KtBinaryExpression) {
if (bindingContext == BindingContext.EMPTY) return
if (expression.operationToken != EQEQEQ && expression.operationToken != EXCLEQEQEQ) return

val checkedType = expression.left?.getType(bindingContext)?.fqNameOrNull() ?: return
Expand Down
Expand Up @@ -11,7 +11,6 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.resolve.BindingContext

/**
* Deprecated elements are expected to be removed in the future. Alternatives should be found if possible.
Expand All @@ -30,7 +29,6 @@ class Deprecation(config: Config) : Rule(config) {
override val defaultRuleIdAliases = setOf("DEPRECATION")

override fun visitElement(element: PsiElement) {
if (bindingContext == BindingContext.EMPTY) return
if (hasDeprecationCompilerWarnings(element)) {
val entity = if (element is KtNamedDeclaration) Entity.atName(element) else Entity.from(element)
report(CodeSmell(issue, entity, "${element.text} is deprecated."))
Expand Down
Expand Up @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtIsExpression
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getType

/**
Expand Down Expand Up @@ -50,16 +49,13 @@ class DontDowncastCollectionTypes(config: Config) : Rule(config) {

override fun visitIsExpression(expression: KtIsExpression) {
super.visitIsExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

checkForDowncast(expression, expression.leftHandSide, expression.typeReference)
}

override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS) {
super.visitBinaryWithTypeRHSExpression(expression)

if (bindingContext == BindingContext.EMPTY) return

checkForDowncast(expression, expression.left, expression.right)
}

Expand Down
Expand Up @@ -65,8 +65,6 @@ class DoubleMutabilityForCollection(config: Config = Config.empty) : Rule(config
override fun visitProperty(property: KtProperty) {
super.visitProperty(property)

if (bindingContext == BindingContext.EMPTY) return

val type = (bindingContext[BindingContext.VARIABLE, property])?.type ?: return
val standardType = type.fqNameOrNull()
if (property.isVar && standardType in mutableTypes) {
Expand Down
Expand Up @@ -10,7 +10,6 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.cfg.WhenChecker
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.isBooleanOrNullableBoolean
Expand Down Expand Up @@ -63,7 +62,6 @@ class ElseCaseInsteadOfExhaustiveWhen(config: Config = Config.empty) : Rule(conf
override fun visitWhenExpression(whenExpression: KtWhenExpression) {
super.visitWhenExpression(whenExpression)

if (bindingContext == BindingContext.EMPTY) return
checkForElseCaseInsteadOfExhaustiveWhenExpression(whenExpression)
}

Expand Down
Expand Up @@ -12,7 +12,6 @@ import io.gitlab.arturbosch.detekt.rules.isMainFunction
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull

Expand Down Expand Up @@ -58,8 +57,6 @@ class ExitOutsideMain(config: Config = Config.empty) : Rule(config) {
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

if (bindingContext == BindingContext.EMPTY) return

if (expression.getStrictParentOfType<KtNamedFunction>()?.isMainFunction() == true) return
val fqName = expression.getResolvedCall(bindingContext)?.resultingDescriptor?.fqNameOrNull() ?: return

Expand Down
Expand Up @@ -51,7 +51,6 @@ class HasPlatformType(config: Config) : Rule(config) {

override fun visitKtElement(element: KtElement) {
super.visitKtElement(element)
if (bindingContext == BindingContext.EMPTY) return

if (element is KtCallableDeclaration && element.hasImplicitPlatformType()) {
report(
Expand Down
Expand Up @@ -15,7 +15,6 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.api.simplePatternToRegex
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.typeUtil.isUnit
Expand Down Expand Up @@ -75,7 +74,6 @@ class IgnoredReturnValue(config: Config = Config.empty) : Rule(config) {
@Suppress("ReturnCount")
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

if (expression.isUsedAsExpression(bindingContext)) return

Expand Down
Expand Up @@ -13,7 +13,6 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.typeUtil.isUnit

Expand Down Expand Up @@ -58,9 +57,6 @@ class ImplicitUnitReturnType(config: Config) : Rule(config) {
@Suppress("ReturnCount")
override fun visitNamedFunction(function: KtNamedFunction) {
super.visitNamedFunction(function)
if (BindingContext.EMPTY == bindingContext) {
return
}

if (allowExplicitReturnType && function.hasDeclaredReturnType()) {
return
Expand Down
Expand Up @@ -14,7 +14,6 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.cfg.WhenChecker
import org.jetbrains.kotlin.diagnostics.WhenMissingCase
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.calls.util.getType

Expand Down Expand Up @@ -83,7 +82,6 @@ class MissingWhenCase(config: Config = Config.empty) : Rule(config) {

override fun visitWhenExpression(expression: KtWhenExpression) {
super.visitWhenExpression(expression)
if (bindingContext == BindingContext.EMPTY) return
if (allowElseExpression && expression.elseExpression != null) return
checkMissingWhenExpression(expression)
}
Expand Down
Expand Up @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull

Expand Down Expand Up @@ -62,7 +61,6 @@ class NullCheckOnMutableProperty(config: Config) : Rule(config) {
)

override fun visitKtFile(file: KtFile) {
if (bindingContext == BindingContext.EMPTY) return
super.visitKtFile(file)
NullCheckVisitor().visitKtFile(file)
}
Expand Down
Expand Up @@ -61,7 +61,6 @@ class NullableToStringCall(config: Config = Config.empty) : Rule(config) {

override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
super.visitSimpleNameExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

val simpleOrCallExpression = expression.parent.safeAs<KtCallExpression>() ?: expression
val targetExpression = simpleOrCallExpression.targetExpression() ?: return
Expand Down
Expand Up @@ -11,7 +11,6 @@ import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.resolve.BindingContext

/**
* Turn on this rule to flag `when` expressions that contain a redundant `else` case. This occurs when it can be
Expand Down Expand Up @@ -72,7 +71,6 @@ class RedundantElseInWhen(config: Config = Config.empty) : Rule(config) {
override fun visitWhenExpression(whenExpression: KtWhenExpression) {
super.visitWhenExpression(whenExpression)

if (bindingContext == BindingContext.EMPTY) return
val elseEntry = whenExpression.entries.lastOrNull { it.isElse } ?: return
val compilerReports = bindingContext.diagnostics.forElement(elseEntry)
if (compilerReports.any { it.factory == Errors.REDUNDANT_ELSE_IN_WHEN }) {
Expand Down
Expand Up @@ -11,7 +11,6 @@ import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtUnaryExpression
import org.jetbrains.kotlin.resolve.BindingContext

/**
* Reports unnecessary not-null operator usage (!!) that can be removed by the user.
Expand Down Expand Up @@ -39,7 +38,6 @@ class UnnecessaryNotNullOperator(config: Config = Config.empty) : Rule(config) {

override fun visitUnaryExpression(expression: KtUnaryExpression) {
super.visitUnaryExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

val compilerReports = bindingContext.diagnostics.forElement(expression.operationReference)
if (compilerReports.any { it.factory == Errors.UNNECESSARY_NOT_NULL_ASSERTION }) {
Expand Down
Expand Up @@ -14,7 +14,6 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.resolve.BindingContext

/**
* Reports unnecessary safe call operators (`?.`) that can be removed by the user.
Expand Down Expand Up @@ -44,8 +43,6 @@ class UnnecessarySafeCall(config: Config = Config.empty) : Rule(config) {
override fun visitSafeQualifiedExpression(expression: KtSafeQualifiedExpression) {
super.visitSafeQualifiedExpression(expression)

if (bindingContext == BindingContext.EMPTY) return

val safeAccessElement = expression.getChildOfType<LeafPsiElement>()
if (safeAccessElement == null || safeAccessElement.elementType != KtTokens.SAFE_ACCESS) {
return
Expand Down
Expand Up @@ -60,7 +60,6 @@ class UnreachableCatchBlock(config: Config = Config.empty) : Rule(config) {
@Suppress("ReturnCount")
override fun visitCatchSection(catchClause: KtCatchClause) {
super.visitCatchSection(catchClause)
if (bindingContext == BindingContext.EMPTY) return

val tryExpression = catchClause.getStrictParentOfType<KtTryExpression>() ?: return
val prevCatchClauses = tryExpression.catchClauses.takeWhile { it != catchClause }
Expand Down
Expand Up @@ -11,7 +11,6 @@ import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext

/**
* Reports unreachable code.
Expand Down Expand Up @@ -46,9 +45,7 @@ class UnreachableCode(config: Config = Config.empty) : Rule(config) {

override fun visitExpression(expression: KtExpression) {
super.visitExpression(expression)
if (bindingContext != BindingContext.EMPTY &&
bindingContext.diagnostics.forElement(expression).any { it.factory == Errors.UNREACHABLE_CODE }
) {
if (bindingContext.diagnostics.forElement(expression).any { it.factory == Errors.UNREACHABLE_CODE }) {
report(
CodeSmell(
issue,
Expand Down

0 comments on commit 5c1a3b9

Please sign in to comment.