@@ -381,10 +381,11 @@ void NarrowingConversionsCheck::diagNarrowTypeOrConstant(
381
381
const Expr &Rhs) {
382
382
APValue Constant = getConstantExprValue (Context, Rhs);
383
383
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);
388
389
}
389
390
390
391
void NarrowingConversionsCheck::handleIntegralCast (const ASTContext &Context,
@@ -460,10 +461,10 @@ void NarrowingConversionsCheck::handleFloatingToIntegral(
460
461
llvm::APFloat FloatConstant (0.0 );
461
462
if (getFloatingConstantExprValue (Context, Rhs, FloatConstant)) {
462
463
if (!isFloatExactlyRepresentable (Context, FloatConstant, Lhs.getType ()))
463
- return diagNarrowConstant (SourceLoc, Lhs, Rhs);
464
+ diagNarrowConstant (SourceLoc, Lhs, Rhs);
464
465
465
- if (PedanticMode)
466
- return diagConstantCast (SourceLoc, Lhs, Rhs);
466
+ else if (PedanticMode)
467
+ diagConstantCast (SourceLoc, Lhs, Rhs);
467
468
468
469
return ;
469
470
}
@@ -478,7 +479,7 @@ void NarrowingConversionsCheck::handleFloatingToIntegral(
478
479
void NarrowingConversionsCheck::handleFloatingToBoolean (
479
480
const ASTContext &Context, SourceLocation SourceLoc, const Expr &Lhs,
480
481
const Expr &Rhs) {
481
- return diagNarrowTypeOrConstant (Context, SourceLoc, Lhs, Rhs);
482
+ diagNarrowTypeOrConstant (Context, SourceLoc, Lhs, Rhs);
482
483
}
483
484
484
485
void NarrowingConversionsCheck::handleBooleanToSignedIntegral (
@@ -532,19 +533,20 @@ void NarrowingConversionsCheck::handleBinaryOperator(const ASTContext &Context,
532
533
if (LhsType == RhsType)
533
534
return ;
534
535
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);
548
550
}
549
551
550
552
bool NarrowingConversionsCheck::handleConditionalOperator (
@@ -577,21 +579,28 @@ void NarrowingConversionsCheck::handleImplicitCast(
577
579
SourceLocation SourceLoc = Lhs.getExprLoc ();
578
580
switch (Cast.getCastKind ()) {
579
581
case CK_BooleanToSignedIntegral:
580
- return handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
582
+ handleBooleanToSignedIntegral (Context, SourceLoc, Lhs, Rhs);
583
+ return ;
581
584
case CK_IntegralToBoolean:
582
- return handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
585
+ handleIntegralToBoolean (Context, SourceLoc, Lhs, Rhs);
586
+ return ;
583
587
case CK_IntegralToFloating:
584
- return handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
588
+ handleIntegralToFloating (Context, SourceLoc, Lhs, Rhs);
589
+ return ;
585
590
case CK_IntegralCast:
586
- return handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
591
+ handleIntegralCast (Context, SourceLoc, Lhs, Rhs);
592
+ return ;
587
593
case CK_FloatingToBoolean:
588
- return handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
594
+ handleFloatingToBoolean (Context, SourceLoc, Lhs, Rhs);
595
+ return ;
589
596
case CK_FloatingToIntegral:
590
- return handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
597
+ handleFloatingToIntegral (Context, SourceLoc, Lhs, Rhs);
598
+ return ;
591
599
case CK_FloatingCast:
592
- return handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
600
+ handleFloatingCast (Context, SourceLoc, Lhs, Rhs);
601
+ return ;
593
602
default :
594
- break ;
603
+ return ;
595
604
}
596
605
}
597
606
@@ -610,9 +619,10 @@ void NarrowingConversionsCheck::handleBinaryOperator(const ASTContext &Context,
610
619
611
620
void NarrowingConversionsCheck::check (const MatchFinder::MatchResult &Result) {
612
621
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" );
617
627
}
618
628
} // namespace clang::tidy::bugprone
0 commit comments