PointlessBooleanExpression: tests fixed; now warning issued on fields as well (after 6e87cb9bfd)

This commit is contained in:
Tagir Valeev
2017-09-13 11:09:28 +07:00
parent 308e007dd8
commit cb8a02e657
4 changed files with 79 additions and 6 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2017 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.
*/
class C {
double sideEffect(int x) {
System.out.println("Side effect"+x);
return Math.random();
}
boolean field;
{
sideEffect(4);
field = false;
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2017 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.
*/
class C {
double sideEffect(int x) {
System.out.println("Side effect"+x);
return Math.random();
}
boolean field = sideEffect(4) > 0.5 &<caret>& false;
}
@@ -34,12 +34,31 @@ class PointlessBooleanExpression {
return Math.random() > 0.5;
}
// side-effect cannot be extracted from field declaration
boolean field = sideEffect() && false;
boolean field1 = false & sideEffect();
// no side-effect extraction necessary
boolean field2 = <warning descr="'sideEffect() && true' can be simplified to 'sideEffect()'">sideEffect() && true</warning>;
boolean field3 = <warning descr="'false && sideEffect()' can be simplified to 'false'">false && sideEffect()</warning>;
class X {
X(boolean b) {}
}
class Y extends X {
Y(int i) {
// side-effect cannot be extracted from super call
super(sideEffect() && false);
}
Y(long l) {
// side-effect cannot be extracted from super call
super(false & sideEffect());
}
Y(double d) {
// no side-effect extraction necessary
super(<warning descr="'sideEffect() && true' can be simplified to 'sideEffect()'">sideEffect() && true</warning>);
}
Y(float f) {
// no side-effect extraction necessary
super(<warning descr="'false && sideEffect()' can be simplified to 'false'">false && sideEffect()</warning>);
}
}
void method() {
if(<warning descr="'sideEffect() && false' can be simplified to 'false'">sideEffect() && false</warning>) {
@@ -35,4 +35,7 @@ public class PointlessBooleanExpressionFixTest extends IGQuickFixesTestCase {
public void testSideEffects() {
doTest(InspectionGadgetsBundle.message("constant.conditional.expression.simplify.quickfix.sideEffect"));
}
public void testSideEffectsField() {
doTest(InspectionGadgetsBundle.message("constant.conditional.expression.simplify.quickfix.sideEffect"));
}
}