Skip to content

Commit 3a86eea

Browse files
committed
Exclude non-trivial inherited expressions in RedundantParentheses
1 parent 6346662 commit 3a86eea

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Non-trivial `inherited` expressions are excluded in `RedundantParentheses`.
13+
1014
## [1.9.0] - 2024-09-03
1115

1216
### Added

delphi-checks/src/main/java/au/com/integradev/delphi/checks/RedundantParenthesesCheck.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
package au.com.integradev.delphi.checks;
2020

2121
import org.sonar.check.Rule;
22+
import org.sonar.plugins.communitydelphi.api.ast.ExpressionNode;
2223
import org.sonar.plugins.communitydelphi.api.ast.ParenthesizedExpressionNode;
2324
import org.sonar.plugins.communitydelphi.api.ast.PrimaryExpressionNode;
25+
import org.sonar.plugins.communitydelphi.api.ast.utils.ExpressionNodeUtils;
2426
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
2527
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
2628
import org.sonar.plugins.communitydelphi.api.reporting.QuickFix;
@@ -35,8 +37,7 @@ public class RedundantParenthesesCheck extends DelphiCheck {
3537
@Override
3638
public DelphiCheckContext visit(
3739
ParenthesizedExpressionNode expression, DelphiCheckContext context) {
38-
if (expression.getExpression() instanceof ParenthesizedExpressionNode
39-
|| expression.getExpression() instanceof PrimaryExpressionNode) {
40+
if (isRedundant(expression)) {
4041
context
4142
.newIssue()
4243
.onNode(expression.getChild(0))
@@ -50,4 +51,17 @@ public DelphiCheckContext visit(
5051
}
5152
return super.visit(expression, context);
5253
}
54+
55+
private static boolean isRedundant(ParenthesizedExpressionNode expression) {
56+
ExpressionNode parenthesized = expression.getExpression();
57+
if (parenthesized instanceof PrimaryExpressionNode) {
58+
return !isNonTrivialInherited(parenthesized);
59+
}
60+
return parenthesized instanceof ParenthesizedExpressionNode;
61+
}
62+
63+
private static boolean isNonTrivialInherited(ExpressionNode expression) {
64+
return ExpressionNodeUtils.isInherited(expression)
65+
&& !ExpressionNodeUtils.isBareInherited(expression);
66+
}
5367
}

delphi-checks/src/test/java/au/com/integradev/delphi/checks/RedundantParenthesesCheckTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ void testParenthesesOnUnaryExpressionShouldNotAddIssue() {
8888
.verifyNoIssues();
8989
}
9090

91+
@Test
92+
void testParenthesesOnNonTrivialInheritedExpressionShouldAddIssue() {
93+
CheckVerifier.newVerifier()
94+
.withCheck(new RedundantParenthesesCheck())
95+
.onFile(
96+
new DelphiTestUnitBuilder()
97+
.appendImpl("procedure Foo;")
98+
.appendImpl("begin")
99+
.appendImpl(" (inherited Bar);")
100+
.appendImpl("end;"))
101+
.verifyNoIssues();
102+
}
103+
91104
@Test
92105
void testParenthesesOnParenthesizedExpressionShouldAddIssue() {
93106
CheckVerifier.newVerifier()
@@ -121,4 +134,19 @@ void testParenthesesOnPrimaryExpressionShouldAddIssue() {
121134
.appendImpl("end;"))
122135
.verifyIssues();
123136
}
137+
138+
@Test
139+
void testParenthesesOnBareInheritedExpressionShouldAddIssue() {
140+
CheckVerifier.newVerifier()
141+
.withCheck(new RedundantParenthesesCheck())
142+
.onFile(
143+
new DelphiTestUnitBuilder()
144+
.appendImpl("procedure Foo;")
145+
.appendImpl("begin")
146+
.appendImpl(" // Fix@[+2:2 to +2:3] <<>>")
147+
.appendImpl(" // Fix@[+1:12 to +1:13] <<>>")
148+
.appendImpl(" (inherited); // Noncompliant")
149+
.appendImpl("end;"))
150+
.verifyIssues();
151+
}
124152
}

0 commit comments

Comments
 (0)