From 17dcc983f51a93820dbd64819e9dd9d8d64f6b71 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 18 Feb 2014 12:32:28 +0100 Subject: [PATCH] IDEA-120799 (Quick-fix for "Unnecessary parentheses" changes expression semantics) --- .../src/com/siyeh/ig/psiutils/ParenthesesUtils.java | 7 ++++--- .../UnnecessaryParenthesesInspection.java | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java index bc44d6d44b79..29bc9dd6182a 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -505,15 +505,16 @@ public class ParenthesesUtils { } } final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType(); + final IElementType childOperator = childPolyadicExpression.getOperationTokenType(); if (ignoreClarifyingParentheses) { - final IElementType childOperator = childPolyadicExpression.getOperationTokenType(); if (!childOperator.equals(parentOperator)) { return true; } } final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands(); if (!PsiTreeUtil.isAncestor(parentOperands[0], expression, false)) { - if (!isCommutativeOperator(parentOperator)) { + if (!isAssociativeOperation(parentPolyadicExpression) || + JavaTokenType.DIV == childOperator || JavaTokenType.PERC == childOperator) { return true; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java index 70695c4e1fff..555bf464523e 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java @@ -112,4 +112,12 @@ public class UnnecessaryParenthesesInspection Object info = new Object[]{"abc"}; String s = (String)((Object[])info)[0]; } + + void zz() { + int a = 10; + int b = 20; + + final int i = a * ((b + 2) / 3); // no warn + final int j = a * ((b + 2) % 3); // no warn + } }