Skip to content

Commit 47a548f

Browse files
committed
C++: FP test for "operator= doesn't return *this"
This rule should not apply to functions that never return.
1 parent 7affbe4 commit 47a548f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ class Obj2 {
112112
int val;
113113
};
114114

115+
struct Exception {
116+
virtual ~Exception();
117+
};
118+
119+
class AlwaysThrows {
120+
public:
121+
AlwaysThrows &operator=(int _val) { // GOOD [FALSE POSITIVE]
122+
throw Exception();
123+
// No `return` statement is generated by the C++ front end because it can
124+
// statically see that the end of the function is unreachable.
125+
}
126+
127+
AlwaysThrows &operator=(int *_val) { // GOOD [FALSE POSITIVE]
128+
int one = 1;
129+
if (one)
130+
throw Exception();
131+
// A `return` statement is generated by the C++ front end, but the
132+
// control-flow pruning in QL will establish that this is unreachable.
133+
}
134+
};
135+
136+
115137
int main() {
116138
Container c;
117139
c = c;

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
| 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. |
33
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<T> does not return a reference to *this. |
44
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
5+
| AV Rule 82.cpp:121:17:121:25 | operator= | Assignment operator in class AlwaysThrows does not return a reference to *this. |
6+
| AV Rule 82.cpp:127:17:127:25 | operator= | Assignment operator in class AlwaysThrows does not return a reference to *this. |

0 commit comments

Comments
 (0)