From cb8a02e657252a00663defa59d30ef1e6fff8f95 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 13 Sep 2017 11:09:28 +0700 Subject: [PATCH] PointlessBooleanExpression: tests fixed; now warning issued on fields as well (after 6e87cb9bfd86) --- .../SideEffectsField.after.java | 28 +++++++++++++++++ .../pointlessboolean/SideEffectsField.java | 23 ++++++++++++++ .../PointlessBooleanExpression.java | 31 +++++++++++++++---- .../PointlessBooleanExpressionFixTest.java | 3 ++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.java diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.after.java new file mode 100644 index 000000000000..157f6c690f88 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.after.java @@ -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; + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.java new file mode 100644 index 000000000000..29e83094790b --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/SideEffectsField.java @@ -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 && false; +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java index 13414778edd8..ae688bed4c3c 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java @@ -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 = sideEffect() && true; - boolean field3 = false && sideEffect(); + 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(sideEffect() && true); + } + + Y(float f) { + // no side-effect extraction necessary + super(false && sideEffect()); + } + } void method() { if(sideEffect() && false) { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java index 98e970976c23..32b90a530cf0 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java @@ -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")); + } }