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:
Jonas Jensen
2018-10-25 09:37:57 +02:00
parent 3c6bed4de6
commit d144f0d154
2 changed files with 40 additions and 0 deletions

View File

@@ -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;

View File

@@ -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. |