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

Skip to content

Commit fda75ab

Browse files
authored
Merge pull request #381 from geoffw0/comments
CPP: Fix false positive in EmptyBlock.ql
2 parents a4371ca + e7f2d7f commit fda75ab

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

change-notes/1.19/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
| **Query** | **Expected impact** | **Change** |
1717
|----------------------------|------------------------|------------------------------------------------------------------|
18+
| Empty branch of conditional | Fewer false positive results | The query now recognizes commented blocks more reliably. |
1819
| Resource not released in destructor | Fewer false positive results | Placement new is now excluded from the query. Also fixed an issue where false positives could occur if the destructor body was not in the snapshot. |
1920
| Missing return statement (`cpp/missing-return`) | Visible by default | The precision of this query has been increased from 'medium' to 'high', which makes it visible by default in LGTM. It was 'medium' in release 1.17 and 1.18 because it had false positives due to an extractor bug that was fixed in 1.18. |
2021
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |

cpp/ql/src/Best Practices/Likely Errors/EmptyBlock.ql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class AffectedFile extends File {
2929
}
3030
}
3131

32+
/**
33+
* A block, or an element we might find textually within a block that is
34+
* not a child of it in the AST.
35+
*/
3236
class BlockOrNonChild extends Element {
3337
BlockOrNonChild() {
3438
( this instanceof Block
@@ -68,6 +72,9 @@ class BlockOrNonChild extends Element {
6872
}
6973
}
7074

75+
/**
76+
* A block that contains a non-child element.
77+
*/
7178
predicate emptyBlockContainsNonchild(Block b) {
7279
emptyBlock(_, b) and
7380
exists(BlockOrNonChild c, AffectedFile file |
@@ -78,7 +85,27 @@ predicate emptyBlockContainsNonchild(Block b) {
7885
)
7986
}
8087

88+
/**
89+
* A block that is entirely on one line, which also contains a comment. Chances
90+
* are the comment is intended to refer to the block.
91+
*/
92+
predicate lineComment(Block b) {
93+
emptyBlock(_, b) and
94+
exists(Location bLocation, File f, int line |
95+
bLocation = b.getLocation() and
96+
f = bLocation.getFile() and
97+
line = bLocation.getStartLine() and
98+
line = bLocation.getEndLine() and
99+
exists(Comment c, Location cLocation |
100+
cLocation = c.getLocation() and
101+
cLocation.getFile() = f and
102+
cLocation.getStartLine() = line
103+
)
104+
)
105+
}
106+
81107
from ControlStructure s, Block eb
82108
where emptyBlock(s, eb)
83109
and not emptyBlockContainsNonchild(eb)
110+
and not lineComment(eb)
84111
select eb, "Empty block without comment"

cpp/ql/test/query-tests/Best Practices/Likely Errors/EmptyBlock/empty_block.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ int f(int x) {
4848

4949
// GOOD (no block)
5050
for (;;) ;
51+
52+
// GOOD (has comment): [FALSE POSITIVE]
53+
if (x) {} // comment
54+
55+
// GOOD (has comment): [FALSE POSITIVE]
56+
if (x) {} // comment
5157
}

0 commit comments

Comments
 (0)