mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
C++: Test for unreachable return statement
This test shows that the previous fix did not solve the problem where a bad return statement exists but is unreachable.
This commit is contained in:
@@ -133,6 +133,43 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class Reachability {
|
||||
Reachability &operator=(Reachability &that) { // GOOD [FALSE POSITIVE]
|
||||
int one = 1;
|
||||
if (one)
|
||||
return *this;
|
||||
else
|
||||
return that; // unreachable
|
||||
}
|
||||
|
||||
// helper function that always returns a reference to `*this`.
|
||||
Reachability &returnThisReference() {
|
||||
int one = 1;
|
||||
if (one)
|
||||
return *this;
|
||||
else
|
||||
return staticInstance; // unreachable
|
||||
}
|
||||
|
||||
// helper function that always returns `this`.
|
||||
Reachability *const returnThisPointer() {
|
||||
int one = 1;
|
||||
if (one)
|
||||
return this;
|
||||
else
|
||||
return &staticInstance; // unreachable
|
||||
}
|
||||
|
||||
Reachability &operator=(int _val) { // GOOD [FALSE POSITIVE]
|
||||
return returnThisReference();
|
||||
}
|
||||
|
||||
Reachability &operator=(short _val) { // GOOD [FALSE POSITIVE]
|
||||
return *returnThisPointer();
|
||||
}
|
||||
|
||||
static Reachability staticInstance;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Container c;
|
||||
|
||||
@@ -2,3 +2,6 @@
|
||||
| AV Rule 82.cpp:24:8:24:16 | operator= | Assignment operator in class Bad2 should have return type Bad2&. Otherwise a copy is created at each call. |
|
||||
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<T> does not return a reference to *this. |
|
||||
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
|
||||
| AV Rule 82.cpp:137:17:137:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |
|
||||
| AV Rule 82.cpp:163:17:163:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |
|
||||
| AV Rule 82.cpp:167:17:167:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |
|
||||
|
||||
Reference in New Issue
Block a user