From: David Blaikie Date: Mon, 16 Jul 2012 20:47:22 +0000 (+0000) Subject: Simplify float comparison checks by using early return. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=980343b9a1d0e85f960fa289c2c9a727004964f2;p=clang Simplify float comparison checks by using early return. Found while investigating PR13330 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160318 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 91f5ce5813..c0597cddd5 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3630,8 +3630,6 @@ do { /// Issue a warning if these are no self-comparisons, as they are not likely /// to do what the programmer intended. void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { - bool EmitWarning = true; - Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); @@ -3640,7 +3638,7 @@ void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { if (DeclRefExpr* DRL = dyn_cast(LeftExprSansParen)) if (DeclRefExpr* DRR = dyn_cast(RightExprSansParen)) if (DRL->getDecl() == DRR->getDecl()) - EmitWarning = false; + return; // Special case: check for comparisons against literals that can be exactly @@ -3648,32 +3646,26 @@ void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { // is a heuristic: often comparison against such literals are used to // detect if a value in a variable has not changed. This clearly can // lead to false negatives. - if (EmitWarning) { - if (FloatingLiteral* FLL = dyn_cast(LeftExprSansParen)) { - if (FLL->isExact()) - EmitWarning = false; - } else - if (FloatingLiteral* FLR = dyn_cast(RightExprSansParen)){ - if (FLR->isExact()) - EmitWarning = false; - } - } + if (FloatingLiteral* FLL = dyn_cast(LeftExprSansParen)) { + if (FLL->isExact()) + return; + } else + if (FloatingLiteral* FLR = dyn_cast(RightExprSansParen)) + if (FLR->isExact()) + return; // Check for comparisons with builtin types. - if (EmitWarning) - if (CallExpr* CL = dyn_cast(LeftExprSansParen)) - if (CL->isBuiltinCall()) - EmitWarning = false; + if (CallExpr* CL = dyn_cast(LeftExprSansParen)) + if (CL->isBuiltinCall()) + return; - if (EmitWarning) - if (CallExpr* CR = dyn_cast(RightExprSansParen)) - if (CR->isBuiltinCall()) - EmitWarning = false; + if (CallExpr* CR = dyn_cast(RightExprSansParen)) + if (CR->isBuiltinCall()) + return; // Emit the diagnostic. - if (EmitWarning) - Diag(Loc, diag::warn_floatingpoint_eq) - << LHS->getSourceRange() << RHS->getSourceRange(); + Diag(Loc, diag::warn_floatingpoint_eq) + << LHS->getSourceRange() << RHS->getSourceRange(); } //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//