Of course the sample program itself is silly and does not serve any real purpose, it is just the simplest piece of code that I was able to trigger this false positive with.
Applying any of the following changes prevents the false positive from being triggered:
replacing foo.index == 1 with foo.index == 0
removing the if (foo.index == ...) condition altogether
using local variables for index and value instead of structure fields
git bisect claims that commit c267d85640523c045c7d43ba7ce9c0f305423c5d, i.e. pull request #3001, is the culprit. I also carried out an additional bisect on that pull request's branch (pfultz2/generic-before-condition) and it pointed at the first commit on that branch, 811e3d858f854ca574500c907832a4aca2fbb60d ("Add generic before condition").
Please let me know if any further information and/or experiments would help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hmm.. I agree it is a bug but I think it's not super-critical. The condition is redundant / never true. So instead of the 'either the condition is redundant..' you should get a 'condition is always true' message.
While the warning has some merit for the sample program (because the condition is indeed redundant as it can never be satisfied), it is confusing for similar code using post-incrementation:
Hi there,
Thank you again for your work on cppcheck!
For the following C program:
cppcheck 2.4.1 (with
--enable=warning
) reports:Of course the sample program itself is silly and does not serve any real purpose, it is just the simplest piece of code that I was able to trigger this false positive with.
Applying any of the following changes prevents the false positive from being triggered:
foo.index == 1
withfoo.index == 0
if (foo.index == ...)
condition altogetherindex
andvalue
instead of structure fieldsgit bisect
claims that commit c267d85640523c045c7d43ba7ce9c0f305423c5d, i.e. pull request #3001, is the culprit. I also carried out an additional bisect on that pull request's branch (pfultz2/generic-before-condition
) and it pointed at the first commit on that branch, 811e3d858f854ca574500c907832a4aca2fbb60d ("Add generic before condition").Please let me know if any further information and/or experiments would help.
hmm.. I agree it is a bug but I think it's not super-critical. The condition is redundant / never true. So instead of the 'either the condition is redundant..' you should get a 'condition is always true' message.
Apparently Cppcheck does not understand that
foo.index
is always 0. I think https://trac.cppcheck.net/ticket/8121 is about this improvement.While the warning has some merit for the sample program (because the condition is indeed redundant as it can never be satisfied), it is confusing for similar code using post-incrementation:
In this case, neither the condition is redundant nor the array is accessed out of bounds.
yes this one is a critical FP. The warning is just wrong. Thanks!
I have created https://trac.cppcheck.net/ticket/10284