GeezerVison is showing github.com — C++: Exclude all parenthesized CommaExprs. · github/codeql@6490333
File treeExpand file treeCollapse file tree Open diff view settings src/Best Practices/Likely Errors test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,14 @@ predicate isInDecltypeOrSizeof(CommaExpr ce) { | 36 | 36 | ce.getParent*() = any(Decltype d).getExpr() | 37 | 37 | } | 38 | 38 |
| | 39 | +predicate isParenthesized(CommaExpr ce) { | | 40 | + isInLoopHead(ce) | | 41 | + or | | 42 | + isInDecltypeOrSizeof(ce) | | 43 | + or | | 44 | + ce.getParent*().(Expr).isParenthesised() | | 45 | +} | | 46 | + | 39 | 47 | from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc | 40 | 48 | where | 41 | 49 | ce.fromSource() and | | 44 | 52 | right = normalizeExpr(ce.getRightOperand()) and | 45 | 53 | leftLoc = left.getLocation() and | 46 | 54 | 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 | 49 | 56 | leftLoc.getEndLine() < rightLoc.getStartLine() and | 50 | 57 | leftLoc.getStartColumn() > rightLoc.getStartColumn() | 51 | 58 | select right, "The indentation level may be misleading (for some tab sizes)." |
Collapse file Copy file name to clipboardExpand all lines: cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/test.cpp +22-11Lines changed: 22 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,35 +44,46 @@ int Foo::test(int (*baz)(int)) | 44 | 44 | (void)i, // BAD | 45 | 45 | (void)j; | 46 | 46 |
| | 47 | + if (1) FOO(i), | | 48 | + (void)x.foo(j); // BAD | | 49 | + | 47 | 50 | // Parenthesized comma (borderline example): | 48 | 51 |
| 49 | 52 | foo(i++), j++; // GOOD | 50 | 53 | (foo(i++), j++); // GOOD | 51 | 54 | (foo(i++), // GOOD | 52 | 55 | j++); | 53 | 56 | (foo(i++), | 54 | | - j++); // BAD (?) | | 57 | + foo(i++), | | 58 | + j++, // GOOD (?) -- Currently explicitly excluded | | 59 | + j++); | 55 | 60 |
| 56 | 61 | x.foo(i++), j++; // GOOD | 57 | 62 | (x.foo(i++), j++); // GOOD | 58 | 63 | (x.foo(i++), // GOOD | 59 | 64 | j++); | 60 | 65 | (x.foo(i++), | 61 | | - j++); // BAD (?) | | 66 | + x.foo(i++), | | 67 | + j++, // GOOD (?) -- Currently explicitly excluded | | 68 | + j++); | 62 | 69 |
| 63 | 70 | FOO(i++), j++; // GOOD | 64 | 71 | (FOO(i++), j++); // GOOD | 65 | 72 | (FOO(i++), // GOOD | 66 | 73 | j++); | 67 | 74 | (FOO(i++), | 68 | | - j++); // BAD (?) | | 75 | + FOO(i++), | | 76 | + j++, // GOOD (?) -- Currently explicitly excluded | | 77 | + j++); | 69 | 78 |
| 70 | 79 | (void)(i++), j++; // GOOD | 71 | 80 | ((void)(i++), j++); // GOOD | 72 | 81 | ((void)(i++), // GOOD | 73 | 82 | j++); | 74 | 83 | ((void)(i++), | 75 | | - j++); // BAD (?) | | 84 | + (void)(i++), | | 85 | + j++, // GOOD (?) -- Currently explicitly excluded | | 86 | + j++); | 76 | 87 |
| 77 | 88 | // Comma in argument list doesn't count: | 78 | 89 |
| @@ -102,7 +113,7 @@ int Foo::test(int (*baz)(int)) | 102 | 113 | j++); | 103 | 114 |
| 104 | 115 | 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 | 106 | 117 |
| 107 | 118 | // Comma in loops | 108 | 119 |
| @@ -128,10 +139,10 @@ int Foo::test(int (*baz)(int)) | 128 | 139 |
| 129 | 140 | // Mixed tabs and spaces (ugly case): | 130 | 141 |
| 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. | 132 | 143 | j = 0; | 133 | 144 | 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. | 135 | 146 | j++); | 136 | 147 |
| 137 | 148 | if (i) | @@ -140,13 +151,13 @@ int Foo::test(int (*baz)(int)) | 140 | 151 |
| 141 | 152 | // LHS ends on same line RHS begins on: | 142 | 153 |
| 143 | | - int k1 = (foo( | | 154 | + if (1) foo( | 144 | 155 | i++ | 145 | | - ), j++); // GOOD? [FALSE POSITIVE] | | 156 | + ), j++; // GOOD? [FALSE POSITIVE] | 146 | 157 |
| 147 | | - int k2 = (baz( | | 158 | + if (1) baz( | 148 | 159 | i++ | 149 | | - ), j++); // GOOD when it's a function-pointer call!? | | 160 | + ), j++; // GOOD... when calling a function pointer..!? | 150 | 161 |
| 151 | 162 | // Weird cases: | 152 | 163 |
| |
|