[clang-tools-extra] [clang-tidy] Add `modernize-use-if-consteval` check (PR #189743)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri May 29 08:33:55 PDT 2026
================
@@ -0,0 +1,304 @@
+// RUN: %check_clang_tidy -std=c++23-or-later %s modernize-use-if-consteval %t
+
+namespace std {
+constexpr bool is_constant_evaluated() noexcept {
+ return __builtin_is_constant_evaluated();
+}
+}
+
+namespace mine {
+constexpr bool is_constant_evaluated() noexcept {
+ return __builtin_is_constant_evaluated();
+}
+}
+
+namespace alias = std;
+
+#define ICE_CALL() std::is_constant_evaluated()
+#define IF_ICE_HEADER if (std::is_constant_evaluated())
+#define IF_ONLY if
+#define RETURN_ONE() return 1;
+#define RETURN_THREE() return 3;
+
+bool runtime_predicate();
+
+int direct() {
+ if (std::is_constant_evaluated())
+ return 1;
+ else
+ return 2;
+ // CHECK-MESSAGES: :[[@LINE-4]]:7: warning: use 'if consteval' instead of checking 'std::is_constant_evaluated()' [modernize-use-if-consteval]
+ // CHECK-FIXES: if consteval {
+ // CHECK-FIXES-NEXT: return 1;
+ // CHECK-FIXES-NEXT: } else
----------------
zwuis wrote:
> `statement` must be a compound statement, and it will still be treated as a part of the consteval if statement even if it is not a compound statement (and thus results in a compilation error):
https://en.cppreference.com/cpp/language/if#Consteval_if
https://github.com/llvm/llvm-project/pull/189743
More information about the cfe-commits
mailing list