diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h @@ -98,6 +98,7 @@ const BuiltinType &ToType) const; const bool WarnOnIntegerNarrowingConversion; + const bool WarnOnIntegerToFloatingPointNarrowingConversion; const bool WarnOnFloatingPointNarrowingConversion; const bool WarnWithinTemplateInstantiation; const bool WarnOnEquivalentBitWidth; diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp @@ -37,6 +37,8 @@ : ClangTidyCheck(Name, Context), WarnOnIntegerNarrowingConversion( Options.get("WarnOnIntegerNarrowingConversion", true)), + WarnOnIntegerToFloatingPointNarrowingConversion( + Options.get("WarnOnIntegerToFloatingPointNarrowingConversion", true)), WarnOnFloatingPointNarrowingConversion( Options.get("WarnOnFloatingPointNarrowingConversion", true)), WarnWithinTemplateInstantiation( @@ -49,6 +51,8 @@ ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "WarnOnIntegerNarrowingConversion", WarnOnIntegerNarrowingConversion); + Options.store(Opts, "WarnOnIntegerToFloatingPointNarrowingConversion", + WarnOnIntegerToFloatingPointNarrowingConversion); Options.store(Opts, "WarnOnFloatingPointNarrowingConversion", WarnOnFloatingPointNarrowingConversion); Options.store(Opts, "WarnWithinTemplateInstantiation", @@ -376,19 +380,21 @@ void NarrowingConversionsCheck::handleIntegralToFloating( const ASTContext &Context, SourceLocation SourceLoc, const Expr &Lhs, const Expr &Rhs) { - const BuiltinType *ToType = getBuiltinType(Lhs); - llvm::APSInt IntegerConstant; - if (getIntegerConstantExprValue(Context, Rhs, IntegerConstant)) { - if (!isWideEnoughToHold(Context, IntegerConstant, *ToType)) - diagNarrowIntegerConstant(SourceLoc, Lhs, Rhs, IntegerConstant); - return; - } + if (WarnOnIntegerToFloatingPointNarrowingConversion) { + const BuiltinType *ToType = getBuiltinType(Lhs); + llvm::APSInt IntegerConstant; + if (getIntegerConstantExprValue(Context, Rhs, IntegerConstant)) { + if (!isWideEnoughToHold(Context, IntegerConstant, *ToType)) + diagNarrowIntegerConstant(SourceLoc, Lhs, Rhs, IntegerConstant); + return; + } - const BuiltinType *FromType = getBuiltinType(Rhs); - if (isWarningInhibitedByEquivalentSize(Context, *FromType, *ToType)) - return; - if (!isWideEnoughToHold(Context, *FromType, *ToType)) - diagNarrowType(SourceLoc, Lhs, Rhs); + const BuiltinType *FromType = getBuiltinType(Rhs); + if (isWarningInhibitedByEquivalentSize(Context, *FromType, *ToType)) + return; + if (!isWideEnoughToHold(Context, *FromType, *ToType)) + diagNarrowType(SourceLoc, Lhs, Rhs); + } } void NarrowingConversionsCheck::handleFloatingToIntegral( diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst --- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst @@ -15,7 +15,8 @@ We enforce only part of the guideline, more specifically, we flag narrowing conversions from: - an integer to a narrower integer (e.g. ``char`` to ``unsigned char``) if WarnOnIntegerNarrowingConversion Option is set, - - an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``), + - an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``) + if WarnOnIntegerToFloatingPointNarrowingConversion Option is set, - a floating-point to an integer (e.g. ``double`` to ``int``), - a floating-point to a narrower floating-point (e.g. ``double`` to ``float``) if WarnOnFloatingPointNarrowingConversion Option is set. @@ -36,6 +37,11 @@ When `true`, the check will warn on narrowing integer conversion (e.g. ``int`` to ``size_t``). `true` by default. +.. option:: WarnOnIntegerToFloatingPointNarrowingConversion + + When `true`, the check will warn on narrowing integer to floating-point + conversion (e.g. ``size_t`` to ``double``). `true` by default. + .. option:: WarnOnFloatingPointNarrowingConversion When `true`, the check will warn on narrowing floating point conversion diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-narrowingintegertofloatingpoint-option.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-narrowingintegertofloatingpoint-option.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-narrowingintegertofloatingpoint-option.cpp @@ -0,0 +1,13 @@ +// RUN: %check_clang_tidy -check-suffix=DEFAULT %s \ +// RUN: cppcoreguidelines-narrowing-conversions %t -- \ +// RUN: -config='{CheckOptions: [{key: cppcoreguidelines-narrowing-conversions.WarnOnIntegerToFloatingPointNarrowingConversion, value: true}]}' + +// RUN: %check_clang_tidy -check-suffix=DISABLED %s \ +// RUN: cppcoreguidelines-narrowing-conversions %t -- \ +// RUN: -config='{CheckOptions: [{key: cppcoreguidelines-narrowing-conversions.WarnOnIntegerToFloatingPointNarrowingConversion, value: false}]}' + +void foo(size_t value) { + double a = value; + // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:11: warning: narrowing conversion from 'size_t' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions] + // DISABLED: No warning for integer to floating-point narrowing conversions when WarnOnIntegerToFloatingPointNarrowingConversion = false. +}