C++: Add testcase demonstrating false positive

This commit is contained in:
Mathias Vorreiter Pedersen
2020-01-07 14:09:39 +01:00
parent 9a841636dc
commit 6bbe2c48bf
2 changed files with 37 additions and 0 deletions

View File

@@ -14,3 +14,11 @@
| test.cpp:84:7:84:11 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:92:17:92:22 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:113:6:113:10 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:129:17:129:21 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:134:19:134:23 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:138:21:138:25 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:141:7:141:11 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:144:32:144:36 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:147:41:147:45 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:150:32:150:36 | ... = ... | Use of '=' where '==' may have been intended. |
| test.cpp:153:46:153:50 | ... = ... | Use of '=' where '==' may have been intended. |

View File

@@ -123,4 +123,33 @@ void f2() {
if(sz = "def") { // GOOD: a == comparison with a string literal is probably not the intent here
}
}
void f3(int x, int y) {
if(x == 1 && (y = 2)) { // GOOD [FALSE POSITIVE]: the programmer seems to be okay with unparenthesized
// comparison operands, so the parenthesis was used to mark this
// as an assignment
}
if((x == 1) && (y = 2)) { // BAD
}
long z = x;
if(((z == 42) || (y = 2)) && (x == 1)) { // BAD
}
if((y = 2) && (x == z || x == 1)) { // GOOD [FALSE POSITIVE]
}
if(((x == 42) || x == 1) && (y = 2)) { // BAD
}
if(x == 10 || (x == 42 && x == 1) && (y = 2)) { // GOOD [FALSE POSITIVE]
}
if(x == 10 || ((x == 42) && (y = 2)) && (z == 1)) { // BAD
}
if((x == 10) || ((z == z) && (x == 1)) && (y = 2)) { // BAD
}
}