Warn on for-in-array range variable assignment in loop body
According to KT-21354, this should be a warning in 1.2 and before, and no warning (with changed semantics) in 1.3 and later. NB there are some false positives in this check. #KT-21354 In Progress #KT-21321 In Progress
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.jvm.checkers
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtForExpression
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.AdditionalTypeChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
object JvmArrayVariableInLoopAssignmentChecker : AdditionalTypeChecker {
|
||||
|
||||
override fun checkType(
|
||||
expression: KtExpression,
|
||||
expressionType: KotlinType,
|
||||
expressionTypeWithSmartCast: KotlinType,
|
||||
c: ResolutionContext<*>
|
||||
) {
|
||||
if (c.languageVersionSettings.supportsFeature(LanguageFeature.ProperForInArrayLoopRangeVariableAssignmentSemantic)) return
|
||||
|
||||
val binaryExpression = expression as? KtBinaryExpression ?: return
|
||||
if (binaryExpression.operationToken != KtTokens.EQ) return
|
||||
|
||||
val lhsExpression = binaryExpression.left as? KtSimpleNameExpression ?: return
|
||||
val resolvedCall = lhsExpression.getResolvedCall(c.trace.bindingContext) ?: return
|
||||
val variableDescriptor = resolvedCall.resultingDescriptor as? LocalVariableDescriptor ?: return
|
||||
if (variableDescriptor is SyntheticFieldDescriptor) return
|
||||
if (variableDescriptor.isDelegated) return
|
||||
|
||||
val variableType = variableDescriptor.returnType
|
||||
if (!KotlinBuiltIns.isArrayOrPrimitiveArray(variableType)) return
|
||||
|
||||
if (!isOuterForLoopRangeVariable(expression, variableDescriptor, c)) return
|
||||
|
||||
val dataFlowValueKind = DataFlowValueFactory.createDataFlowValue(lhsExpression, variableType, c).kind
|
||||
if (dataFlowValueKind != DataFlowValue.Kind.STABLE_VARIABLE) return
|
||||
|
||||
c.trace.report(ErrorsJvm.ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE.on(lhsExpression))
|
||||
}
|
||||
|
||||
private fun isOuterForLoopRangeVariable(
|
||||
expression: KtExpression,
|
||||
variableDescriptor: CallableDescriptor,
|
||||
c: ResolutionContext<*>
|
||||
): Boolean {
|
||||
for (parent in expression.parents) {
|
||||
if (parent is KtForExpression) {
|
||||
val rangeExpression = parent.loopRange as? KtSimpleNameExpression ?: continue
|
||||
val rangeResolvedCall = rangeExpression.getResolvedCall(c.trace.bindingContext) ?: continue
|
||||
if (rangeResolvedCall.resultingDescriptor == variableDescriptor) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
+3
@@ -127,6 +127,9 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE, "Symbol is declared in module ''{0}'' which does not export package ''{1}''", STRING, STRING);
|
||||
|
||||
MAP.put(API_VERSION_IS_AT_LEAST_ARGUMENT_SHOULD_BE_CONSTANT, "'apiVersionIsAtLeast' argument should be a constant expression");
|
||||
|
||||
MAP.put(ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE, "Assignment to a for-in-array loop range variable. Behavior may change in Kotlin 1.3. " +
|
||||
"See https://youtrack.jetbrains.com/issue/KT-21354 for more details");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -109,6 +109,8 @@ public interface ErrorsJvm {
|
||||
|
||||
DiagnosticFactory0<KtExpression> API_VERSION_IS_AT_LEAST_ARGUMENT_SHOULD_BE_CONSTANT = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
DiagnosticFactory0<KtExpression> ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
enum NullabilityInformationSource {
|
||||
KOTLIN {
|
||||
@NotNull
|
||||
|
||||
+2
-1
@@ -62,7 +62,8 @@ object JvmPlatformConfigurator : PlatformConfigurator(
|
||||
JavaNullabilityChecker(),
|
||||
RuntimeAssertionsTypeChecker,
|
||||
JavaGenericVarianceViolationTypeChecker,
|
||||
JavaTypeAccessibilityChecker()
|
||||
JavaTypeAccessibilityChecker(),
|
||||
JvmArrayVariableInLoopAssignmentChecker
|
||||
),
|
||||
|
||||
additionalClassifierUsageCheckers = listOf(
|
||||
|
||||
Reference in New Issue
Block a user