From 8ddfb52eaeae322a6b18cdab97efd3e9ba317d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 5 Feb 2023 12:00:53 +0000 Subject: [PATCH] src: use stricter compile-time guidance SnapshotSerializerDeserializer::GetName() appears to confuse static analysis such as Coverity. This changes the function structure to a sequence of if-else blocks and marks all branch conditions as constexpr. (Unfortunately, this results in a dangling 'else' keyword in the V macro.) As per a request in the PR discussion, this change does _not_ ensure that GetName() can only be called for known types T and instead still returns an empty string in that case. Also use std::is_unsigned_v instead of !std::is_signed_v. --- src/node_snapshotable.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index aa66eb331267c4..5fd551ff1689f1 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -164,22 +164,19 @@ class SnapshotSerializerDeserializer { V(std::string) #define V(TypeName) \ - if (std::is_same_v) { \ + if constexpr (std::is_same_v) { \ return #TypeName; \ - } + } else // NOLINT(readability/braces) TYPE_LIST(V) #undef V - std::string name; - if (std::is_arithmetic_v) { - if (!std::is_signed_v) { - name += "u"; - } - name += std::is_integral_v ? "int" : "float"; - name += std::to_string(sizeof(T) * 8); - name += "_t"; + if constexpr (std::is_arithmetic_v) { + return (std::is_unsigned_v ? "uint" + : std::is_integral_v ? "int" + : "float") + + std::to_string(sizeof(T) * 8) + "_t"; } - return name; + return ""; } bool is_debug = false;