llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

-Woverloaded-shift-op-parentheses does not detect issues with implicit cast to bool

Open PiotrZSL opened this issue 3 years ago • 13 comments

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.

PiotrZSL avatar Apr 05 '23 08:04 PiotrZSL

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

shafik avatar Apr 05 '23 15:04 shafik

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.

AaronBallman avatar Apr 05 '23 16:04 AaronBallman

@llvm/issue-subscribers-good-first-issue

llvmbot avatar Apr 05 '23 21:04 llvmbot

@AaronBallman @shafik can i look into this one?

chaitanyav avatar Apr 06 '23 15:04 chaitanyav

@AaronBallman @shafik can i look into this one?

Absolutely! (FWIW, anyone can work on any issued they'd like -- no permission needed.)

AaronBallman avatar Apr 06 '23 15:04 AaronBallman

hey @chaitanyav , are you still working on this. Could I pick it up?

kmemdump avatar Apr 15 '23 05:04 kmemdump

@krishenm94 am working on it, want to get some feedback from @AaronBallman on this https://reviews.llvm.org/D147844

chaitanyav avatar Apr 15 '23 15:04 chaitanyav

@krishenm94 please go ahead. I will assign this to you.

chaitanyav avatar Apr 16 '23 15:04 chaitanyav

@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 avatar Apr 17 '23 11:04 AaronBallman

@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 avatar Apr 17 '23 13:04 chaitanyav

@chaitanyav are you still working on this?

Endilll avatar Aug 16 '23 13:08 Endilll

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.

chaitanyav avatar Aug 16 '23 17:08 chaitanyav

See discussion on D147844

erichkeane avatar Jan 16 '25 16:01 erichkeane