C++: Test for PointlessComparison with templates

This commit is contained in:
Jonas Jensen
2018-10-03 14:39:32 +02:00
parent 604ff232e2
commit 2eea359f79
2 changed files with 21 additions and 0 deletions

View File

@@ -32,3 +32,7 @@
| PointlessComparison.c:129:12:129:16 | ... > ... | Comparison is always false because a <= 3. |
| PointlessComparison.c:197:7:197:11 | ... < ... | Comparison is always false because x >= 0. |
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
| Templates.cpp:3:10:3:24 | ... <= ... | Comparison is always true because param <= 32767. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

View File

@@ -0,0 +1,17 @@
template<typename T>
bool sometimesPointless(T param) {
return param <= 0xFFFF; // GOOD (FALSE POSITIVE: hypothetical instantiations are okay)
}
template<typename T>
bool alwaysPointless(T param) {
short local = param;
return local <= 0xFFFF; // BAD (in all instantiations)
}
static int caller(int i) {
return
sometimesPointless<short>(i) ||
alwaysPointless<short>(i) ||
alwaysPointless<int>(i);
}