Java: fix "Variable assigned to itself" inspection quick-fix operator assignment bug (IDEA-306674)

GitOrigin-RevId: 92f5ab3b782cd0a2e6af8f83ff80de64f000c3bf
This commit is contained in:
Bas Leijdekkers
2024-06-20 17:06:55 +02:00
committed by intellij-monorepo-bot
parent c4c6723e5a
commit 774c6c6d42
6 changed files with 31 additions and 23 deletions

View File

@@ -2,6 +2,7 @@
package com.intellij.codeInspection.sillyAssignment;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.java.JavaBundle;
@@ -9,7 +10,6 @@ import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.EquivalenceChecker;
@@ -141,7 +141,7 @@ public final class SillyAssignmentInspection extends AbstractBaseJavaLocalInspec
@NotNull
@Override
public String getFamilyName() {
return JavaBundle.message("assignment.to.itself.quickfix.name");
return InspectionsBundle.message("assignment.to.itself.quickfix.name");
}
@Override
@@ -154,24 +154,18 @@ public final class SillyAssignmentInspection extends AbstractBaseJavaLocalInspec
if (parent instanceof PsiVariable) {
expression.delete();
}
if (!(parent instanceof PsiAssignmentExpression assignmentExpression)) {
return;
}
final PsiExpression lhs = assignmentExpression.getLExpression();
final PsiExpression rhs = assignmentExpression.getRExpression();
if (PsiTreeUtil.isAncestor(lhs, expression, false)) {
if (rhs != null) {
assignmentExpression.replace(rhs);
else if (parent instanceof PsiAssignmentExpression assignmentExpression) {
PsiElement grandParent = PsiUtil.skipParenthesizedExprUp(assignmentExpression.getParent());
if (grandParent instanceof PsiAssignmentExpression) {
grandParent.replace(assignmentExpression);
}
else {
assignmentExpression.delete();
}
}
else {
final PsiElement grandParent = assignmentExpression.getParent();
if (grandParent instanceof PsiExpressionStatement) {
else if (grandParent instanceof PsiExpressionStatement) {
grandParent.delete();
}
else if (grandParent instanceof PsiVariable variable) {
PsiExpression rhs = assignmentExpression.getRExpression();
variable.setInitializer(rhs == null ? null : (PsiExpression)rhs.copy());
}
else {
assignmentExpression.replace(expression);
}

View File

@@ -0,0 +1,7 @@
class OperatorAssignment {
void x() {
String name = "Everest";
name <caret>+= ".class";
}
}

View File

@@ -0,0 +1,7 @@
class OperatorAssignment {
void x() {
String name = "Everest";
name = name<caret> += ".class";
}
}

View File

@@ -1,5 +1,5 @@
class C {
void a(int i) {
i =
i =
}
}

View File

@@ -1,10 +1,10 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.sillyAssignment.SillyAssignmentInspection;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
@@ -14,6 +14,7 @@ import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
public class RemoveSillyAssignmentFixTest extends LightJavaCodeInsightFixtureTestCase {
public void testRemoveCompleteAssignment() { doTest(); }
public void testOperatorAssignment() { doTest(); }
public void testKeepReference() { doTest(); }
public void testFieldAssignsItself() { doTest(); }
public void testFieldKeepInitializer() { doTest(); }
@@ -27,7 +28,7 @@ public class RemoveSillyAssignmentFixTest extends LightJavaCodeInsightFixtureTes
public void doTest() {
myFixture.enableInspections(SillyAssignmentInspection.class);
myFixture.configureByFile(getTestName(false) + ".java");
final IntentionAction intention = myFixture.findSingleIntention(JavaBundle.message("assignment.to.itself.quickfix.name"));
final IntentionAction intention = myFixture.findSingleIntention(InspectionsBundle.message("assignment.to.itself.quickfix.name"));
assertNotNull(intention);
myFixture.launchAction(intention);
NonBlockingReadActionImpl.waitForAsyncTaskCompletion();
@@ -36,8 +37,8 @@ public class RemoveSillyAssignmentFixTest extends LightJavaCodeInsightFixtureTes
protected void assertQuickfixNotAvailable() {
myFixture.enableInspections(SillyAssignmentInspection.class);
final String quickfixName = JavaBundle.message("assignment.to.itself.quickfix.name");
myFixture.configureByFile(getTestName(false) + ".java");
final String quickfixName = InspectionsBundle.message("assignment.to.itself.quickfix.name");
assertEmpty("Quickfix '" + quickfixName + "' is available unexpectedly", myFixture.filterAvailableIntentions(quickfixName));
}

View File

@@ -38,7 +38,6 @@ annotate.intention.chooser.title=Choose Annotation to Add
assignment.to.declared.variable.problem.descriptor=Variable ''{0}'' is initialized with self assignment
assignment.to.itself.problem.descriptor=Variable ''{0}'' is assigned to itself
assignment.array.element.to.itself.problem.descriptor=Array element is assigned to itself
assignment.to.itself.quickfix.name=Remove self assignment
bean.property=Bean Property
boolean.method.is.always.inverted.display.name=Boolean method is always inverted
boolean.method.is.always.inverted.problem.descriptor=Calls to boolean method '#ref()' are always inverted