mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-75717 ("Referenced checked for null not used inside if" false positive)
This commit is contained in:
+2
-1
@@ -1731,7 +1731,8 @@ field.may.be.final.display.name=Field may be 'final'
|
||||
field.may.be.final.problem.descriptor=Field <code>#ref</code> may be 'final' #loc
|
||||
cast.that.loses.precision.option=Ignore casts from int to char
|
||||
variable.not.used.inside.if.display.name=Reference checked for 'null' is not used inside 'if'
|
||||
variable.not.used.inside.if.problem.descriptor=<code>#ref</code> is not used inside if #loc
|
||||
variable.not.used.inside.if.problem.descriptor=<code>#ref</code> checked for 'null' is not used inside 'if' #loc
|
||||
variable.not.used.inside.conditional.problem.descriptor=<code>#ref</code> checked for 'null' is not used inside conditional #loc
|
||||
if.may.be.conditional.display.name='if' statement could be replaced with conditional expression
|
||||
if.may.be.conditional.problem.descriptor=<code>#ref</code> could be replaced with conditional expression #loc
|
||||
if.may.be.conditional.quickfix=Replace with conditional expression
|
||||
|
||||
+27
-18
@@ -31,15 +31,19 @@ public class VariableNotUsedInsideIfInspection extends BaseInspection {
|
||||
@Nls
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"variable.not.used.inside.if.display.name");
|
||||
return InspectionGadgetsBundle.message("variable.not.used.inside.if.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"variable.not.used.inside.if.problem.descriptor");
|
||||
final boolean isIf = ((Boolean)infos[0]).booleanValue();
|
||||
if (isIf) {
|
||||
return InspectionGadgetsBundle.message("variable.not.used.inside.if.problem.descriptor");
|
||||
}
|
||||
else {
|
||||
return InspectionGadgetsBundle.message("variable.not.used.inside.conditional.problem.descriptor");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -63,10 +67,14 @@ public class VariableNotUsedInsideIfInspection extends BaseInspection {
|
||||
}
|
||||
final IElementType tokenType = binaryExpression.getOperationTokenType();
|
||||
if (tokenType == JavaTokenType.EQEQ) {
|
||||
checkVariableUsage(referenceExpression, expression.getThenExpression(), expression.getElseExpression());
|
||||
if (checkVariableUsage(referenceExpression, expression.getThenExpression(), expression.getElseExpression())) {
|
||||
registerError(referenceExpression, Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
else if (tokenType == JavaTokenType.NE) {
|
||||
checkVariableUsage(referenceExpression, expression.getElseExpression(), expression.getThenExpression());
|
||||
if (checkVariableUsage(referenceExpression, expression.getElseExpression(), expression.getThenExpression())) {
|
||||
registerError(referenceExpression, Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,29 +92,30 @@ public class VariableNotUsedInsideIfInspection extends BaseInspection {
|
||||
}
|
||||
final IElementType tokenType = binaryExpression.getOperationTokenType();
|
||||
if (tokenType == JavaTokenType.EQEQ) {
|
||||
checkVariableUsage(referenceExpression, statement.getThenBranch(), statement.getElseBranch());
|
||||
if (checkVariableUsage(referenceExpression, statement.getThenBranch(), statement.getElseBranch())) {
|
||||
registerError(referenceExpression, Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
else if (tokenType == JavaTokenType.NE) {
|
||||
checkVariableUsage(referenceExpression, statement.getElseBranch(), statement.getThenBranch());
|
||||
if (checkVariableUsage(referenceExpression, statement.getElseBranch(), statement.getThenBranch())) {
|
||||
registerError(referenceExpression, Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkVariableUsage(PsiReferenceExpression referenceExpression, PsiElement thenContext, PsiElement elseContext) {
|
||||
if (thenContext == null) {
|
||||
return;
|
||||
}
|
||||
private boolean checkVariableUsage(PsiReferenceExpression referenceExpression, PsiElement thenContext, PsiElement elseContext) {
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiVariable)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
final PsiVariable variable = (PsiVariable)target;
|
||||
if (contextExits(thenContext) || VariableAccessUtils.variableIsAssigned(variable, thenContext)) {
|
||||
return;
|
||||
if (thenContext != null && (contextExits(thenContext) || VariableAccessUtils.variableIsAssigned(variable, thenContext))) {
|
||||
return false;
|
||||
}
|
||||
if (elseContext != null && (contextExits(elseContext) || VariableAccessUtils.variableIsUsed(variable, elseContext))) {
|
||||
return;
|
||||
if (elseContext == null || VariableAccessUtils.variableIsUsed(variable, elseContext)) {
|
||||
return false;
|
||||
}
|
||||
registerError(referenceExpression);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PsiReferenceExpression extractVariableReference(PsiBinaryExpression expression) {
|
||||
|
||||
+22
-3
@@ -27,10 +27,10 @@ public class VariableNotUsedInsideIf {
|
||||
}
|
||||
|
||||
void bat(String s) {
|
||||
if (s == null) {
|
||||
if (s != null) {
|
||||
System.out.println();
|
||||
}
|
||||
if (s != null) {
|
||||
if (s == null) {
|
||||
|
||||
} else {
|
||||
|
||||
@@ -38,8 +38,27 @@ public class VariableNotUsedInsideIf {
|
||||
}
|
||||
|
||||
void money(String s) {
|
||||
if ((s == null)) {
|
||||
if (((s) != (null))) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
void x(Integer x){
|
||||
if (x != null) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
int x(Integer x, int y){
|
||||
if (x != null) return y;//oops, wrong one
|
||||
return y;
|
||||
}
|
||||
|
||||
int conditional(Integer x) {
|
||||
return x == null ? 1 : someValue();
|
||||
}
|
||||
|
||||
private int someValue() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+21
@@ -20,4 +20,25 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Reference checked for 'null' is not used inside 'if'</problem_class>
|
||||
<description><code>s</code> is not used inside if #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>VariableNotUsedInsideIf.java</file>
|
||||
<line>47</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Reference checked for 'null' is not used inside 'if'</problem_class>
|
||||
<description><code>x</code> checked for 'null' is not used inside 'if' #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>VariableNotUsedInsideIf.java</file>
|
||||
<line>53</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Reference checked for 'null' is not used inside 'if'</problem_class>
|
||||
<description><code>x</code> checked for 'null' is not used inside 'if' #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>VariableNotUsedInsideIf.java</file>
|
||||
<line>58</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Reference checked for 'null' is not used inside 'if'</problem_class>
|
||||
<description><code>x</code> checked for 'null' is not used inside conditional #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
Reference in New Issue
Block a user