[Clang] Should we warn on more chained comparisons?
#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
@llvm/issue-subscribers-clang-frontend
Author: None (Sirraide)
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
Agreed.
We also should error by default on things like
void f(auto... a) {
(a < ...);
}
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.
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.
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).
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
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.
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 forS a, b, c;, the expressiona < b < cdoesn’t even compile unless you also defineoperator<(bool, S), at which point I have no idea what that is possibly supposed to do semantically (if there’s an implicit conversion frombooltoSthen, 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)
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.