I recently discovered a bug in my code that cppcheck hasn't warned me about. In my opinion cppcheck should be fully aware of this error . In simpified provided example below cppcheck is silent when it comes to if condition being always false:
If i remove variable "e" from that condition cppcheck warns me about always false condition. When I prepend definition of "e" with static cppcheck works as expected.
When I move defition of "e" to main cppcheck detects always false condition.
So why cppcheck can't warn about knowConditionTrueFalse when there is a global variable with external linkage involved? Regardless of actual value of "e" condition will always be false.
Last edit: Hubert Melchert 2019-12-06
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You are right, testEnum_a & testEnum_b will always be false, so Cppcheck could warn even if the value of e is not known. Maybe some check to avoid false positives avoids this warning to be generated, but I do not know. I think this false negative is worth a ticket.
Last edit: versat 2019-12-10
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
While trying to reproduce the issue and finding a minimal example I found out something.
When the if condition is changed to testEnum_a & testEnum_b & e the issue is reported:
So it matters where exactly the unknown/unsure variable value is in the condition. But in this case that does not make a difference and the issue should be detected regardless of the order of the variables in the condition.
I recently discovered a bug in my code that cppcheck hasn't warned me about. In my opinion cppcheck should be fully aware of this error . In simpified provided example below cppcheck is silent when it comes to if condition being always false:
If i remove variable "e" from that condition cppcheck warns me about always false condition. When I prepend definition of "e" with static cppcheck works as expected.
When I move defition of "e" to main cppcheck detects always false condition.
So why cppcheck can't warn about knowConditionTrueFalse when there is a global variable with external linkage involved? Regardless of actual value of "e" condition will always be false.
Last edit: Hubert Melchert 2019-12-06
You are right,
testEnum_a & testEnum_b
will always be false, so Cppcheck could warn even if the value ofe
is not known. Maybe some check to avoid false positives avoids this warning to be generated, but I do not know. I think this false negative is worth a ticket.Last edit: versat 2019-12-10
While trying to reproduce the issue and finding a minimal example I found out something.
When the
if
condition is changed totestEnum_a & testEnum_b & e
the issue is reported:So it matters where exactly the unknown/unsure variable value is in the condition. But in this case that does not make a difference and the issue should be detected regardless of the order of the variables in the condition.
Edit: I have created a ticket: https://trac.cppcheck.net/ticket/9527
Thanks for reporting that issue.
Last edit: versat 2019-12-10
Can you create a ticket based on this post? If not then I will create a Trac account and report this.
I have created a ticket: https://trac.cppcheck.net/ticket/9527