Merge pull request #779 from jbj/commented-out-code-braces

C++: Relax commented-out code heuristic for `}`
This commit is contained in:
Geoffrey White
2019-01-21 09:37:30 +00:00
committed by GitHub
3 changed files with 50 additions and 0 deletions

View File

@@ -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]+\\})"))
}

View File

@@ -1,3 +1,4 @@
| 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 |
| test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |

View File

@@ -0,0 +1,37 @@
/*
* This sentence contains a semicolon;
* however, this doesn't make it code.
*/
// This sentence contains a semicolon;
// however, this doesn't make it code.
/* Mention a ';' */
/* Mention a '{' */
/* JSON example: {"foo":"bar"} */
/* JSON example in backticks: `{"foo":"bar"}` */
/* JSON example in quotes: '{"foo":"bar"}' */
/*
* Code example: `return 0;`.
*/
// Code example:
//
// return 0;
// Code example:
//
// ```
// return 0;
// ```
// { 1, 2, 3, 4 }
// Example: { 1, 2, 3, 4 }
// int myFunction() { return myValue; }