[clang-tools-extra] [clang-tidy] fix cppcoreguidelines-narrowing-conversions false positives when narrowing integer to signed integer in C++20 (PR #116591)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 24 08:45:04 PST 2024


================
@@ -393,8 +393,14 @@ void NarrowingConversionsCheck::handleIntegralCast(const ASTContext &Context,
                                                    const Expr &Lhs,
                                                    const Expr &Rhs) {
   if (WarnOnIntegerNarrowingConversion) {
+    // From [conv.integral] since C++20
+    // The result is the unique value of the destination type that is congruent
+    // to the source integer modulo 2^N, where N is the width of the destination
+    // type.
+    if (getLangOpts().CPlusPlus20)
+      return;
----------------
PiotrZSL wrote:

Better would-be to change default value for WarnOnIntegerNarrowingConversion on C++20. We could use `optional<bool>` for that option, and if not set then it could assing value based on C++20 being used or not.

https://github.com/llvm/llvm-project/pull/116591


More information about the cfe-commits mailing list