Skip to content

Commit 6490333

Browse files
committed
C++: Exclude all parenthesized CommaExprs.
1 parent 909b36a commit 6490333

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ predicate isInDecltypeOrSizeof(CommaExpr ce) {
3636
ce.getParent*() = any(Decltype d).getExpr()
3737
}
3838

39+
predicate isParenthesized(CommaExpr ce) {
40+
isInLoopHead(ce)
41+
or
42+
isInDecltypeOrSizeof(ce)
43+
or
44+
ce.getParent*().(Expr).isParenthesised()
45+
}
46+
3947
from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
4048
where
4149
ce.fromSource() and
@@ -44,8 +52,7 @@ where
4452
right = normalizeExpr(ce.getRightOperand()) and
4553
leftLoc = left.getLocation() and
4654
rightLoc = right.getLocation() and
47-
not isInLoopHead(ce) and // <- HACK to reduce FPs in loop heads; assumption: unlikely to be misread due to '(', ')' delimiters
48-
not isInDecltypeOrSizeof(ce) and // <- Removes arguable FPs since, like function calls (and loop heads), these Exprs have clear delimiters.
55+
not isParenthesized(ce) and
4956
leftLoc.getEndLine() < rightLoc.getStartLine() and
5057
leftLoc.getStartColumn() > rightLoc.getStartColumn()
5158
select right, "The indentation level may be misleading (for some tab sizes)."

cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/test.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,46 @@ int Foo::test(int (*baz)(int))
4444
(void)i, // BAD
4545
(void)j;
4646

47+
if (1) FOO(i),
48+
(void)x.foo(j); // BAD
49+
4750
// Parenthesized comma (borderline example):
4851

4952
foo(i++), j++; // GOOD
5053
(foo(i++), j++); // GOOD
5154
(foo(i++), // GOOD
5255
j++);
5356
(foo(i++),
54-
j++); // BAD (?)
57+
foo(i++),
58+
j++, // GOOD (?) -- Currently explicitly excluded
59+
j++);
5560

5661
x.foo(i++), j++; // GOOD
5762
(x.foo(i++), j++); // GOOD
5863
(x.foo(i++), // GOOD
5964
j++);
6065
(x.foo(i++),
61-
j++); // BAD (?)
66+
x.foo(i++),
67+
j++, // GOOD (?) -- Currently explicitly excluded
68+
j++);
6269

6370
FOO(i++), j++; // GOOD
6471
(FOO(i++), j++); // GOOD
6572
(FOO(i++), // GOOD
6673
j++);
6774
(FOO(i++),
68-
j++); // BAD (?)
75+
FOO(i++),
76+
j++, // GOOD (?) -- Currently explicitly excluded
77+
j++);
6978

7079
(void)(i++), j++; // GOOD
7180
((void)(i++), j++); // GOOD
7281
((void)(i++), // GOOD
7382
j++);
7483
((void)(i++),
75-
j++); // BAD (?)
84+
(void)(i++),
85+
j++, // GOOD (?) -- Currently explicitly excluded
86+
j++);
7687

7788
// Comma in argument list doesn't count:
7889

@@ -102,7 +113,7 @@ int Foo::test(int (*baz)(int))
102113
j++);
103114

104115
BAZ("%d %d\n", i,
105-
j); // GOOD [FALSE POSITIVE] -- but can only be excluded by excluding all parenthesized commas (which sounds like a good idea actually)
116+
j); // GOOD -- Currently explicitly excluded
106117

107118
// Comma in loops
108119

@@ -128,10 +139,10 @@ int Foo::test(int (*baz)(int))
128139

129140
// Mixed tabs and spaces (ugly case):
130141

131-
for (i = 0, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
142+
for (i = 0, // GOOD if tab >= 4 spaces else BAD -- Currently ignoring loop heads.
132143
j = 0;
133144
i + j < 10;
134-
i++, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
145+
i++, // GOOD if tab >= 4 spaces else BAD -- Currently ignoring loop heads.
135146
j++);
136147

137148
if (i)
@@ -140,13 +151,13 @@ int Foo::test(int (*baz)(int))
140151

141152
// LHS ends on same line RHS begins on:
142153

143-
int k1 = (foo(
154+
if (1) foo(
144155
i++
145-
), j++); // GOOD? [FALSE POSITIVE]
156+
), j++; // GOOD? [FALSE POSITIVE]
146157

147-
int k2 = (baz(
158+
if (1) baz(
148159
i++
149-
), j++); // GOOD when it's a function-pointer call!?
160+
), j++; // GOOD... when calling a function pointer..!?
150161

151162
// Weird cases:
152163

0 commit comments

Comments
 (0)