mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-90631 (Unclear Binary Expression and Unnecessary Parentheses inspections conflict)
This commit is contained in:
+23
-29
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 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.
|
||||
@@ -36,34 +36,29 @@ public class UnnecessaryParenthesesInspection extends BaseInspection {
|
||||
|
||||
@SuppressWarnings({"PublicField"})
|
||||
public boolean ignoreParenthesesOnConditionals = false;
|
||||
|
||||
@SuppressWarnings("PublicField")
|
||||
public boolean ignoreParenthesesOnLambdaParameter = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"unnecessary.parentheses.display.name");
|
||||
return InspectionGadgetsBundle.message("unnecessary.parentheses.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"unnecessary.parentheses.problem.descriptor");
|
||||
return InspectionGadgetsBundle.message("unnecessary.parentheses.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
final MultipleCheckboxOptionsPanel optionsPanel =
|
||||
new MultipleCheckboxOptionsPanel(this);
|
||||
optionsPanel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"unnecessary.parentheses.option"),
|
||||
"ignoreClarifyingParentheses");
|
||||
optionsPanel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"unnecessary.parentheses.conditional.option"),
|
||||
final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this);
|
||||
optionsPanel.addCheckbox(InspectionGadgetsBundle.message("unnecessary.parentheses.option"), "ignoreClarifyingParentheses");
|
||||
optionsPanel.addCheckbox(InspectionGadgetsBundle.message("unnecessary.parentheses.conditional.option"),
|
||||
"ignoreParenthesesOnConditionals");
|
||||
optionsPanel.addCheckbox("Ignore parentheses around single no formal type lambda parameter",
|
||||
"ignoreParenthesesOnLambdaParameter");
|
||||
optionsPanel.addCheckbox("Ignore parentheses around single no formal type lambda parameter", "ignoreParenthesesOnLambdaParameter");
|
||||
return optionsPanel;
|
||||
}
|
||||
|
||||
@@ -112,26 +107,28 @@ public class UnnecessaryParenthesesInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitParenthesizedExpression(
|
||||
PsiParenthesizedExpression expression) {
|
||||
public void visitParenthesizedExpression(PsiParenthesizedExpression expression) {
|
||||
final PsiElement parent = expression.getParent();
|
||||
final PsiExpression child = expression.getExpression();
|
||||
if (child == null) {
|
||||
return;
|
||||
}
|
||||
if (!(parent instanceof PsiExpression) ||
|
||||
parent instanceof PsiParenthesizedExpression) {
|
||||
if (!(parent instanceof PsiExpression) || parent instanceof PsiParenthesizedExpression) {
|
||||
registerError(expression);
|
||||
return;
|
||||
}
|
||||
final int parentPrecedence =
|
||||
ParenthesesUtils.getPrecedence((PsiExpression)parent);
|
||||
final int parentPrecedence = ParenthesesUtils.getPrecedence((PsiExpression)parent);
|
||||
final int childPrecedence = ParenthesesUtils.getPrecedence(child);
|
||||
if (parentPrecedence > childPrecedence) {
|
||||
if (ignoreClarifyingParentheses) {
|
||||
if (parent instanceof PsiPolyadicExpression &&
|
||||
child instanceof PsiPolyadicExpression) {
|
||||
return;
|
||||
if (child instanceof PsiPolyadicExpression) {
|
||||
if (parent instanceof PsiPolyadicExpression) {
|
||||
return;
|
||||
} else if (parent instanceof PsiConditionalExpression) {
|
||||
return;
|
||||
} else if (parent instanceof PsiInstanceOfExpression) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (child instanceof PsiInstanceOfExpression) {
|
||||
return;
|
||||
@@ -139,10 +136,8 @@ public class UnnecessaryParenthesesInspection extends BaseInspection {
|
||||
}
|
||||
if (ignoreParenthesesOnConditionals) {
|
||||
if (parent instanceof PsiConditionalExpression) {
|
||||
final PsiConditionalExpression conditionalExpression =
|
||||
(PsiConditionalExpression)parent;
|
||||
final PsiExpression condition =
|
||||
conditionalExpression.getCondition();
|
||||
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)parent;
|
||||
final PsiExpression condition = conditionalExpression.getCondition();
|
||||
if (expression == condition) {
|
||||
return;
|
||||
}
|
||||
@@ -152,8 +147,7 @@ public class UnnecessaryParenthesesInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
if (parentPrecedence == childPrecedence) {
|
||||
if (!ParenthesesUtils.areParenthesesNeeded(expression,
|
||||
ignoreClarifyingParentheses)) {
|
||||
if (!ParenthesesUtils.areParenthesesNeeded(expression, ignoreClarifyingParentheses)) {
|
||||
registerError(expression);
|
||||
return;
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.siyeh.igtest.style.clarifying_parentheses;
|
||||
|
||||
class UnnecessaryParentheses {
|
||||
|
||||
String oldValue;
|
||||
boolean update(UnnecessaryParentheses that) {
|
||||
final boolean c = ("" + "asdf") instanceof String;
|
||||
boolean b = true && (that instanceof Object);
|
||||
if ((oldValue != null) ? !oldValue.equals(that.oldValue) : (that.oldValue != null)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
|
||||
</problems>
|
||||
+7
-2
@@ -8,8 +8,7 @@ import com.siyeh.ig.IGInspectionTestCase;
|
||||
public class UnnecessaryParenthesesInspectionTest extends IGInspectionTestCase {
|
||||
|
||||
public void test() throws Exception {
|
||||
final UnnecessaryParenthesesInspection inspection =
|
||||
new UnnecessaryParenthesesInspection();
|
||||
final UnnecessaryParenthesesInspection inspection = new UnnecessaryParenthesesInspection();
|
||||
inspection.ignoreParenthesesOnConditionals = true;
|
||||
final LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(getProject());
|
||||
final LanguageLevel level = levelProjectExtension.getLanguageLevel();
|
||||
@@ -22,4 +21,10 @@ public class UnnecessaryParenthesesInspectionTest extends IGInspectionTestCase {
|
||||
levelProjectExtension.setLanguageLevel(level);
|
||||
}
|
||||
}
|
||||
|
||||
public void testClarifyingParentheses() {
|
||||
final UnnecessaryParenthesesInspection inspection = new UnnecessaryParenthesesInspection();
|
||||
inspection.ignoreClarifyingParentheses = true;
|
||||
doTest("com/siyeh/igtest/style/clarifying_parentheses", inspection);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user