Fix false negatives in checkBitwiseOnBoolean#2220
Conversation
Thanks, that is way better! |
|
Now the error message lacks some detail, the variable name: Could some more details be added again? |
|
@amai2012, you're right. Though, since the boolean in question could be a long expression, it may be unpleasant to look at. This might be better handled as a separate issue, in that there are many checks which could print more context, and we could enhance reporting in general. I'll follow up on this. |
|
@danmar IIRC recently the column attribute was added. That is not yet mandatory in the message? Or just missing in this output? |
|
The column is not written by default in the tests. But it should be written in the normal output. |
|
@danmar Could you please trigger a re-run? I could not find the reason for travis failure... |
|
Cppcheck executed by Travis now finds several suspicious places in Cppcheck itself: The code referenced in the following warnings looks like it was written using bitwise operations to be shorter, maybe to be better readable (not sure if it really is the case). IMHO when explicitly using boolean types then no bitwise operations should be used. Otherwise human or machine readers could be mislead. If bitwise operations should be used, the variable types could be changed to integers. The code referenced in the following warning can be clearly optimized. I see no need to use a bitwise operator here. IMHO all the detected issues are more or less true positives that uncover so-called code smells that could be changed to make the code better readable. |
|
Can we find a pattern which should be marked as inconclusive? |
As far as I know the intention of this checker is to warn for that kind of code... so if you don't like it you should suppress it. As far as I remember we used to suppress these warnings in Cppcheck. Not sure if/when/why that suppression was removed. |
|
@tgnottingham can you please add the suppression again.. add it at the top of the file |
|
@danmar Done. Also fixed the issue in lib/valueflow.cpp. Thanks all for the feedback and discussion. |
We should check if the LHS or RHS has side effects. We do something similar for |
The intent is to use
But that would imply the functions returns something other than |
| ValueFlow::Value result(0); | ||
| result.condition = value1.condition ? value1.condition : value2.condition; | ||
| result.setInconclusive(value1.isInconclusive() | value2.isInconclusive()); | ||
| result.setInconclusive(value1.isInconclusive() || value2.isInconclusive()); |
There was a problem hiding this comment.
Assuming that Value::isInconclusive() is inlined, I guess this will just cause extra CPU load. Instead of A|B there will be A ? 1 : B .. so in best case you replaced a read with a jump (which is probably slower as far as I know).. and in worst case this just means an extra jump.
I think for readability reasons we should allow bitwise or.
There was a problem hiding this comment.
Assuming that Value::isInconclusive() is inlined, I guess this will just cause extra CPU load. Instead of
A|Bthere will beA ? 1 : B.. so in best case you replaced a read with a jump (which is probably slower as far as I know).. and in worst case this just means an extra jump.
True, though the same could be said of many uses of boolean operators where short-circuiting isn't being utilized. I suspect the general practice is to use boolean operators by default, and use bitwise operators in select optimizations.
There was a problem hiding this comment.
True, though the same could be said of many uses of boolean operators where short-circuiting isn't being utilized. I suspect the general practice is to use boolean operators by default, and use bitwise operators in select optimizations.
Yes. The checker works as it should if it warns for this code. I just don't want that we use this checker in our own code.
There was a problem hiding this comment.
I like this PR :-)
I feel I can merge this PR if this change in ValueFlow is reverted.
There was a problem hiding this comment.
Thanks. :) Reverted the change to ValueFlow.
| shadowFunction | ||
| functionConst | ||
| functionStatic | ||
| bitwiseOnBoolean:lib/checkcondition.cpp |
There was a problem hiding this comment.
I suggest that you suppress this in all files.
There was a problem hiding this comment.
Instead of suppressing this, can we not warn when isConstExpression(tok->astOperand1()) is false?
There was a problem hiding this comment.
Updated to suppress in all files.
We should check if the LHS or RHS has side effects.
My concern is that this would skip cases where this check is most useful. If the RHS has side effects and the bitwise op is unintentional, then the check as-is reveals a potential bug. That may be fairly rare, but it's hard to be sure of the programmer's intent.
There was a problem hiding this comment.
We should check if the LHS or RHS has side effects.
Instead of suppressing this, can we not warn when isConstExpression(tok->astOperand1()) is false?
I did not want to have this checker personally.. so I am not 100% sure. But I think those that wanted to have this checker wanted it to warn always for boolean operators.
I think we should keep these warnings.. but we can use a different ID if we see some special case.. however I am thinking that "separation" can be done separately. This PR is about making the checker work as it was intended to start with as far as I see.
If the logic behavior might be changed if | is replaced by || I think it would be best to mention this in the warning message.. amd possible use a different ID.. but I think that can be done in a separate PR also.
Use AST-based tests in favor of token-based tests for greater coverage.
* Fix false negatives in checkBitwiseOnBoolean Use AST-based tests in favor of token-based tests for greater coverage. * Travis: add suppressions for bitwiseOnBool
Use AST-based tests in favor of token-based tests for greater coverage.