mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
IDEA-123034 Unused assignment inspection - Remove quick fix
This commit is contained in:
+6
-1
@@ -101,7 +101,8 @@ public class DefUseInspectionBase extends BaseJavaBatchLocalInspectionTool {
|
||||
final PsiAssignmentExpression assignment = (PsiAssignmentExpression)context;
|
||||
holder.registerProblem(assignment.getLExpression(),
|
||||
InspectionsBundle.message("inspection.unused.assignment.problem.descriptor3",
|
||||
assignment.getRExpression().getText(), "<code>#ref</code>" + " #loc"), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
|
||||
assignment.getRExpression().getText(), "<code>#ref</code>" + " #loc"),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, createRemoveAssignmentFix());
|
||||
}
|
||||
else {
|
||||
if (context instanceof PsiPrefixExpression && REPORT_PREFIX_EXPRESSIONS ||
|
||||
@@ -153,6 +154,10 @@ public class DefUseInspectionBase extends BaseJavaBatchLocalInspectionTool {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected LocalQuickFix createRemoveAssignmentFix() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new OptionsPanel();
|
||||
|
||||
@@ -50,6 +50,30 @@ public class DefUseInspection extends DefUseInspectionBase {
|
||||
return new RemoveInitializerFix();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LocalQuickFix createRemoveAssignmentFix() {
|
||||
return new RemoveAssignmentFix();
|
||||
}
|
||||
|
||||
private static class RemoveAssignmentFix extends RemoveInitializerFix {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return InspectionsBundle.message("inspection.unused.assignment.remove.assignment.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final PsiElement left = descriptor.getPsiElement();
|
||||
if (!(left instanceof PsiReferenceExpression)) return;
|
||||
final PsiElement parent = left.getParent();
|
||||
if (!(parent instanceof PsiAssignmentExpression)) return;
|
||||
|
||||
final PsiElement resolve = ((PsiReferenceExpression)left).resolve();
|
||||
if (!(resolve instanceof PsiVariable)) return;
|
||||
sideEffectAwareRemove(project, ((PsiAssignmentExpression)parent).getRExpression(), parent, (PsiVariable)resolve);
|
||||
}
|
||||
}
|
||||
private static class RemoveInitializerFix implements LocalQuickFix {
|
||||
@Override
|
||||
@NotNull
|
||||
@@ -62,9 +86,14 @@ public class DefUseInspection extends DefUseInspectionBase {
|
||||
final PsiElement psiInitializer = descriptor.getPsiElement();
|
||||
if (!(psiInitializer instanceof PsiExpression)) return;
|
||||
if (!(psiInitializer.getParent() instanceof PsiVariable)) return;
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(psiInitializer.getContainingFile())) return;
|
||||
|
||||
final PsiVariable variable = (PsiVariable)psiInitializer.getParent();
|
||||
sideEffectAwareRemove(project, psiInitializer, psiInitializer, variable);
|
||||
}
|
||||
|
||||
protected void sideEffectAwareRemove(Project project, PsiElement psiInitializer, PsiElement elementToDelete, PsiVariable variable) {
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(psiInitializer.getContainingFile())) return;
|
||||
|
||||
final PsiDeclarationStatement declaration = (PsiDeclarationStatement)variable.getParent();
|
||||
final List<PsiElement> sideEffects = new ArrayList<PsiElement>();
|
||||
boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects);
|
||||
@@ -74,25 +103,30 @@ public class DefUseInspection extends DefUseInspectionBase {
|
||||
res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable,
|
||||
FileEditorManager.getInstance(project).getSelectedTextEditor(),
|
||||
hasSideEffects, sideEffects.get(0).getText(),
|
||||
variable.getTypeElement().getText() + " " + variable.getName() + ";<br>" + PsiExpressionTrimRenderer
|
||||
.render((PsiExpression)psiInitializer));
|
||||
variable.getTypeElement().getText() +
|
||||
" " +
|
||||
variable.getName() +
|
||||
";<br>" +
|
||||
PsiExpressionTrimRenderer
|
||||
.render((PsiExpression)psiInitializer)
|
||||
);
|
||||
}
|
||||
try {
|
||||
if (res == RemoveUnusedVariableUtil.DELETE_ALL) {
|
||||
psiInitializer.delete();
|
||||
elementToDelete.delete();
|
||||
}
|
||||
else if (res == RemoveUnusedVariableUtil.MAKE_STATEMENT) {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null);
|
||||
declaration.getParent().addAfter(statementFromText, declaration);
|
||||
psiInitializer.delete();
|
||||
elementToDelete.delete();
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Remove redundant assignment" "true"
|
||||
class A {
|
||||
void m() {
|
||||
int i;
|
||||
i = 0;
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Remove redundant assignment" "true"
|
||||
class A {
|
||||
void m() {
|
||||
int i;
|
||||
<caret>i = 9;
|
||||
i = 0;
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.codeInspection.defUse.DefUseInspection;
|
||||
import com.intellij.codeInspection.ex.EditInspectionToolsSettingsAction;
|
||||
import com.intellij.psi.JavaElementVisitor;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.psi.PsiLiteralExpression;
|
||||
@@ -88,7 +89,7 @@ public class EmptyIntentionInspectionQuickFixTest extends LightQuickFixTestCase
|
||||
}
|
||||
assertTrue(i < emptyActions.size());
|
||||
for (; i < emptyActions.size(); i++) {
|
||||
if (emptyActions.get(i) instanceof EmptyIntentionAction) {
|
||||
if (emptyActions.get(i) instanceof EditInspectionToolsSettingsAction) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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 com.intellij.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.defUse.DefUseInspection;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class RemoveUnusedAssignmentTest extends LightQuickFixTestCase {
|
||||
public void test() throws Exception { doAllTests(); }
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[] {new DefUseInspection()};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/unusedAssignment";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
return LanguageLevel.JDK_1_5;
|
||||
}
|
||||
}
|
||||
@@ -185,6 +185,7 @@ inspection.unused.assignment.option=Report ++i when may be replaced with (i + 1)
|
||||
inspection.unused.assignment.option1=Report i++ when changed value is not used afterwards
|
||||
inspection.unused.assignment.option2=Report redundant initializers
|
||||
inspection.unused.assignment.remove.quickfix=Remove Redundant Initializer
|
||||
inspection.unused.assignment.remove.assignment.quickfix=Remove redundant assignment
|
||||
inspection.unused.assignment.problem.descriptor1=Variable {0} is never used
|
||||
inspection.unused.assignment.problem.descriptor2=Variable {0} initializer {1} is redundant
|
||||
inspection.unused.assignment.problem.descriptor3=The value {0} assigned to {1} is never used
|
||||
|
||||
Reference in New Issue
Block a user