Skip to content

Commit 3f167a6

Browse files
committed
CPP: Add a test involving templates.
1 parent 4fc73ca commit 3f167a6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/ComparisonPrecedence/ComparisonPrecedence.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
2+
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
3+
| template.cpp:10:7:10:15 | ... < ... | Check the comparison operator precedence. |
14
| test.cpp:42:6:42:14 | ... < ... | Check the comparison operator precedence. |
25
| test.cpp:43:6:43:14 | ... > ... | Check the comparison operator precedence. |
36
| test.cpp:44:6:44:16 | ... <= ... | Check the comparison operator precedence. |
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
template <typename T>
3+
void templateFunc1(T x, T y, T z) {
4+
if (x < y < z) {} // BAD (though dubious as we can imagine other instantiations using an overloaded `operator<`)
5+
if (x < y && y < z) {} // GOOD
6+
};
7+
8+
template <typename T>
9+
void templateFunc2(T x, T y, T z) {
10+
if (x < y < z) {} // GOOD (used with an overloaded `operator<`) [FALSE POSITIVE]
11+
if (x < y && y < z) {} // GOOD
12+
};
13+
14+
struct myStruct {
15+
operator bool() {
16+
return true;
17+
}
18+
19+
myStruct operator<(myStruct &other) {
20+
return other; // non-standard `operator<` behaviour
21+
}
22+
};
23+
24+
int main() {
25+
int x = 3;
26+
myStruct y;
27+
28+
templateFunc1(x, x, x);
29+
templateFunc2(y, y, y);
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)