mirror of
https://github.com/github/codeql.git
synced 2026-05-03 12:45:27 +02:00
CPP: Add a test involving templates.
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
|
||||
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
|
||||
| template.cpp:10:7:10:15 | ... < ... | Check the comparison operator precedence. |
|
||||
| test.cpp:42:6:42:14 | ... < ... | Check the comparison operator precedence. |
|
||||
| test.cpp:43:6:43:14 | ... > ... | Check the comparison operator precedence. |
|
||||
| test.cpp:44:6:44:16 | ... <= ... | Check the comparison operator precedence. |
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
template <typename T>
|
||||
void templateFunc1(T x, T y, T z) {
|
||||
if (x < y < z) {} // BAD (though dubious as we can imagine other instantiations using an overloaded `operator<`)
|
||||
if (x < y && y < z) {} // GOOD
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void templateFunc2(T x, T y, T z) {
|
||||
if (x < y < z) {} // GOOD (used with an overloaded `operator<`) [FALSE POSITIVE]
|
||||
if (x < y && y < z) {} // GOOD
|
||||
};
|
||||
|
||||
struct myStruct {
|
||||
operator bool() {
|
||||
return true;
|
||||
}
|
||||
|
||||
myStruct operator<(myStruct &other) {
|
||||
return other; // non-standard `operator<` behaviour
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int x = 3;
|
||||
myStruct y;
|
||||
|
||||
templateFunc1(x, x, x);
|
||||
templateFunc2(y, y, y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user