From 2cf9fb2e3a1d33dc6cc003af76ca12588f12520c Mon Sep 17 00:00:00 2001 From: Lucas <24826124+Luro02@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:45:51 +0100 Subject: [PATCH] Fix missing parens for `(string + string).contains(...)` (#6094) --- .../java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java | 2 +- .../spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 76af0925e80..b7e1ed269cf 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -431,7 +431,7 @@ private boolean shouldSetBracketAroundExpressionAndCast(CtExpression e) { return requiresBrackets == RoundBracketAnalyzer.EncloseInRoundBrackets.YES || !e.getTypeCasts().isEmpty(); } if (e.isParentInitialized() && e.getParent() instanceof CtTargetedExpression && ((CtTargetedExpression) e.getParent()).getTarget() == e) { - return e instanceof CtVariableRead && !e.getTypeCasts().isEmpty(); + return e instanceof CtVariableRead && !e.getTypeCasts().isEmpty() || e instanceof CtBinaryOperator; } } else if (!e.getTypeCasts().isEmpty()) { return true; diff --git a/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java b/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java index 094fe84ec05..45bcbf787f2 100644 --- a/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java +++ b/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java @@ -77,6 +77,7 @@ public class DefaultJavaPrettyPrinterTest { "(1 | 2) ^ 3", "((int) (1 + 2)) * 3", "(int) (int) (1 + 1)", + "(\"1\" + \"2\").contains(\"1\")", }) public void testParenOptimizationCorrectlyPrintsParenthesesForExpressions(String rawExpression) { // contract: When input expressions are minimally parenthesized, pretty-printed output