mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-87794 no quickfix for condtional inspection fixed
This commit is contained in:
@@ -34,6 +34,7 @@ import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.dataFlow.instructions.*;
|
||||
import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.lang.ASTFactory;
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -41,6 +42,8 @@ import com.intellij.openapi.project.ProjectManager;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
@@ -230,8 +233,11 @@ public class DataFlowInspection extends BaseLocalInspectionTool {
|
||||
else if (psiAnchor != null && !reportedAnchors.contains(psiAnchor) && !isCompileConstantInIfCondition(psiAnchor)) {
|
||||
boolean evaluatesToTrue = trueSet.contains(instruction);
|
||||
if (onTheLeftSideOfConditionalAssignemnt(psiAnchor)) {
|
||||
holder.registerProblem(psiAnchor, InspectionsBundle.message("dataflow.message.pointless.assignment.expression",
|
||||
Boolean.toString(evaluatesToTrue)));
|
||||
holder.registerProblem(
|
||||
psiAnchor,
|
||||
InspectionsBundle.message("dataflow.message.pointless.assignment.expression", Boolean.toString(evaluatesToTrue)),
|
||||
createSimplifyToAssignmentFix()
|
||||
);
|
||||
}
|
||||
else {
|
||||
boolean report = !(psiAnchor.getParent() instanceof PsiAssertStatement) || !DONT_REPORT_TRUE_ASSERT_STATEMENTS || !evaluatesToTrue;
|
||||
@@ -370,6 +376,40 @@ public class DataFlowInspection extends BaseLocalInspectionTool {
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static LocalQuickFix createSimplifyToAssignmentFix() {
|
||||
return new LocalQuickFix() {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return InspectionsBundle.message("inspection.data.flow.simplify.to.assignment.quickfix.name");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return InspectionsBundle.message("inspection.data.flow.simplify.boolean.expression.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final PsiElement psiElement = descriptor.getPsiElement();
|
||||
if (psiElement == null) return;
|
||||
|
||||
final PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(psiElement, PsiAssignmentExpression.class);
|
||||
if (assignmentExpression == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
final String lExpressionText = assignmentExpression.getLExpression().getText();
|
||||
final PsiExpression rExpression = assignmentExpression.getRExpression();
|
||||
final String rExpressionText = rExpression != null ? rExpression.getText() : "";
|
||||
assignmentExpression.replace(factory.createExpressionFromText(lExpressionText + " = " + rExpressionText, psiElement));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static SimplifyBooleanExpressionFix createIntention(PsiElement element, boolean value) {
|
||||
if (!(element instanceof PsiExpression)) return null;
|
||||
final PsiExpression expression = (PsiExpression)element;
|
||||
|
||||
@@ -49,6 +49,7 @@ inspection.data.flow.nullable.quickfix.option=<html><body>Suggest @Nullable anno
|
||||
inspection.data.flow.true.asserts.option=<html><body>Don't report assert statements with condition statically proven to be always <code>true</code></body></html>
|
||||
inspection.data.flow.redundant.instanceof.quickfix=Replace with != null
|
||||
inspection.data.flow.simplify.boolean.expression.quickfix=Simplify Boolean Expression
|
||||
inspection.data.flow.simplify.to.assignment.quickfix.name=Simplify to normal assignment
|
||||
configure.annotations.option=Configure annotations
|
||||
|
||||
#messages from dataflow inspection
|
||||
|
||||
+13
-15
@@ -31,21 +31,19 @@ import java.util.Map;
|
||||
public class ReplaceOperatorAssignmentWithAssignmentIntention
|
||||
extends MutablyNamedIntention {
|
||||
|
||||
private static final Map<IElementType, IElementType> tokenMap = new HashMap();
|
||||
|
||||
static {
|
||||
tokenMap.put(JavaTokenType.PLUSEQ, JavaTokenType.PLUS);
|
||||
tokenMap.put(JavaTokenType.MINUSEQ, JavaTokenType.MINUS);
|
||||
tokenMap.put(JavaTokenType.ASTERISKEQ, JavaTokenType.ASTERISK);
|
||||
tokenMap.put(JavaTokenType.DIVEQ, JavaTokenType.DIV);
|
||||
tokenMap.put(JavaTokenType.ANDEQ, JavaTokenType.AND);
|
||||
tokenMap.put(JavaTokenType.OREQ, JavaTokenType.OR);
|
||||
tokenMap.put(JavaTokenType.XOREQ, JavaTokenType.XOR);
|
||||
tokenMap.put(JavaTokenType.PERCEQ, JavaTokenType.PERC);
|
||||
tokenMap.put(JavaTokenType.LTLTEQ, JavaTokenType.LTLT);
|
||||
tokenMap.put(JavaTokenType.GTGTEQ, JavaTokenType.GTGT);
|
||||
tokenMap.put(JavaTokenType.GTGTGTEQ, JavaTokenType.GTGTGT);
|
||||
}
|
||||
private static final Map<IElementType, IElementType> tokenMap = new HashMap<IElementType, IElementType>() {{
|
||||
put(JavaTokenType.PLUSEQ, JavaTokenType.PLUS);
|
||||
put(JavaTokenType.MINUSEQ, JavaTokenType.MINUS);
|
||||
put(JavaTokenType.ASTERISKEQ, JavaTokenType.ASTERISK);
|
||||
put(JavaTokenType.DIVEQ, JavaTokenType.DIV);
|
||||
put(JavaTokenType.ANDEQ, JavaTokenType.AND);
|
||||
put(JavaTokenType.OREQ, JavaTokenType.OR);
|
||||
put(JavaTokenType.XOREQ, JavaTokenType.XOR);
|
||||
put(JavaTokenType.PERCEQ, JavaTokenType.PERC);
|
||||
put(JavaTokenType.LTLTEQ, JavaTokenType.LTLT);
|
||||
put(JavaTokenType.GTGTEQ, JavaTokenType.GTGT);
|
||||
put(JavaTokenType.GTGTGTEQ, JavaTokenType.GTGTGT);
|
||||
}};
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
|
||||
Reference in New Issue
Block a user