diff options
author | John McCall <rjmccall@apple.com> | 2010-03-12 07:11:26 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-03-12 07:11:26 +0000 |
commit | 0faede6f31b07bcec7b776f2b420c3ea9bb3e58c (patch) | |
tree | b442f88dbe25c9f5e0a9666ff67a4ac152d01dac /lib | |
parent | 617def321f362e010cc8ec0523b4f54157e5a7a5 (diff) |
Improve the unused-value check to look into comma expressions and filter out
voids in sub-expressions. Patch by Mike M!
Fixes PR4806.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98335 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AST/Expr.cpp | 10 | ||||
-rw-r--r-- | lib/Sema/SemaStmt.cpp | 7 |
2 files changed, 11 insertions, 6 deletions
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index efd0fd1b8d..1b3202dd42 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -788,6 +788,8 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, switch (getStmtClass()) { default: + if (getType()->isVoidType()) + return false; Loc = getExprLoc(); R1 = getSourceRange(); return true; @@ -834,8 +836,8 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, if (IE->getValue() == 0) return false; - return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) || - BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); + return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) || + BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); } if (BO->isAssignmentOp()) @@ -936,6 +938,8 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, if (const Expr *E = dyn_cast<Expr>(CS->body_back())) return E->isUnusedResultAWarning(Loc, R1, R2, Ctx); + if (getType()->isVoidType()) + return false; Loc = cast<StmtExpr>(this)->getLParenLoc(); R1 = getSourceRange(); return true; @@ -949,6 +953,8 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); return true; case CXXFunctionalCastExprClass: { + if (getType()->isVoidType()) + return false; const CastExpr *CE = cast<CastExpr>(this); // If this is a cast to void or a constructor conversion, check the operand. diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 75d9f67ad9..fd65c32c20 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -76,10 +76,6 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) { if (!E) return; - // Ignore expressions that have void type. - if (E->getType()->isVoidType()) - return; - SourceLocation Loc; SourceRange R1, R2; if (!E->isUnusedResultAWarning(Loc, R1, R2, Context)) @@ -103,6 +99,9 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) { } if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { + if (E->getType()->isVoidType()) + return; + // If the callee has attribute pure, const, or warn_unused_result, warn with // a more specific message to make it clear what is happening. if (const Decl *FD = CE->getCalleeDecl()) { |