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

warn_consecutive_comparision produces confusing message.

Open earnol opened this issue 1 year ago • 6 comments

The warning for line like:

if (a >= b <= c)      {}

produces warning or error after https://github.com/llvm/llvm-project/pull/128145 like:

 warning: comparisons like 'X<=Y<=Z' don't have their mathematical meaning [-Wparentheses]

I'm finding this warning/error quite confusing as user's code contains neither X nor Y. Operators also got messed up. I believe the message reported should be corrected and should looks like:

 warning: comparisons like 'a>=b<=c' don't have their mathematical meaning [-Wparentheses]

in the provided example.

Also lines like 11:

if (a < b == c)      {}

are not reported either. Which looks like a bug.

Please see live code example: https://godbolt.org/z/TTxrb1WcG

earnol avatar Feb 27 '25 15:02 earnol

I am interested to work on it 😋

AmrDeveloper avatar Feb 27 '25 21:02 AmrDeveloper

Also lines like 11:

if (a < b == c)      {}

are not reported either. Which looks like a bug.

Yeah, there are a few cases that we’re currently not diagnosing; I’ll make a new issue for those real quick so this issue can be more about improving the actual diagnostic since @AmrDeveloper is already working on that.

Sirraide avatar Mar 03 '25 10:03 Sirraide

Actually, after thinking about this a bit more, I think it’s not that straight-forward: While chained </>/<=/>= are for all intents and purposes always unsound because it doesn’t make sense to use any of these with a bool to begin with, the same is not true for == and !=, and something like this is maybe not too common but perfectly sound:

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

If c is not a bool but e.g. an int, then this might be a bug and we might want to warn on it, but also, at that point, we should probably just warn about any comparison of a bool with a non-literal value that isn’t a bool, because that’s a bit of a weird thing to do—comparing the result of a comparison to an ‘arbitrary’ integer value that is.

The clang-tidy check for this does warn on it, but we accept false positives more readily in clang-tidy than the compiler proper, so I’m not sure we really want to warn about this in Clang (at least there’s definitely a lot more room for discussion here than for chained < and friends imo).

Sirraide avatar Mar 03 '25 10:03 Sirraide

Yes. It is tricky. This example is also not a bug:

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

as comparison > happens before ==. However if c is anything else but bool it looks like a bug.

Yeah, there are a few cases that we’re currently not diagnosing; I’ll make a new issue for those real quick so this issue can be more about improving the actual diagnostic since @AmrDeveloper is already working on that.

Could you please put reference to this new issue here?

earnol avatar Mar 03 '25 15:03 earnol

Could you please put reference to this new issue here?

Ah, I ended up not creating one because I wasn’t entirely sure if there were other cases we might want to handle in the compiler, but I suppose it wouldn’t hurt if to have one so we could discuss this there instead.

Sirraide avatar Mar 03 '25 18:03 Sirraide

#129570

Sirraide avatar Mar 03 '25 18:03 Sirraide