diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -13957,8 +13957,10 @@ return getLangOpts().CPlusPlus ? LHSType : LHSType.getAtomicUnqualifiedType(); } -// Only ignore explicit casts to void. -static bool IgnoreCommaOperand(const Expr *E) { +// Scenarios to ignore if expression E is: +// 1. an explicit cast expression into void +// 2. a function call expression that returins void +static bool IgnoreCommaOperand(const Expr *E, const ASTContext &Context) { E = E->IgnoreParens(); if (const CastExpr *CE = dyn_cast(E)) { @@ -13973,6 +13975,9 @@ } } + if (const CallExpr *CE = dyn_cast(E)) + if (const Type *T = CE->getCallReturnType(Context).getTypePtrOrNull()) + return T->isVoidType(); return false; } @@ -14014,7 +14019,7 @@ } // Only allow some expressions on LHS to not warn. - if (IgnoreCommaOperand(LHS)) + if (IgnoreCommaOperand(LHS, Context)) return; Diag(Loc, diag::warn_comma_operator); diff --git a/clang/test/SemaCXX/warn-comma-operator.cpp b/clang/test/SemaCXX/warn-comma-operator.cpp --- a/clang/test/SemaCXX/warn-comma-operator.cpp +++ b/clang/test/SemaCXX/warn-comma-operator.cpp @@ -140,6 +140,16 @@ // CHECK: fix-it:{{.*}}:{[[@LINE-8]]:46-[[@LINE-8]]:46}:")" } + +void void_func(); +int int_func() { return 0; } + +void buggy(){ + void_func(), int_func(); // expected no -Wcomma because of the returning type `void` + // Reported by https://github.com/llvm/llvm-project/issues/57151 + // Descriptions about -Wcomma: https://reviews.llvm.org/D3976 +} + #ifdef __cplusplus class S2 { public: