@@ -381,10 +381,11 @@ void NarrowingConversionsCheck::diagNarrowTypeOrConstant(
381381 const Expr &Rhs) {
382382 APValue Constant = getConstantExprValue (Context, Rhs);
383383 if (Constant.isInt ())
384- return diagNarrowIntegerConstant (SourceLoc, Lhs, Rhs, Constant.getInt ());
385- if (Constant.isFloat ())
386- return diagNarrowConstant (SourceLoc, Lhs, Rhs);
387- return diagNarrowType (SourceLoc, Lhs, Rhs);
384+ diagNarrowIntegerConstant (SourceLoc, Lhs, Rhs, Constant.getInt ());
385+ else if (Constant.isFloat ())
386+ diagNarrowConstant (SourceLoc, Lhs, Rhs);
387+ else
388+ diagNarrowType (SourceLoc, Lhs, Rhs);
388389}
389390
390391void NarrowingConversionsCheck::handleIntegralCast (const ASTContext &Context,
@@ -460,10 +461,10 @@ void NarrowingConversionsCheck::handleFloatingToIntegral(
460461 llvm::APFloat FloatConstant (0.0 );
461462 if (getFloatingConstantExprValue (Context, Rhs, FloatConstant)) {
462463 if (!isFloatExactlyRepresentable (Context, FloatConstant, Lhs.getType ()))
463- return diagNarrowConstant (SourceLoc, Lhs, Rhs);
464+ diagNarrowConstant (SourceLoc, Lhs, Rhs);
464465
465- if (PedanticMode)
466- return diagConstantCast (SourceLoc, Lhs, Rhs);
466+ else if (PedanticMode)
467+ diagConstantCast (SourceLoc, Lhs, Rhs);
467468
468469 return ;
469470 }
@@ -478,7 +479,7 @@ void NarrowingConversionsCheck::handleFloatingToIntegral(
478479void NarrowingConversionsCheck::handleFloatingToBoolean (
479480 const ASTContext &Context, SourceLocation SourceLoc, const Expr &Lhs,
480481 const Expr &Rhs) {
481- return diagNarrowTypeOrConstant (Context, SourceLoc, Lhs, Rhs);
482+ diagNarrowTypeOrConstant (Context, SourceLoc, Lhs, Rhs);
482483}
483484
484485void NarrowingConversionsCheck::handleBooleanToSignedIntegral (
@@ -532,19 +533,20 @@ void NarrowingConversionsCheck::handleBinaryOperator(const ASTContext &Context,
532533 if (LhsType == RhsType)
533534 return ;
534535 if (RhsType->getKind () == BuiltinType::Bool && LhsType->isSignedInteger ())
535- return handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
536- if (RhsType->isInteger () && LhsType->getKind () == BuiltinType::Bool)
537- return handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
538- if (RhsType->isInteger () && LhsType->isFloatingPoint ())
539- return handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
540- if (RhsType->isInteger () && LhsType->isInteger ())
541- return handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
542- if (RhsType->isFloatingPoint () && LhsType->getKind () == BuiltinType::Bool)
543- return handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
544- if (RhsType->isFloatingPoint () && LhsType->isInteger ())
545- return handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
546- if (RhsType->isFloatingPoint () && LhsType->isFloatingPoint ())
547- return handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
536+ handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
537+ else if (RhsType->isInteger () && LhsType->getKind () == BuiltinType::Bool)
538+ handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
539+ else if (RhsType->isInteger () && LhsType->isFloatingPoint ())
540+ handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
541+ else if (RhsType->isInteger () && LhsType->isInteger ())
542+ handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
543+ else if (RhsType->isFloatingPoint () &&
544+ LhsType->getKind () == BuiltinType::Bool)
545+ handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
546+ else if (RhsType->isFloatingPoint () && LhsType->isInteger ())
547+ handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
548+ else if (RhsType->isFloatingPoint () && LhsType->isFloatingPoint ())
549+ handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
548550}
549551
550552bool NarrowingConversionsCheck::handleConditionalOperator (
@@ -577,21 +579,28 @@ void NarrowingConversionsCheck::handleImplicitCast(
577579 SourceLocation SourceLoc = Lhs.getExprLoc ();
578580 switch (Cast.getCastKind ()) {
579581 case CK_BooleanToSignedIntegral:
580- return handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
582+ handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
583+ return ;
581584 case CK_IntegralToBoolean:
582- return handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
585+ handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
586+ return ;
583587 case CK_IntegralToFloating:
584- return handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
588+ handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
589+ return ;
585590 case CK_IntegralCast:
586- return handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
591+ handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
592+ return ;
587593 case CK_FloatingToBoolean:
588- return handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
594+ handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
595+ return ;
589596 case CK_FloatingToIntegral:
590- return handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
597+ handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
598+ return ;
591599 case CK_FloatingCast:
592- return handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
600+ handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
601+ return ;
593602 default :
594- break ;
603+ return ;
595604 }
596605}
597606
@@ -610,9 +619,10 @@ void NarrowingConversionsCheck::handleBinaryOperator(const ASTContext &Context,
610619
611620void NarrowingConversionsCheck::check (const MatchFinder::MatchResult &Result) {
612621 if (const auto *Op = Result.Nodes .getNodeAs <BinaryOperator>(" binary_op" ))
613- return handleBinaryOperator (*Result.Context , *Op);
614- if (const auto *Cast = Result.Nodes .getNodeAs <ImplicitCastExpr>(" cast" ))
615- return handleImplicitCast (*Result.Context , *Cast);
616- llvm_unreachable (" must be binary operator or cast expression" );
622+ handleBinaryOperator (*Result.Context , *Op);
623+ else if (const auto *Cast = Result.Nodes .getNodeAs <ImplicitCastExpr>(" cast" ))
624+ handleImplicitCast (*Result.Context , *Cast);
625+ else
626+ llvm_unreachable (" must be binary operator or cast expression" );
617627}
618628} // namespace clang::tidy::bugprone
0 commit comments