mirror of
https://github.com/github/codeql.git
synced 2025-12-22 03:36:30 +01:00
C++: Reduce FPs by excluding all commas in loop heads
This leads to a 50% reduction of alerts in MRVA 1000.
This commit is contained in:
@@ -20,6 +20,12 @@ Expr normalizeExpr(Expr e) {
|
|||||||
else result = e
|
else result = e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
predicate isInLoopHead(CommaExpr ce) {
|
||||||
|
ce.getParent*() = [any(Loop l).getCondition(), any(ForStmt f).getUpdate()]
|
||||||
|
or
|
||||||
|
ce.getEnclosingStmt() = any(ForStmt f).getInitialization()
|
||||||
|
}
|
||||||
|
|
||||||
from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
|
from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
|
||||||
where
|
where
|
||||||
ce.fromSource() and
|
ce.fromSource() and
|
||||||
@@ -28,6 +34,7 @@ where
|
|||||||
right = normalizeExpr(ce.getRightOperand()) and
|
right = normalizeExpr(ce.getRightOperand()) and
|
||||||
leftLoc = left.getLocation() and
|
leftLoc = left.getLocation() and
|
||||||
rightLoc = right.getLocation() and
|
rightLoc = right.getLocation() and
|
||||||
|
not isInLoopHead(ce) and // HACK to reduce FPs in loop heads; assumption: unlikely to be misread due to '(', ')' delimiters
|
||||||
leftLoc.getEndLine() < rightLoc.getStartLine() and
|
leftLoc.getEndLine() < rightLoc.getStartLine() and
|
||||||
leftLoc.getStartColumn() > rightLoc.getStartColumn()
|
leftLoc.getStartColumn() > rightLoc.getStartColumn()
|
||||||
select right, "The indentation level after the comma can be misleading (for some tab sizes)."
|
select right, "The indentation after the comma may be misleading (for some tab sizes)."
|
||||||
|
|||||||
@@ -86,6 +86,15 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
|
|||||||
i = j = i + j;
|
i = j = i + j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0, // GOOD? Currently ignoring loop heads.
|
||||||
|
j = 1;
|
||||||
|
i + j < 10;
|
||||||
|
i++, j++);
|
||||||
|
|
||||||
|
for (i = 0,
|
||||||
|
j = 1; i < 10; i += 2, // GOOD? Currently ignoring loop heads.
|
||||||
|
j++) {}
|
||||||
|
|
||||||
// Mixed tabs and spaces (ugly case):
|
// Mixed tabs and spaces (ugly case):
|
||||||
|
|
||||||
for (i = 0, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
|
for (i = 0, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
|
||||||
@@ -98,17 +107,6 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
|
|||||||
(void)i, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
|
(void)i, // GOOD if tab >= 4 spaces else BAD -- can't exclude w/o source code text :/
|
||||||
(void)j;
|
(void)j;
|
||||||
|
|
||||||
// One char difference (common but borderline):
|
|
||||||
|
|
||||||
for (i = 0, // GOOD? [FALSE POSITIVE] -- can't exclude w/o source code text :/
|
|
||||||
j = 1;
|
|
||||||
i + j < 10;
|
|
||||||
i++, j++);
|
|
||||||
|
|
||||||
for (i = 0,
|
|
||||||
j = 1; i < 10; i += 2, // GOOD? [FALSE POSITIVE] -- can't exclude w/o source code text :/
|
|
||||||
j++) {}
|
|
||||||
|
|
||||||
// LHS ends on same line RHS begins on:
|
// LHS ends on same line RHS begins on:
|
||||||
|
|
||||||
int k = (foo(
|
int k = (foo(
|
||||||
|
|||||||
Reference in New Issue
Block a user