Skip to content

Commit 100720c

Browse files
authored
[libc] Use if constexpr for compile-time conditionals (#113417)
Don't use plain `if` for things that are compile-time constants. Instead, use `if constexpr`. This both ensures that these are properly wired up constant expressions as intended, and prevents warnings from the compiler about useless `if` checks that look in the source like they're meant to do something at runtime but will just be compiled away.
1 parent 9afcdaa commit 100720c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

libc/test/UnitTest/FPMatcher.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,35 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
124124

125125
bool match(T actualValue) {
126126
actual = actualValue;
127-
if (cpp::is_complex_type_same<T, _Complex float>())
127+
if constexpr (cpp::is_complex_type_same<T, _Complex float>())
128128
return matchComplex<float>();
129-
else if (cpp::is_complex_type_same<T, _Complex double>())
129+
else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
130130
return matchComplex<double>();
131-
else if (cpp::is_complex_type_same<T, _Complex long double>())
131+
else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
132132
return matchComplex<long double>();
133133
#ifdef LIBC_TYPES_HAS_CFLOAT16
134-
else if (cpp::is_complex_type_same<T, cfloat16>)
134+
else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
135135
return matchComplex<float16>();
136136
#endif
137137
#ifdef LIBC_TYPES_HAS_CFLOAT128
138-
else if (cpp::is_complex_type_same<T, cfloat128>)
138+
else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
139139
return matchComplex<float128>();
140140
#endif
141141
}
142142

143143
void explainError() override {
144-
if (cpp::is_complex_type_same<T, _Complex float>())
144+
if constexpr (cpp::is_complex_type_same<T, _Complex float>())
145145
return explainErrorComplex<float>();
146-
else if (cpp::is_complex_type_same<T, _Complex double>())
146+
else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
147147
return explainErrorComplex<double>();
148-
else if (cpp::is_complex_type_same<T, _Complex long double>())
148+
else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
149149
return explainErrorComplex<long double>();
150150
#ifdef LIBC_TYPES_HAS_CFLOAT16
151-
else if (cpp::is_complex_type_same<T, cfloat16>)
151+
else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
152152
return explainErrorComplex<float16>();
153153
#endif
154154
#ifdef LIBC_TYPES_HAS_CFLOAT128
155-
else if (cpp::is_complex_type_same<T, cfloat128>)
155+
else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
156156
return explainErrorComplex<float128>();
157157
#endif
158158
}

0 commit comments

Comments
 (0)