mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: remove redundant "Constant 'if' statement" inspection
This commit is contained in:
+2
-7
@@ -115,12 +115,7 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
|
||||
if (isAvailable()) {
|
||||
doSimplify();
|
||||
}
|
||||
}
|
||||
|
||||
public void doSimplify() {
|
||||
if (!isAvailable()) return;
|
||||
PsiExpression subExpression = getSubExpression();
|
||||
if (subExpression == null) return;
|
||||
if (shouldExtractSideEffect()) {
|
||||
@@ -144,7 +139,7 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
|
||||
return;
|
||||
}
|
||||
}
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(subExpression.getProject());
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
final PsiExpression constExpression = factory.createExpressionFromText(Boolean.toString(mySubExpressionValue), subExpression);
|
||||
PsiExpression expression = (PsiExpression)subExpression.replace(constExpression);
|
||||
while (expression.getParent() instanceof PsiExpression) {
|
||||
|
||||
-2
@@ -663,7 +663,6 @@ c.style.array.declaration.display.name=C-style array declaration
|
||||
final.method.in.final.class.display.name='final' method in 'final' class
|
||||
extends.annotation.display.name=Class extends annotation interface
|
||||
naked.notify.display.name='notify()' or 'notifyAll()' without corresponding state change
|
||||
constant.if.statement.display.name=Constant 'if' statement
|
||||
switch.statement.density.display.name='switch' statement with too low of a branch density
|
||||
switch.statement.with.too.few.branches.display.name='switch' statement with too few branches
|
||||
upper.case.field.name.not.constant.display.name=Non-constant field with upper-case name
|
||||
@@ -956,7 +955,6 @@ non.final.field.of.exception.problem.descriptor=Non-final field <code>#ref</code
|
||||
unnecessary.label.on.break.statement.problem.descriptor=Unnecessary label on <code>#ref</code> statement #loc
|
||||
unnecessary.label.on.continue.statement.problem.descriptor=Unnecessary label on <code>#ref</code> statement #loc
|
||||
trivial.if.problem.descriptor=<code>#ref</code> statement can be simplified #loc
|
||||
constant.if.statement.problem.descriptor=<code>#ref</code> statement can be simplified #loc
|
||||
unnecessary.parentheses.problem.descriptor=Parentheses around <code>#ref</code> are unnecessary #loc
|
||||
unnecessary.local.variable.problem.descriptor=Local variable <code>#ref</code> is redundant #loc
|
||||
unnecessary.this.problem.descriptor=<code>#ref</code> is unnecessary in this context #loc
|
||||
|
||||
@@ -625,9 +625,6 @@
|
||||
key="constant.conditional.expression.display.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.control.flow.issues" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.siyeh.ig.controlflow.ConstantConditionalExpressionInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="ConstantIfStatement" bundle="com.siyeh.InspectionGadgetsBundle" key="constant.if.statement.display.name"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.control.flow.issues" enabledByDefault="true"
|
||||
level="WARNING" implementationClass="com.siyeh.ig.controlflow.ConstantIfStatementInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="ContinueStatement" bundle="com.siyeh.InspectionGadgetsBundle" key="continue.statement.display.name"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.control.flow.issues" enabledByDefault="false"
|
||||
level="WARNING" implementationClass="com.siyeh.ig.controlflow.ContinueStatementInspection"/>
|
||||
|
||||
-102
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
package com.siyeh.ig.controlflow;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpressionFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiIfStatement;
|
||||
import com.intellij.psi.PsiStatement;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.psiutils.BoolUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ConstantIfStatementInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"constant.if.statement.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabledByDefault() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"constant.if.statement.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new ConstantIfStatementVisitor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new ConstantIfStatementFix();
|
||||
}
|
||||
|
||||
private static class ConstantIfStatementFix extends InspectionGadgetsFix {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"constant.conditional.expression.simplify.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
PsiElement ifKeyword = descriptor.getPsiElement();
|
||||
PsiExpression condition = Objects.requireNonNull(((PsiIfStatement)ifKeyword.getParent()).getCondition());
|
||||
new SimplifyBooleanExpressionFix(condition, BoolUtils.isTrue(condition)).doSimplify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ConstantIfStatementVisitor
|
||||
extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitIfStatement(PsiIfStatement statement) {
|
||||
super.visitIfStatement(statement);
|
||||
final PsiExpression condition = statement.getCondition();
|
||||
if (condition == null) {
|
||||
return;
|
||||
}
|
||||
final PsiStatement thenBranch = statement.getThenBranch();
|
||||
if (thenBranch == null) {
|
||||
return;
|
||||
}
|
||||
if (BoolUtils.isTrue(condition) || BoolUtils.isFalse(condition)) {
|
||||
registerStatementError(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
Reports <b>if</b> statements of the form
|
||||
<b>if(true)...</b> or <b>if(false)...</b>.
|
||||
These statements sometimes occur due to automatic refactorings, and may obviously
|
||||
be simplified.
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
class Test {
|
||||
{
|
||||
// this comment will be deleted.
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
class Test {
|
||||
{
|
||||
i<caret>f (true) {
|
||||
// this comment will be deleted.
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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.controlflow;
|
||||
|
||||
import com.intellij.codeInspection.InspectionProfileEntry;
|
||||
import com.siyeh.ig.LightInspectionTestCase;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
class ConstantIfStatementInspectionTest extends LightInspectionTestCase {
|
||||
|
||||
void testParentheses() {
|
||||
doStatementTest("""/*'if' statement can be simplified*/if/**/ (((false))) {
|
||||
System.out.println(1);
|
||||
} else {
|
||||
System.out.println(2);
|
||||
}""");
|
||||
}
|
||||
|
||||
void "test remove following statements when body always returns via break"() {
|
||||
myFixture.configureByText"a.java", """
|
||||
class Foo {
|
||||
void f(){
|
||||
while (true) {
|
||||
<warning descr="'if' statement can be simplified">i<caret>f</warning> (true) {
|
||||
break;//comment
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
myFixture.enableInspections(getInspection())
|
||||
myFixture.checkHighlighting()
|
||||
myFixture.launchAction(myFixture.findSingleIntention("Simplify"))
|
||||
myFixture.checkResult """
|
||||
class Foo {
|
||||
void f(){
|
||||
while (true) {
|
||||
break;//comment
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InspectionProfileEntry getInspection() {
|
||||
return new ConstantIfStatementInspection();
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2013 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.
|
||||
*/
|
||||
package com.siyeh.ig.fixes.controlflow;
|
||||
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.IGQuickFixesTestCase;
|
||||
import com.siyeh.ig.controlflow.ConstantIfStatementInspection;
|
||||
|
||||
public class ConstantIfFixTest extends IGQuickFixesTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.enableInspections(new ConstantIfStatementInspection());
|
||||
myRelativePath = "controlflow/constant_if";
|
||||
myDefaultHint = InspectionGadgetsBundle.message("constant.conditional.expression.simplify.quickfix");
|
||||
}
|
||||
|
||||
public void testComment() { doTest(); }
|
||||
}
|
||||
-1
@@ -17,7 +17,6 @@ package com.siyeh.ig.fixes.controlflow;
|
||||
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.IGQuickFixesTestCase;
|
||||
import com.siyeh.ig.controlflow.ConstantIfStatementInspection;
|
||||
import com.siyeh.ig.controlflow.SimplifiableEqualsExpressionInspection;
|
||||
|
||||
public class SimplifiableEqualsExpressionFixTest extends IGQuickFixesTestCase {
|
||||
|
||||
Reference in New Issue
Block a user