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

[Clang] Should we warn on more chained comparisons?

Open Sirraide opened this issue 1 year ago • 9 comments

#128145 made chained comparisons an error by default, and both there and in #129069, we’ve been having a discussion that there might be other cases that we might want to warn about too; since #129069 is also concerned with the wording of the diagnostic, I’m opening a new issue just for the former.

Currently, the question is what to do with cases such as:

void f(int a, int b, bool c) {
    if (a < b == c) { /* ... */ }
}

This is sound because we’re comparing two bools with == here, but if we change the type of c to something other than bool, then this becomes more questionable. We might want to do something about this case as well (it is worth noting that clang-tidy already warns about this). Currently, we only diagnose chained </>/<=/>=.

See also https://github.com/llvm/llvm-project/issues/129069#issuecomment-2693937679

Sirraide avatar Mar 03 '25 18:03 Sirraide

@llvm/issue-subscribers-clang-frontend

Author: None (Sirraide)

#128145 made chained comparisons an error by default, and both there and in #129069, we’ve been having a discussion that there might be other cases that we might want to warn about too; since #129069 is also concerned with the wording of the diagnostic, I’m opening a new issue just for the former.

Currently, the question is what to do with cases such as:

void f(int a, int b, bool c) {
    if (a &lt; b == c) { /* ... */ }
}

This is sound because we’re comparing two bools with == here, but if we change the type of c to something other than bool, then this becomes more questionable. We might want to do something about this case as well (it is worth noting that clang-tidy already warns about this). Currently, we only diagnose chained &lt;/&gt;/&lt;=/&gt;=.

See also https://github.com/llvm/llvm-project/issues/129069#issuecomment-2693937679

llvmbot avatar Mar 03 '25 18:03 llvmbot

Agreed.

We also should error by default on things like

void f(auto... a) {
    (a < ...);
}

cor3ntin avatar Mar 04 '25 13:03 cor3ntin

Oh, the pack expansion is a good example too; I didn’t even think of that...

For that one, we should probably warn when we parse it rather than at instantiation time; otherwise, we won’t be able to distinguish between that and e.g. (a < b) < c.

Sirraide avatar Mar 04 '25 16:03 Sirraide

Note that similar issue can in theory also happen on overloaded operators, not only binary one. Check bugprone-chaine-comparison handle both, if compiler would by default handle even only binary ones, then check could be reduced to cover only scenarios that involve overloaded operators.

PS: Nice catch with fold expressions.

PiotrZSL avatar Mar 05 '25 08:03 PiotrZSL

Note that similar issue can in theory also happen on overloaded operators, not only binary one.

Well, overloaded operators can in theory have any semantics, so I’m not sure we can warn about that in the general case. Also, I’m not sure overloaded operators would really run into this problem because, e.g. say you define bool operator<(S, S), then for S a, b, c;, the expression a < b < c doesn’t even compile unless you also define operator<(bool, S), at which point I have no idea what that is possibly supposed to do semantically (if there’s an implicit conversion from bool to S then, yeah, you’d have a problem, but I’m not sure that’s really all that common, but I could be wrong).

Sirraide avatar Mar 05 '25 19:03 Sirraide

That said, requiring people to write (a < b) < c if that’s really what they meant to do isn’t the end of the world either for overloaded opeartors imo. So yeah, that would also be an option

Sirraide avatar Mar 05 '25 19:03 Sirraide

We can split this task in two: for C and for C++. In C we have no overloading operators. Also it is possible to apply this to situation only where comparison operators are not overloaded one's. It will be a step forward, yet it will not expose overloaded operators complications.

earnol avatar Mar 10 '25 13:03 earnol

Well, overloaded operators can in theory have any semantics, so I’m not sure we can warn about that in the general case. Also, I’m not sure overloaded operators would really run into this problem because, e.g. say you define bool operator<(S, S), then for S a, b, c;, the expression a < b < c doesn’t even compile unless you also define operator<(bool, S), at which point I have no idea what that is possibly supposed to do semantically (if there’s an implicit conversion from bool to S then, yeah, you’d have a problem, but I’m not sure that’s really all that common, but I could be wrong).

Right, user-defined operators have different constraints, in particular people write crazy DSL so we cannot really do anything without risking breaking some code(and i feel it's much less of an issue)

cor3ntin avatar Mar 10 '25 13:03 cor3ntin

Right, user-defined operators have different constraints, in particular people write crazy DSL so we cannot really do anything without risking breaking some code(and i feel it's much less of an issue)

Yeah, that’s pretty much my point of view as well. I don’t think we really want to do anything about overloaded operators.

Sirraide avatar Mar 10 '25 23:03 Sirraide