mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
IJ-CR-107309 [java-inspections] IDEA-303605 Constant expression - new option to skip reference. PR
GitOrigin-RevId: da46a96d7203ed55853eb6376d6404a69899c479
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b8fa1f2b75
commit
d2645f66ba
@@ -5,7 +5,7 @@ Reports compile-time constant expressions and suggests replacing them with their
|
||||
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
Use the <b>Not report if expression contains non-literal operands</b> option to ignore the expressions which contain non-literal operands,
|
||||
Use the <b>Don't report when the expression contains references to defined constants</b> option to ignore the expressions which contain references to non-literal operands,
|
||||
such as fields, and variables.
|
||||
<p><small>New in 2018.1</small></p>
|
||||
</body>
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
// "Fix all 'Constant expression can be evaluated' problems in file" "true"
|
||||
class Test {
|
||||
private final String field = "field"
|
||||
private final String field = "field";
|
||||
|
||||
void test() {
|
||||
String foo = "Testtest2";
|
||||
String foo = "Test" + field;
|
||||
String foo2 = "Test" + field;
|
||||
int i1 = 2;
|
||||
int i2 = 0;
|
||||
int i3 = Season.SPRING.i + 2;
|
||||
int i4 = Season.i2 + 2;
|
||||
}
|
||||
|
||||
|
||||
enum Season {
|
||||
SPRING(1);
|
||||
public static final int i2 = 2;
|
||||
public final int i;
|
||||
|
||||
Season(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,24 @@
|
||||
// "Fix all 'Constant expression can be evaluated' problems in file" "true"
|
||||
class Test {
|
||||
private final String field = "field"
|
||||
private final String field = "field";
|
||||
|
||||
void test() {
|
||||
String foo = "Test"<caret> + "test2";
|
||||
String foo = "Test" + field;
|
||||
String foo2 = "Test" + field;
|
||||
int i1 = 1 + 1;
|
||||
int i2 = 1 + -1;
|
||||
int i3 = Season.SPRING.i + 2;
|
||||
int i4 = Season.i2 + 2;
|
||||
}
|
||||
|
||||
|
||||
enum Season {
|
||||
SPRING(1);
|
||||
public static final int i2 = 2;
|
||||
public final int i;
|
||||
|
||||
Season(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2266,7 +2266,7 @@ inspection.constant.expression.fix.name=Compute constant value of ''{0}''
|
||||
inspection.constant.expression.fix.name.short=Replace with constant value
|
||||
inspection.constant.expression.fix.name.with.value=Replace ''{0}'' with constant value ''{1}''
|
||||
inspection.constant.expression.fix.family.name=Compute constant value
|
||||
inspection.constant.expression.skip.non.literal=Not report if expression contains non-literal operands
|
||||
inspection.constant.expression.skip.non.literal=Don't report when the expression contains references to defined constants
|
||||
|
||||
inspection.redundant.compare.call.display.name=Redundant 'compare()' method call
|
||||
inspection.redundant.compare.call.fix.name=Inline 'compare()' call
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeHighlighting.HighlightDisplayLevel;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.options.OptPane;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -9,8 +8,8 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.ConstantEvaluationOverflowException;
|
||||
import com.intellij.psi.util.PsiExpressionTrimRenderer;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
@@ -23,12 +22,12 @@ public class ConstantExpressionInspection extends AbstractBaseJavaLocalInspectio
|
||||
private static final int MAX_RESULT_LENGTH_TO_DISPLAY = 40;
|
||||
private static final int MAX_EXPRESSION_LENGTH = 200;
|
||||
|
||||
public boolean skipIfContainsNonLiteralExpression = false;
|
||||
public boolean skipIfContainsReferenceExpression = false;
|
||||
|
||||
@Override
|
||||
public @NotNull OptPane getOptionsPane() {
|
||||
return OptPane.pane(
|
||||
OptPane.checkbox("skipIfContainsNonLiteralExpression",
|
||||
OptPane.checkbox("skipIfContainsReferenceExpression",
|
||||
InspectionGadgetsBundle.message("inspection.constant.expression.skip.non.literal")));
|
||||
}
|
||||
|
||||
@@ -62,9 +61,8 @@ public class ConstantExpressionInspection extends AbstractBaseJavaLocalInspectio
|
||||
String message = valueText.length() > MAX_RESULT_LENGTH_TO_DISPLAY ?
|
||||
InspectionGadgetsBundle.message("inspection.constant.expression.display.name") :
|
||||
InspectionGadgetsBundle.message("inspection.constant.expression.message", valueText);
|
||||
if (getDefaultLevel() != HighlightDisplayLevel.DO_NOT_SHOW &&
|
||||
skipIfContainsNonLiteralExpression &&
|
||||
hasNonLiteralConstant(expression)) {
|
||||
if (skipIfContainsReferenceExpression &&
|
||||
hasReferences(expression)) {
|
||||
if (isOnTheFly) {
|
||||
holder.registerProblem(expression, message, ProblemHighlightType.INFORMATION,
|
||||
new ComputeConstantValueFix(expression, valueText));
|
||||
@@ -81,19 +79,11 @@ public class ConstantExpressionInspection extends AbstractBaseJavaLocalInspectio
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasNonLiteralConstant(PsiExpression expression) {
|
||||
if (expression instanceof PsiLiteralExpression) {
|
||||
return false;
|
||||
}
|
||||
else if (expression instanceof PsiPolyadicExpression polyadicExpression) {
|
||||
return ContainerUtil.exists(polyadicExpression.getOperands(), t -> hasNonLiteralConstant(t));
|
||||
}
|
||||
else if (expression instanceof PsiParenthesizedExpression parenthesizedExpression) {
|
||||
return hasNonLiteralConstant(PsiUtil.skipParenthesizedExprDown(parenthesizedExpression));
|
||||
}
|
||||
else {
|
||||
private static boolean hasReferences(PsiExpression expression) {
|
||||
if (PsiTreeUtil.getChildOfAnyType(expression, PsiReferenceExpression.class) != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class ConstantExpressionInspectionWithFinalTest extends LightQuickFixPara
|
||||
@Override
|
||||
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
|
||||
ConstantExpressionInspection inspection = new ConstantExpressionInspection();
|
||||
inspection.skipIfContainsNonLiteralExpression = true;
|
||||
inspection.skipIfContainsReferenceExpression = true;
|
||||
return new LocalInspectionTool[]{inspection};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user