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:
Nora Dimitrijević 2022-09-28 19:38:41 +02:00
Родитель 823b0109f0
Коммит 592bc18a97
2 изменённых файлов: 17 добавлений и 12 удалений

Просмотреть файл

@ -20,6 +20,12 @@ Expr normalizeExpr(Expr 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
where
ce.fromSource() and
@ -28,6 +34,7 @@ where
right = normalizeExpr(ce.getRightOperand()) and
leftLoc = left.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.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;
}
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):
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)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:
int k = (foo(