Skip to content

Commit 451850a

Browse files
committed
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 a side effect, this change ensures that GetName<T>() can only be called for known types T (instead of returning an empty string). Also use std::is_unsigned_v instead of !std::is_signed_v.
1 parent 6bbc2fb commit 451850a

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/node_snapshotable.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,17 @@ class SnapshotSerializerDeserializer {
164164
V(std::string)
165165

166166
#define V(TypeName) \
167-
if (std::is_same_v<T, TypeName>) { \
167+
if constexpr (std::is_same_v<T, TypeName>) { \
168168
return #TypeName; \
169-
}
169+
} else // NOLINT(readability/braces)
170170
TYPE_LIST(V)
171171
#undef V
172172

173-
std::string name;
174-
if (std::is_arithmetic_v<T>) {
175-
if (!std::is_signed_v<T>) {
176-
name += "u";
177-
}
178-
name += std::is_integral_v<T> ? "int" : "float";
179-
name += std::to_string(sizeof(T) * 8);
180-
name += "_t";
181-
}
182-
return name;
173+
static_assert(std::is_arithmetic_v<T>);
174+
return (std::is_unsigned_v<T> ? "uint"
175+
: std::is_integral_v<T> ? "int"
176+
: "float") +
177+
std::to_string(sizeof(T) * 8) + "_t";
183178
}
184179

185180
bool is_debug = false;

0 commit comments

Comments
 (0)