From: Chris Lattner Date: Tue, 28 Aug 2007 18:40:30 +0000 (+0000) Subject: Emit a slightly better warning for unused values for unary and binary operators: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ba61556d56cc1b10dc0c068f3f55add7478f37ff;p=clang Emit a slightly better warning for unused values for unary and binary operators: t2.c:3:18: warning: expression result unused x = ++x, ++y, y+2; ~~~~~~~~~~~~^ ~~~ git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41551 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp index 6247215f0e..300388cf33 100644 --- a/Sema/SemaStmt.cpp +++ b/Sema/SemaStmt.cpp @@ -21,6 +21,20 @@ #include "clang/Lex/IdentifierTable.h" using namespace clang; +/// DiagnoseDeadExpr - The specified expression is side-effect free and +/// evaluated in a context where the result is unused. Emit a diagnostic to +/// warn about this if appropriate. +static void DiagnoseDeadExpr(Expr *E, Sema &S) { + if (const BinaryOperator *BO = dyn_cast(E)) + S.Diag(BO->getOperatorLoc(), diag::warn_unused_expr, + BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); + else if (const UnaryOperator *UO = dyn_cast(E)) + S.Diag(UO->getOperatorLoc(), diag::warn_unused_expr, + UO->getSubExpr()->getSourceRange()); + else + S.Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); +} + Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { Expr *E = static_cast(expr); assert(E && "ParseExprStmt(): missing expression"); @@ -28,7 +42,7 @@ Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { // Exprs are statements, so there is no need to do a conversion here. However, // diagnose some potentially bad code. if (!E->hasLocalSideEffect() && !E->getType()->isVoidType()) - Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); + DiagnoseDeadExpr(E, *this); return E; }