1919#include " llvm/ADT/SmallVector.h"
2020
2121#include < cstdint>
22+ #include < optional>
2223
2324using namespace clang ::ast_matchers;
2425
@@ -48,7 +49,7 @@ NarrowingConversionsCheck::NarrowingConversionsCheck(StringRef Name,
4849 ClangTidyContext *Context)
4950 : ClangTidyCheck(Name, Context),
5051 WarnOnIntegerNarrowingConversion (
51- Options.get(" WarnOnIntegerNarrowingConversion" , true )),
52+ Options.get< bool > (" WarnOnIntegerNarrowingConversion" )),
5253 WarnOnIntegerToFloatingPointNarrowingConversion(
5354 Options.get(" WarnOnIntegerToFloatingPointNarrowingConversion" , true )),
5455 WarnOnFloatingPointNarrowingConversion(
@@ -61,8 +62,9 @@ NarrowingConversionsCheck::NarrowingConversionsCheck(StringRef Name,
6162
6263void NarrowingConversionsCheck::storeOptions (
6364 ClangTidyOptions::OptionMap &Opts) {
64- Options.store (Opts, " WarnOnIntegerNarrowingConversion" ,
65- WarnOnIntegerNarrowingConversion);
65+ if (WarnOnIntegerNarrowingConversion.has_value ())
66+ Options.store (Opts, " WarnOnIntegerNarrowingConversion" ,
67+ WarnOnIntegerNarrowingConversion.value ());
6668 Options.store (Opts, " WarnOnIntegerToFloatingPointNarrowingConversion" ,
6769 WarnOnIntegerToFloatingPointNarrowingConversion);
6870 Options.store (Opts, " WarnOnFloatingPointNarrowingConversion" ,
@@ -392,13 +394,13 @@ void NarrowingConversionsCheck::handleIntegralCast(const ASTContext &Context,
392394 SourceLocation SourceLoc,
393395 const Expr &Lhs,
394396 const Expr &Rhs) {
395- if (WarnOnIntegerNarrowingConversion) {
396- // From [conv.integral] since C++20
397- // The result is the unique value of the destination type that is congruent
398- // to the source integer modulo 2^N, where N is the width of the destination
399- // type.
400- if ( getLangOpts ().CPlusPlus20 )
401- return ;
397+ // From [conv.integral] since C++20
398+ // The result is the unique value of the destination type that is congruent
399+ // to the source integer modulo 2^N, where N is the width of the destination
400+ // type.
401+ const bool ActualWarnOnIntegerNarrowingConversion =
402+ WarnOnIntegerNarrowingConversion. value_or (! getLangOpts ().CPlusPlus20 );
403+ if (ActualWarnOnIntegerNarrowingConversion) {
402404 const BuiltinType *ToType = getBuiltinType (Lhs);
403405 // From [conv.integral] before C++20:
404406 // Conversions to unsigned integer is well defined so no warning is issued.
0 commit comments