[clang-tools-extra] [clang-tidy] Adds readability-redundant-const check (PR #189733)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 1 13:57:06 PDT 2026
================
@@ -0,0 +1,156 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-const %t --
+
+struct Foo {};
+
+// Simple allowed usages, nothing to warn
+constexpr int n1 = 10;
+const int n2 = 20;
+constexpr Foo n3 = {};
+
+constexpr const int p1 = 10;
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: redundant 'const' in constexpr variable declaration
+// CHECK-FIXES: constexpr int p1 = 10;
+
+const constexpr int p2 = 20;
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: redundant 'const' in constexpr variable declaration
+// CHECK-FIXES: constexpr int p2 = 20;
+
+static const constexpr int p3 = 20;
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: redundant 'const' in constexpr variable declaration
+// CHECK-FIXES: static constexpr int p3 = 20;
+
+constexpr const Foo p4 = {};
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: redundant 'const' in constexpr variable declaration
+// CHECK-FIXES: constexpr Foo p4 = {};
+
+// Since constexpr makes only the pointer const, this usage is not redundant.
+constexpr const char* n4 = "hello";
+
+constexpr const char* const n5 = "hello";
+// CHECK-MESSAGES: [[@LINE-1]]:23: warning: redundant 'const' in constexpr variable declaration
+// CHECK-FIXES: constexpr const char* n5 = "hello";
+
+// Since T might be a pointer type, we don't warn on this.
+template<typename T>
+const constexpr T n6 = {};
----------------
localspook wrote:
I don't think it's wrong to warn here. That `const` will be applied at the top-level (and is therefore redundant) even if `T` is a pointer type.
https://github.com/llvm/llvm-project/pull/189733
More information about the cfe-commits
mailing list