-Woverloaded-shift-op-parentheses does not detect issues with implicit cast to bool
Example:
This one reports -Woverloaded-shift-op-parentheses:
#include <iostream>
int main() {
int a = 5;
std::cout << "Test" << a == 5 ? 1 : 0;
return 0;
}
warning: overloaded operator << has higher precedence than comparison operator [-Woverloaded-shift-op-parentheses]
But this one not:
#include <iostream>
int main()
{
int a = 5;
std::cout << "Test" << a ? 1 : 0;
return 0;
}
And this one:
#include <iostream>
int main()
{
int a = 5;
std::cout << "Test" << static_cast<bool>(a) ? 1 : 0;
return 0;
}
reports:
warning: operator '?:' has lower precedence than '<<'; '<<' will be evaluated first [-Wparentheses]
I would expect that all of above report some sort of warning. This is reported by Sonar as CWE-783: https://cwe.mitre.org/data/definitions/783.html
Suspicious calculation. Please use parentheses to clarify the code. The code ''a+b?c:d'' should be written as either ''(a+b)?c:d'' or ''a+(b?c:d)''.
This should be handled by some warning, or by some Clang-Tidy check.
So it looks like in DiagnoseBinOpPrecedence(...):
if (BinaryOperator::isComparisonOp(Opc))
DiagnoseShiftCompare(Self, OpLoc, LHSExpr, RHSExpr);
isComparisonOp(...) is limiting to these cases return Opc >= BO_Cmp && Opc<=BO_NE;
So I think this should be expandable to more cases and maybe even a good fist problem wdyt @AaronBallman
I think it makes sense to add diagnostics to these cases, but we should validate my assumption that doing so will predominately catch true positives. As for whether this is a good first issue or not... it seems reasonable to me.
@llvm/issue-subscribers-good-first-issue
@AaronBallman @shafik can i look into this one?
@AaronBallman @shafik can i look into this one?
Absolutely! (FWIW, anyone can work on any issued they'd like -- no permission needed.)
hey @chaitanyav , are you still working on this. Could I pick it up?
@krishenm94 am working on it, want to get some feedback from @AaronBallman on this https://reviews.llvm.org/D147844
@krishenm94 please go ahead. I will assign this to you.
@chaitanyav -- sorry for the delay in getting you that feedback; FWIW, it sometimes takes a week or two for reviewers to review things, depending on how heavy our review backlog is/vacations/other responsibilities.
FWIW, your patch was basically ready. I spotted one question in the test cases (is the change to operator bool() needed for anything?) and that it's missing a release note, but that's basically it.
@AaronBallman thank you for the feedback let me send out another revision with the changes and release notes updated. It was needed for stream << (a ? true : false ) , the compiler was complaining about stream and bool (will paste the msg in the revision). @krishenm94 sorry for the confusion.
@chaitanyav are you still working on this?
https://reviews.llvm.org/D147844, it was decided it is more of a style issue. i think no more work should be done on this.
See discussion on D147844