Skip to content

Commit de3da87

Browse files
committed
Target parenthesized primary expressions in RedundantParentheses
1 parent 31e9188 commit de3da87

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Improve type inference around unsigned integer literals.
1717
- Improve type inference around array constructors containing integer literals.
1818
- Improve type inference around real expressions.
19+
- Parentheses enclosing a primary expression are considered redundant in `RedundantParentheses`.
1920
- `out` parameters are treated as uninitialized at the start of a routine in
2021
`VariableInitialization`.
2122

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.sonar.check.Rule;
2222
import org.sonar.plugins.communitydelphi.api.ast.ParenthesizedExpressionNode;
23+
import org.sonar.plugins.communitydelphi.api.ast.PrimaryExpressionNode;
2324
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
2425
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
2526
import org.sonar.plugins.communitydelphi.api.reporting.QuickFix;
@@ -34,7 +35,8 @@ public class RedundantParenthesesCheck extends DelphiCheck {
3435
@Override
3536
public DelphiCheckContext visit(
3637
ParenthesizedExpressionNode expression, DelphiCheckContext context) {
37-
if (expression.getParent() instanceof ParenthesizedExpressionNode) {
38+
if (expression.getExpression() instanceof ParenthesizedExpressionNode
39+
|| expression.getExpression() instanceof PrimaryExpressionNode) {
3840
context
3941
.newIssue()
4042
.onNode(expression.getChild(0))

delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/RedundantParentheses.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<h2>Why is this an issue?</h2>
22
<p>
33
Parentheses should be used to clarify the intent behind a piece of code or enforce a desired
4-
order of operations. Redundant pairs of parentheses do neither of these things, making code more
5-
confusing and less readable.
4+
order of operations. Redundant parentheses do neither of these things, making code more confusing
5+
and less readable.
6+
</p>
7+
<p>
8+
This rule will suggest removing parentheses that wrap another parenthesized expression, or a
9+
primary expression (e.g. <code>'foo'</code> or <code>Bar.Baz(123)</code>). In both cases,
10+
parenthesizing the expression is syntactically meaningless without improving readability for a
11+
human.
612
</p>
713
<h2>How to fix it</h2>
814
<p>Remove the redundant parentheses:</p>

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

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
class RedundantParenthesesCheckTest {
2626
@Test
27-
void testNoParenthesesShouldNotAddIssue() {
27+
void testPrimaryExpressionShouldNotAddIssue() {
2828
CheckVerifier.newVerifier()
2929
.withCheck(new RedundantParenthesesCheck())
3030
.onFile(
@@ -37,29 +37,87 @@ void testNoParenthesesShouldNotAddIssue() {
3737
}
3838

3939
@Test
40-
void testParenthesesShouldNotAddIssue() {
40+
void testBinaryExpressionShouldNotAddIssue() {
4141
CheckVerifier.newVerifier()
4242
.withCheck(new RedundantParenthesesCheck())
4343
.onFile(
4444
new DelphiTestUnitBuilder()
4545
.appendImpl("function GetInteger: Integer;")
4646
.appendImpl("begin")
47-
.appendImpl(" Result := (123);")
47+
.appendImpl(" Result := 1 + 2;")
4848
.appendImpl("end;"))
4949
.verifyNoIssues();
5050
}
5151

5252
@Test
53-
void testRedundantParenthesesShouldAddIssue() {
53+
void testUnaryExpressionShouldNotAddIssue() {
5454
CheckVerifier.newVerifier()
5555
.withCheck(new RedundantParenthesesCheck())
5656
.onFile(
5757
new DelphiTestUnitBuilder()
5858
.appendImpl("function GetInteger: Integer;")
5959
.appendImpl("begin")
60-
.appendImpl(" // Fix@[+2:13 to +2:14] <<>>")
61-
.appendImpl(" // Fix@[+1:17 to +1:18] <<>>")
62-
.appendImpl(" Result := ((123)); // Noncompliant")
60+
.appendImpl(" Result := -123;")
61+
.appendImpl("end;"))
62+
.verifyNoIssues();
63+
}
64+
65+
@Test
66+
void testParenthesesOnBinaryExpressionShouldNotAddIssue() {
67+
CheckVerifier.newVerifier()
68+
.withCheck(new RedundantParenthesesCheck())
69+
.onFile(
70+
new DelphiTestUnitBuilder()
71+
.appendImpl("function GetInteger: Integer;")
72+
.appendImpl("begin")
73+
.appendImpl(" Result := (1 + 2);")
74+
.appendImpl("end;"))
75+
.verifyNoIssues();
76+
}
77+
78+
@Test
79+
void testParenthesesOnUnaryExpressionShouldNotAddIssue() {
80+
CheckVerifier.newVerifier()
81+
.withCheck(new RedundantParenthesesCheck())
82+
.onFile(
83+
new DelphiTestUnitBuilder()
84+
.appendImpl("function GetInteger: Integer;")
85+
.appendImpl("begin")
86+
.appendImpl(" Result := (-123);")
87+
.appendImpl("end;"))
88+
.verifyNoIssues();
89+
}
90+
91+
@Test
92+
void testParenthesesOnParenthesizedExpressionShouldAddIssue() {
93+
CheckVerifier.newVerifier()
94+
.withCheck(new RedundantParenthesesCheck())
95+
.onFile(
96+
new DelphiTestUnitBuilder()
97+
.appendImpl("function GetInteger: Integer;")
98+
.appendImpl("begin")
99+
.appendImpl(" // Fix@[+2:12 to +2:13] <<>>")
100+
.appendImpl(" // Fix@[+1:20 to +1:21] <<>>")
101+
.appendImpl(" Result := ((1 + 2)); // Noncompliant")
102+
.appendImpl("end;"))
103+
.verifyIssues();
104+
}
105+
106+
@Test
107+
void testParenthesesOnPrimaryExpressionShouldAddIssue() {
108+
CheckVerifier.newVerifier()
109+
.withCheck(new RedundantParenthesesCheck())
110+
.onFile(
111+
new DelphiTestUnitBuilder()
112+
.appendImpl("function GetInteger: Integer;")
113+
.appendImpl("begin")
114+
.appendImpl(" // Fix qf1@[+6:12 to +6:13] <<>>")
115+
.appendImpl(" // Fix qf2@[+5:13 to +5:14] <<>>")
116+
.appendImpl(" // Fix qf2@[+4:17 to +4:18] <<>>")
117+
.appendImpl(" // Fix qf1@[+3:18 to +3:19] <<>>")
118+
.appendImpl(" // Noncompliant@+2")
119+
.appendImpl(" // Noncompliant@+1")
120+
.appendImpl(" Result := ((123));")
63121
.appendImpl("end;"))
64122
.verifyIssues();
65123
}

0 commit comments

Comments
 (0)