IDEA-158970 (Bug in Java pointless bitwise expression inspection)

This commit is contained in:
Bas Leijdekkers
2016-07-29 22:19:31 +02:00
parent 55eeef39b5
commit 5e01863b26
2 changed files with 11 additions and 3 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2016 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.EquivalenceChecker;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.SideEffectChecker;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -241,8 +242,8 @@ public class PointlessBitwiseExpressionInspection extends BaseInspection {
private boolean booleanExpressionIsPointless(PsiExpression[] operands) {
PsiExpression previousExpression = null;
for (PsiExpression operand : operands) {
if (isZero(operand) || isAllOnes(operand) || EquivalenceChecker.getCanonicalPsiEquivalence()
.expressionsAreEquivalent(previousExpression, operand)) {
if (isZero(operand) || isAllOnes(operand) || (EquivalenceChecker.getCanonicalPsiEquivalence()
.expressionsAreEquivalent(previousExpression, operand) && !SideEffectChecker.mayHaveSideEffects(operand))) {
return true;
}
previousExpression = operand;
@@ -1,5 +1,7 @@
package com.siyeh.igtest.bitwise.pointless_bitwise_expression;
import java.util.*;
public class PointlessBitwiseExpression {
private static final int ZERO = 0;
@@ -60,6 +62,11 @@ public class PointlessBitwiseExpression {
System.out.println(k);
}
public static void main5(String[] args) {
Random in = new Random();
System.out.println(in.nextInt() & in.nextInt());
}
void longExpressions(int i, int j) {
i = <warning descr="'j & 0 & 100' can be replaced with '0'">j & 0 & 100</warning>;
}