-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace with compound operator inspection
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...n/kotlin/org/move/ide/inspections/compilerV2/MvReplaceWithCompoundAssignmentInspection.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.move.ide.inspections.compilerV2 | ||
|
||
import com.intellij.codeInsight.PsiEquivalenceUtil | ||
import com.intellij.codeInspection.ProblemHighlightType.WEAK_WARNING | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import org.move.ide.inspections.DiagnosticFix | ||
import org.move.lang.core.MOVE_ARITHMETIC_BINARY_OPS | ||
import org.move.lang.core.psi.MvAssignmentExpr | ||
import org.move.lang.core.psi.MvBinaryExpr | ||
import org.move.lang.core.psi.ext.elementType | ||
import org.move.lang.core.psi.ext.operator | ||
import org.move.lang.core.psi.psiFactory | ||
|
||
class MvReplaceWithCompoundAssignmentInspection: | ||
Move2OnlyInspectionBase<MvAssignmentExpr>(MvAssignmentExpr::class.java) { | ||
|
||
override fun visitTargetElement(element: MvAssignmentExpr, holder: ProblemsHolder, isOnTheFly: Boolean) { | ||
val lhsExpr = element.expr | ||
val initializerExpr = element.initializer.expr ?: return | ||
if (initializerExpr is MvBinaryExpr | ||
&& initializerExpr.operator.elementType in MOVE_ARITHMETIC_BINARY_OPS | ||
) { | ||
// take lhs of binary plus expr | ||
val argumentExpr = initializerExpr.left | ||
if (PsiEquivalenceUtil.areElementsEquivalent(lhsExpr, argumentExpr)) { | ||
val op = initializerExpr.operator.text | ||
holder.registerProblem( | ||
element, | ||
"Can be replaced with compound assignment", | ||
WEAK_WARNING, | ||
ReplaceWithCompoundAssignmentFix(element, op) | ||
) | ||
} | ||
} | ||
} | ||
|
||
class ReplaceWithCompoundAssignmentFix(assignmentExpr: MvAssignmentExpr, val op: String): | ||
DiagnosticFix<MvAssignmentExpr>(assignmentExpr) { | ||
|
||
override fun getText(): String = "Replace with compound assignment expr" | ||
|
||
override fun invoke(project: Project, file: PsiFile, element: MvAssignmentExpr) { | ||
val lhsExpr = element.expr | ||
val rhsExpr = (element.initializer.expr as? MvBinaryExpr)?.right ?: return | ||
|
||
val psiFactory = project.psiFactory | ||
val assignBinExpr = psiFactory.expr<MvBinaryExpr>("x $op= 1") | ||
assignBinExpr.left.replace(lhsExpr) | ||
assignBinExpr.right?.replace(rhsExpr) | ||
|
||
element.replace(assignBinExpr) | ||
} | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/resources/inspectionDescriptions/MvReplaceWithCompoundAssignment.html
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Replaces `x = x + 1` with `x += 1` | ||
</body> | ||
</html> |
72 changes: 72 additions & 0 deletions
72
...tlin/org/move/ide/inspections/compilerV2/MvReplaceWithCompoundAssignmentInspectionTest.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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.move.ide.inspections.compilerV2 | ||
|
||
import org.intellij.lang.annotations.Language | ||
import org.move.utils.tests.MoveV2 | ||
import org.move.utils.tests.annotation.InspectionTestBase | ||
|
||
@MoveV2 | ||
class MvReplaceWithCompoundAssignmentInspectionTest: | ||
InspectionTestBase(MvReplaceWithCompoundAssignmentInspection::class) { | ||
|
||
fun `test replace variable assignment with plus`() = doFixTest( | ||
""" | ||
module 0x1::m { | ||
fun main() { | ||
let x = 1; | ||
<weak_warning descr="Can be replaced with compound assignment">/*caret*/x = x + 1</weak_warning>; | ||
} | ||
} | ||
""", """ | ||
module 0x1::m { | ||
fun main() { | ||
let x = 1; | ||
x += 1; | ||
} | ||
} | ||
""" | ||
) | ||
|
||
fun `test replace variable assignment with left shift`() = doFixTest( | ||
""" | ||
module 0x1::m { | ||
fun main() { | ||
let x = 1; | ||
<weak_warning descr="Can be replaced with compound assignment">/*caret*/x = x << 1</weak_warning>; | ||
} | ||
} | ||
""", """ | ||
module 0x1::m { | ||
fun main() { | ||
let x = 1; | ||
x <<= 1; | ||
} | ||
} | ||
""" | ||
) | ||
|
||
fun `test replace deref assignment with plus`() = doFixTest( | ||
""" | ||
module 0x1::m { | ||
fun main(p: &u8) { | ||
<weak_warning descr="Can be replaced with compound assignment">/*caret*/*p = *p + 1</weak_warning>; | ||
} | ||
} | ||
""", """ | ||
module 0x1::m { | ||
fun main(p: &u8) { | ||
*p += 1; | ||
} | ||
} | ||
""" | ||
) | ||
|
||
private fun doTest(@Language("Move") text: String) = | ||
checkByText(text, checkWarn = false, checkWeakWarn = true) | ||
|
||
private fun doFixTest( | ||
@Language("Move") before: String, | ||
@Language("Move") after: String, | ||
) = | ||
checkFixByText("Replace with compound assignment expr", before, after, | ||
checkWarn = false, checkWeakWarn = true) | ||
} |