Skip to content

Commit e7c1fad

Browse files
committed
C++: Fix member-call- and C-cast-related FPs
1 parent cacf788 commit e7c1fad

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@
99
*/
1010

1111
import cpp
12+
import semmle.code.cpp.commons.Exclusions
1213

13-
from CommaExpr ce
14+
Expr normalizeExpr(Expr e) {
15+
if exists(e.(Call).getQualifier())
16+
then result = normalizeExpr(e.(Call).getQualifier())
17+
else
18+
if e.hasExplicitConversion()
19+
then result = normalizeExpr(e.getFullyConverted())
20+
else result = e
21+
}
22+
23+
from CommaExpr ce, Expr left, Expr right, int leftStartColumn, int rightStartColumn
1424
where
1525
ce.fromSource() and
16-
not exists(MacroInvocation me | ce = me.getAnAffectedElement()) and
17-
ce.getLeftOperand().getLocation().getStartColumn() >
18-
ce.getRightOperand().getLocation().getStartColumn()
19-
select ce, "Comma before misleading indentation."
26+
not isFromMacroDefinition(ce) and
27+
left = normalizeExpr(ce.getLeftOperand()) and
28+
right = normalizeExpr(ce.getRightOperand()) and
29+
leftStartColumn = left.getLocation().getStartColumn() and
30+
rightStartColumn = right.getLocation().getStartColumn() and
31+
leftStartColumn > rightStartColumn
32+
select right, "The indentation level after the comma can be misleading (for some tab sizes)."

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
3636

3737
x.foo(i++), j++; // GOOD
3838
(x.foo(i++), j++); // GOOD
39-
(x.foo(i++), // GOOD [FALSE POSITIVE]
39+
(x.foo(i++), // GOOD
4040
j++);
4141
(x.foo(i++),
4242
j++); // BAD (?)
@@ -50,7 +50,7 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
5050

5151
(void)(i++), j++; // GOOD
5252
((void)(i++), j++); // GOOD
53-
((void)(i++), // GOOD [FALSE POSITIVE]
53+
((void)(i++), // GOOD
5454
j++);
5555
((void)(i++),
5656
j++); // BAD (?)
@@ -73,9 +73,9 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
7373
BAR(i++,
7474
j++); // GOOD: common pattern and unlikely to be misread.
7575

76-
using T = decltype(x.foo(i++), // GOOD [FALSE POSITIVE]
76+
using T = decltype(x.foo(i++), // GOOD
7777
j++);
78-
(void)sizeof(x.foo(i++), // GOOD [FALSE POSITIVE]
78+
(void)sizeof(x.foo(i++), // GOOD
7979
j++);
8080

8181
// Comma in loops

0 commit comments

Comments
 (0)