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.
This commit is contained in:
Jonas Jensen 2019-01-17 09:37:04 +01:00
Родитель 92b48bf6af
Коммит 9c42b5fab7
2 изменённых файлов: 12 добавлений и 3 удалений

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

@ -20,6 +20,18 @@ private predicate looksLikeCode(string line) {
exists(string trimmed |
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "") |
trimmed.regexpMatch(".*[{};]")
and (
// If this line looks like code because it ends with a closing
// brace that's preceded by something other than whitespace ...
trimmed.regexpMatch(".*.\\}")
implies
// ... then there has to be ") {" (with some variation of
// whitespace) on the line, suggesting it's a statement like `if`
// or a function declaration. Otherwise it's likely to be a
// benign use of braces such as a JSON example or explanatory
// pseudocode.
trimmed.regexpMatch(".*\\)\\s*\\{.*")
)
and not trimmed.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})"))
}

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

@ -1,6 +1,3 @@
| test2.cpp:13:1:13:33 | /* JSON example: {"foo":"bar"} */ | This comment appears to contain commented-out code |
| test2.cpp:33:1:33:17 | // { 1, 2, 3, 4 } | This comment appears to contain commented-out code |
| test2.cpp:35:1:35:26 | // Example: { 1, 2, 3, 4 } | This comment appears to contain commented-out code |
| test2.cpp:37:1:37:39 | // int myFunction() { return myValue; } | This comment appears to contain commented-out code |
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |