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 Aug 4, 2022
1 parent 6255584 commit a3aa554
Show file tree
Hide file tree
Showing 58 changed files with 3 additions and 111 deletions.
Expand Up @@ -49,7 +49,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 @@ -66,7 +66,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 @@ -52,8 +52,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 { }
}
}
"""
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 @@ -16,7 +16,6 @@ import org.jetbrains.kotlin.name.FqName
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 @@ -56,7 +55,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 @@ -50,7 +50,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 @@ -62,7 +62,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 @@ -30,7 +30,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 @@ -50,16 +50,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 @@ -63,7 +63,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 @@ -58,8 +58,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 @@ -75,7 +75,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 @@ -58,9 +58,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 @@ -83,7 +83,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 @@ -62,7 +62,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 @@ -72,7 +72,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 @@ -39,7 +39,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 @@ -44,8 +44,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 @@ -46,9 +46,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
Expand Up @@ -46,7 +46,6 @@ class UnsafeCallOnNullableType(config: Config = Config.empty) : Rule(config) {

override fun visitPostfixExpression(expression: KtPostfixExpression) {
super.visitPostfixExpression(expression)
if (bindingContext == BindingContext.EMPTY) return
if (expression.operationToken == KtTokens.EXCLEXCL &&
expression.baseExpression?.getType(bindingContext)?.nullability() == TypeNullability.NULLABLE
) {
Expand Down
Expand Up @@ -46,8 +46,6 @@ class UnsafeCast(config: Config = Config.empty) : Rule(config) {
)

override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS) {
if (bindingContext == BindingContext.EMPTY) return

if (bindingContext.diagnostics.forElement(expression.operationReference)
.any { it.factory == Errors.CAST_NEVER_SUCCEEDS }
) {
Expand Down
Expand Up @@ -51,7 +51,6 @@ class UnusedUnaryOperator(config: Config = Config.empty) : Rule(config) {
@Suppress("ReturnCount")
override fun visitPrefixExpression(expression: KtPrefixExpression) {
super.visitPrefixExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

if (expression.baseExpression == null) return
val operationToken = expression.operationToken
Expand Down
Expand Up @@ -55,7 +55,6 @@ class ObjectExtendsThrowable(config: Config = Config.empty) : Rule(config) {

override fun visitObjectDeclaration(declaration: KtObjectDeclaration) {
super.visitObjectDeclaration(declaration)
if (bindingContext == BindingContext.EMPTY) return
if (!declaration.isObjectLiteral() && declaration.isSubtypeOfThrowable()) {
report(
CodeSmell(
Expand Down
Expand Up @@ -57,7 +57,6 @@ class ReturnFromFinally(config: Config = Config.empty) : Rule(config) {

override fun visitTryExpression(expression: KtTryExpression) {
super.visitTryExpression(expression)
if (bindingContext == BindingContext.EMPTY) return

val finallyBlock = expression.finallyBlock ?: return

Expand Down
Expand Up @@ -62,10 +62,6 @@ class BooleanPropertyNaming(config: Config = Config.empty) : Rule(config) {
}

private fun validateDeclaration(declaration: KtCallableDeclaration) {
if (bindingContext == BindingContext.EMPTY) {
return
}

val name = declaration.identifierName()
val typeName = getTypeName(declaration)
val isBooleanType =
Expand Down
Expand Up @@ -76,16 +76,13 @@ class NoNameShadowing(config: Config = Config.empty) : Rule(config) {

private fun checkNameShadowing(declaration: KtNamedDeclaration) {
val nameIdentifier = declaration.nameIdentifier ?: return
if (bindingContext != BindingContext.EMPTY &&
bindingContext.diagnostics.forElement(declaration).any { it.factory == Errors.NAME_SHADOWING }
) {
if (bindingContext.diagnostics.forElement(declaration).any { it.factory == Errors.NAME_SHADOWING }) {
report(CodeSmell(issue, Entity.from(nameIdentifier), "Name shadowed: ${nameIdentifier.text}"))
}
}

override fun visitLambdaExpression(lambdaExpression: KtLambdaExpression) {
super.visitLambdaExpression(lambdaExpression)
if (bindingContext == BindingContext.EMPTY) return
val implicitParameter = lambdaExpression.implicitParameter(bindingContext) ?: return
if (lambdaExpression.hasImplicitParameterReference(implicitParameter, bindingContext) &&
lambdaExpression.hasParentImplicitParameterLambda()
Expand Down
Expand Up @@ -59,10 +59,6 @@ class NonBooleanPropertyPrefixedWithIs(config: Config = Config.empty) : Rule(con
}

private fun validateDeclaration(declaration: KtCallableDeclaration) {
if (bindingContext == BindingContext.EMPTY) {
return
}

val name = declaration.identifierName()

if (name.startsWith("is") && name.length > 2 && !name[2].isLowerCase()) {
Expand Down
Expand Up @@ -52,11 +52,6 @@ class ArrayPrimitive(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

override fun visitKtFile(file: KtFile) {
if (bindingContext == BindingContext.EMPTY) return
super.visitKtFile(file)
}

override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
if (expression.calleeExpression?.text !in factoryMethodNames) return
Expand Down
Expand Up @@ -51,8 +51,6 @@ class CouldBeSequence(config: Config = Config.empty) : Rule(config) {
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

if (bindingContext == BindingContext.EMPTY) return

if (visitedCallExpressions.contains(expression)) return

if (!expression.isCalling(operationsFqNames)) return
Expand Down
Expand Up @@ -52,7 +52,6 @@ class ExplicitCollectionElementAccessMethod(config: Config = Config.empty) : Rul

override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
super.visitDotQualifiedExpression(expression)
if (bindingContext == BindingContext.EMPTY) return
val call = expression.selectorExpression as? KtCallExpression ?: return
if (isIndexGetterRecommended(call) || isIndexSetterRecommended(call)) {
report(CodeSmell(issue, Entity.from(expression), issue.description))
Expand Down
Expand Up @@ -105,8 +105,6 @@ class ForbiddenMethodCall(config: Config = Config.empty) : Rule(config) {
}

private fun check(expression: KtExpression) {
if (bindingContext == BindingContext.EMPTY) return

val descriptors = expression.getResolvedCall(bindingContext)?.resultingDescriptor?.let {
val foundDescriptors = if (it is PropertyDescriptor) {
listOfNotNull(it.unwrappedGetMethod, it.unwrappedSetMethod)
Expand Down
Expand Up @@ -57,7 +57,6 @@ class ForbiddenVoid(config: Config = Config.empty) : Rule(config) {

@Suppress("ReturnCount")
override fun visitTypeReference(typeReference: KtTypeReference) {
if (bindingContext == BindingContext.EMPTY) return
val kotlinType = typeReference.getAbbreviatedTypeOrType(bindingContext) ?: return

if (kotlinType.fqNameOrNull() == VOID_FQ_NAME) {
Expand Down
Expand Up @@ -54,9 +54,6 @@ class LibraryCodeMustSpecifyReturnType(config: Config = Config.empty) : Rule(con
)

override fun visitProperty(property: KtProperty) {
if (bindingContext == BindingContext.EMPTY) {
return
}
if (property.explicitReturnTypeRequired()) {
report(
CodeSmell(
Expand Down

0 comments on commit a3aa554

Please sign in to comment.