Skip to content

[Clang][diagnostics] Improve the diagnostics for chained comparisons #129285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ Improvements to Clang's diagnostics
:doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
feature will be default-enabled with ``-Wthread-safety`` in a future release.

- Improve the diagnostics for chained comparisons to report actual expressions and operators (#GH129069).

- Improve the diagnostics for shadows template parameter to report correct location (#GH129060).

Improvements to Clang's time-trace
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -7157,7 +7157,7 @@ def note_precedence_conditional_first : Note<
"place parentheses around the '?:' expression to evaluate it first">;

def warn_consecutive_comparison : Warning<
"comparisons like 'X<=Y<=Z' don't have their mathematical meaning">,
"chained comparison 'X %0 Y %1 Z' does not behave the same as a mathematical expression">,
InGroup<Parentheses>, DefaultError;

def warn_enum_constant_in_bool_context : Warning<
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14924,7 +14924,8 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,

if (const auto *BI = dyn_cast<BinaryOperator>(LHSExpr);
BI && BI->isComparisonOp())
Diag(OpLoc, diag::warn_consecutive_comparison);
Diag(OpLoc, diag::warn_consecutive_comparison)
<< BI->getOpcodeStr() << BinaryOperator::getOpcodeStr(Opc);

break;
case BO_EQ:
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Sema/bool-compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void f(int x, int y, int z) {
if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with boolean expression is always true}}

if ((a<y) == z) {} // no warning
if (a>y<z) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
if (a>y<z) {} // expected-error {{chained comparison 'X > Y < Z' does not behave the same as a mathematical expression}}
if ((a<y) > z) {} // no warning
if((a<y)>(z<y)) {} // no warning
if((a<y)==(z<y)){} // no warning
Expand Down Expand Up @@ -145,7 +145,7 @@ void f(int x, int y, int z) {
if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with boolean expression is always true}}

if (z ==(a<y)) {} // no warning
if (z<a>y) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
if (z<a>y) {} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
if (z > (a<y)) {} // no warning
if((z<y)>(a<y)) {} // no warning
if((z<y)==(a<y)){} // no warning
Expand Down
8 changes: 4 additions & 4 deletions clang/test/Sema/parentheses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ namespace PR20735 {
}

void consecutive_builtin_compare(int x, int y, int z) {
(void)(x < y < z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
(void)(x < y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
(void)(x < y <= z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
(void)(x <= y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
(void)(x < y < z); // expected-warning {{chained comparison 'X < Y < Z' does not behave the same as a mathematical expression}}
(void)(x < y > z); // expected-warning {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
(void)(x < y <= z); // expected-warning {{chained comparison 'X < Y <= Z' does not behave the same as a mathematical expression}}
(void)(x <= y > z); // expected-warning {{chained comparison 'X <= Y > Z' does not behave the same as a mathematical expression}}
(void)((x < y) < z); // no-warning
(void)((x < y) >= z); // no-warning

Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/bool-compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void f(int x, int y, int z) {
if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}

if ((a<y) == z) {} // no warning
if (a>y<z) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
if (a>y<z) {} // expected-error {{chained comparison 'X > Y < Z' does not behave the same as a mathematical expression}}
if ((a<y) > z) {} // no warning
if((a<y)>(z<y)) {} // no warning
if((a<y)==(z<y)){} // no warning
Expand Down Expand Up @@ -159,7 +159,7 @@ void f(int x, int y, int z) {
if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}

if (z ==(a<y)) {} // no warning
if (z<a>y) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
if (z<a>y) {} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
if (z > (a<y)) {} // no warning
if((z<y)>(a<y)) {} // no warning
if((z<y)==(a<y)){} // no warning
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaCXX/cxx2a-adl-only-template-id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int e = h<0>(q); // ok, found by unqualified lookup
void fn() {
f<0>(q);
int f;
f<0>(q); // expected-error {{invalid operands to binary expression}} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
f<0>(q); // expected-error {{invalid operands to binary expression}} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
}

void disambig() {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaTemplate/typo-dependent-name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct X : Base<T> {
bool f(T other) {
// A pair of comparisons; 'inner' is a dependent name so can't be assumed
// to be a template.
return this->inner < other > ::z; // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
return this->inner < other > ::z; // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
}
};

Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaTemplate/typo-template-name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace InExpr {

// These are valid expressions.
foo<foo; // expected-warning {{self-comparison}}
foo<int()>(0); // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
foo<int()>(0); // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
foo<int(), true>(false);
foo<Base{}.n;
}
Expand Down