mirror of
https://github.com/github/codeql.git
synced 2026-05-01 19:55:15 +02:00
Merge pull request #381 from geoffw0/comments
CPP: Fix false positive in EmptyBlock.ql
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
| **Query** | **Expected impact** | **Change** |
|
||||
|----------------------------|------------------------|------------------------------------------------------------------|
|
||||
| Empty branch of conditional | Fewer false positive results | The query now recognizes commented blocks more reliably. |
|
||||
| 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. |
|
||||
| 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. |
|
||||
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |
|
||||
|
||||
@@ -29,6 +29,10 @@ class AffectedFile extends File {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A block, or an element we might find textually within a block that is
|
||||
* not a child of it in the AST.
|
||||
*/
|
||||
class BlockOrNonChild extends Element {
|
||||
BlockOrNonChild() {
|
||||
( this instanceof Block
|
||||
@@ -68,6 +72,9 @@ class BlockOrNonChild extends Element {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A block that contains a non-child element.
|
||||
*/
|
||||
predicate emptyBlockContainsNonchild(Block b) {
|
||||
emptyBlock(_, b) and
|
||||
exists(BlockOrNonChild c, AffectedFile file |
|
||||
@@ -78,7 +85,27 @@ predicate emptyBlockContainsNonchild(Block b) {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A block that is entirely on one line, which also contains a comment. Chances
|
||||
* are the comment is intended to refer to the block.
|
||||
*/
|
||||
predicate lineComment(Block b) {
|
||||
emptyBlock(_, b) and
|
||||
exists(Location bLocation, File f, int line |
|
||||
bLocation = b.getLocation() and
|
||||
f = bLocation.getFile() and
|
||||
line = bLocation.getStartLine() and
|
||||
line = bLocation.getEndLine() and
|
||||
exists(Comment c, Location cLocation |
|
||||
cLocation = c.getLocation() and
|
||||
cLocation.getFile() = f and
|
||||
cLocation.getStartLine() = line
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
from ControlStructure s, Block eb
|
||||
where emptyBlock(s, eb)
|
||||
and not emptyBlockContainsNonchild(eb)
|
||||
and not lineComment(eb)
|
||||
select eb, "Empty block without comment"
|
||||
|
||||
@@ -48,4 +48,10 @@ int f(int x) {
|
||||
|
||||
// GOOD (no block)
|
||||
for (;;) ;
|
||||
|
||||
// GOOD (has comment): [FALSE POSITIVE]
|
||||
if (x) {} // comment
|
||||
|
||||
// GOOD (has comment): [FALSE POSITIVE]
|
||||
if (x) {} // comment
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user