Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c42b5f

Browse files
committed
C++: Relax commented-out code heuristic for }
I looked through a few hundred results from this query on lgtm.com and found that most of the FPs had to do with comment lines ending in `}`. This change should fix most of them, at the cost of very few false negatives. On Wireshark, this query goes from 7,425 results to 6,686 results before filtering for generated code. Almost all the lost results were FP, except a handful of results involving initializer lists.
1 parent 92b48bf commit 9c42b5f

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ private predicate looksLikeCode(string line) {
2020
exists(string trimmed |
2121
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "") |
2222
trimmed.regexpMatch(".*[{};]")
23+
and (
24+
// If this line looks like code because it ends with a closing
25+
// brace that's preceded by something other than whitespace ...
26+
trimmed.regexpMatch(".*.\\}")
27+
implies
28+
// ... then there has to be ") {" (with some variation of
29+
// whitespace) on the line, suggesting it's a statement like `if`
30+
// or a function declaration. Otherwise it's likely to be a
31+
// benign use of braces such as a JSON example or explanatory
32+
// pseudocode.
33+
trimmed.regexpMatch(".*\\)\\s*\\{.*")
34+
)
2335
and not trimmed.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})"))
2436
}
2537

cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
| test2.cpp:13:1:13:33 | /* JSON example: {"foo":"bar"} */ | This comment appears to contain commented-out code |
2-
| test2.cpp:33:1:33:17 | // { 1, 2, 3, 4 } | This comment appears to contain commented-out code |
3-
| test2.cpp:35:1:35:26 | // Example: { 1, 2, 3, 4 } | This comment appears to contain commented-out code |
41
| test2.cpp:37:1:37:39 | // int myFunction() { return myValue; } | This comment appears to contain commented-out code |
52
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
63
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |

0 commit comments

Comments
 (0)