[FIR] Fix for CanBeReplacedWithOperatorAssignment Checker

This commit is contained in:
vldf
2020-08-03 14:39:07 +03:00
committed by Mikhail Glukhikh
parent 6f3df6faf4
commit 65ebd02a39
4 changed files with 21 additions and 0 deletions
@@ -0,0 +1,4 @@
fun foo(something: Boolean) {
var res = false
res = res and something
}
@@ -0,0 +1,5 @@
FILE: logicOperators.kt
public final fun foo(something: R|kotlin/Boolean|): R|kotlin/Unit| {
lvar res: R|kotlin/Boolean| = Boolean(false)
R|<local>/res| = R|<local>/res|.R|kotlin/Boolean.and|(R|<local>/something|)
}
@@ -115,6 +115,11 @@ public class ExtendedFirDiagnosticsTestGenerated extends AbstractExtendedFirDiag
runTest("compiler/fir/analysis-tests/testData/extendedCheckers/canBeReplacedWithOperatorAssignment/list.kt");
}
@TestMetadata("logicOperators.kt")
public void testLogicOperators() throws Exception {
runTest("compiler/fir/analysis-tests/testData/extendedCheckers/canBeReplacedWithOperatorAssignment/logicOperators.kt");
}
@TestMetadata("multipleOperators.kt")
public void testMultipleOperators() throws Exception {
runTest("compiler/fir/analysis-tests/testData/extendedCheckers/canBeReplacedWithOperatorAssignment/multipleOperators.kt");
@@ -46,6 +46,7 @@ object CanBeReplacedWithOperatorAssignmentChecker : FirExpressionChecker<FirVari
}
fun KtBinaryExpression.matcher(variable: KtNameReferenceExpression): Boolean {
if (!canBeAugmented()) return false
if ((left as? KtNameReferenceExpression)?.getReferencedName() == variable.getReferencedName()) return true
if ((right as? KtNameReferenceExpression)?.getReferencedName() == variable.getReferencedName() && isCommutative()) return true
@@ -68,5 +69,11 @@ object CanBeReplacedWithOperatorAssignmentChecker : FirExpressionChecker<FirVari
private fun KtBinaryExpression.isCommutative() = this.operationToken == KtTokens.PLUS || this.operationToken == KtTokens.MUL
private fun KtBinaryExpression.canBeAugmented() = this.operationToken == KtTokens.PLUS
|| this.operationToken == KtTokens.MUL
|| this.operationToken == KtTokens.MINUS
|| this.operationToken == KtTokens.DIV
|| this.operationToken == KtTokens.PERC
private fun isHierarchicallyTrue(currentOperation: IElementType, nextOperation: IElementType?) = currentOperation == nextOperation
}