From 7978dc8298e9ad3ebff640489bfc60b8c2c8d193 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 29 Aug 2012 22:13:22 +0200 Subject: [PATCH] IDEA-90631 (Unclear Binary Expression and Unnecessary Parentheses inspections conflict) --- .../UnnecessaryParenthesesInspection.java | 52 ++++++++----------- .../UnnecessaryParentheses.java | 14 +++++ .../style/clarifying_parentheses/expected.xml | 4 ++ .../UnnecessaryParenthesesInspectionTest.java | 9 +++- 4 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/UnnecessaryParentheses.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/expected.xml diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryParenthesesInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryParenthesesInspection.java index a7db797d908a..8a6577083a46 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryParenthesesInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryParenthesesInspection.java @@ -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; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/UnnecessaryParentheses.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/UnnecessaryParentheses.java new file mode 100644 index 000000000000..2f7a798fe0bc --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/UnnecessaryParentheses.java @@ -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; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/expected.xml new file mode 100644 index 000000000000..e8f00e82629f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/clarifying_parentheses/expected.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java index 4fcc38e7e850..97ce053c04da 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java @@ -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); + } } \ No newline at end of file