[clang-tools-extra] [clang-tidy] Add readability-use-builtin-literals check (PR #76065)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 03:31:45 PST 2024


================
@@ -0,0 +1,105 @@
+// RUN: %check_clang_tidy %s readability-use-builtin-literals %t
+
+void warn_and_fix() {
+
+  (char16_t)U'a';
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: u'a';
+  (char32_t)u'a';
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: U'a';
+
+  (int)1;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 1;
+  (unsigned int)0x1ul;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 0x1u;
+  (long int)2l;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 2L;
+  (unsigned long int)0x2lu;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 0x2uL;
+  (long long int)3ll;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 3LL;
+  (unsigned long long int)0x3llu;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 0x3uLL;
+
+  (double)1.f;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 1.;
+  (float)2.;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 2.f;
+  (long double)3e0f;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use built-in literal instead of explicit cast [readability-use-builtin-literals]
+  // CHECK-FIXES: 3e0L;
+
----------------
PiotrZSL wrote:

missing tests for:
- float16_t
- float32_t
- float64_t
- float128_t
- bfloat16

https://github.com/llvm/llvm-project/pull/76065


More information about the cfe-commits mailing list