[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 21 13:36:13 PST 2025


================
@@ -0,0 +1,84 @@
+// RUN: %check_clang_tidy %s readability-use-numeric-limits %t
+#include <stdint.h>
+
+void constants() {
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant -128 is being utilized. Consider using std::numeric_limits<int8_t>::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t a = std::numeric_limits<int8_t>::min();
+  int8_t a = -128;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being utilized. Consider using std::numeric_limits<int8_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t b = std::numeric_limits<int8_t>::max();
+  int8_t b = +127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being utilized. Consider using std::numeric_limits<int8_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t c = std::numeric_limits<int8_t>::max();
+  int8_t c = 127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -32768 is being utilized. Consider using std::numeric_limits<int16_t>::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t d = std::numeric_limits<int16_t>::min();
+  int16_t d = -32768;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being utilized. Consider using std::numeric_limits<int16_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t e = std::numeric_limits<int16_t>::max();
+  int16_t e = +32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being utilized. Consider using std::numeric_limits<int16_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t f = std::numeric_limits<int16_t>::max();
+  int16_t f = 32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -2147483648 is being utilized. Consider using std::numeric_limits<int32_t>::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t g = std::numeric_limits<int32_t>::min();
+  int32_t g = -2147483648;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is being utilized. Consider using std::numeric_limits<int32_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t h = std::numeric_limits<int32_t>::max();
+  int32_t h = +2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is being utilized. Consider using std::numeric_limits<int32_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t i = std::numeric_limits<int32_t>::max();
+  int32_t i = 2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -9223372036854775808 is being utilized. Consider using std::numeric_limits<int64_t>::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t j = std::numeric_limits<int64_t>::min();
+  int64_t j = -9223372036854775808;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 9223372036854775807 is being utilized. Consider using std::numeric_limits<int64_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t k = std::numeric_limits<int64_t>::max();
+  int64_t k = +9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 9223372036854775807 is being utilized. Consider using std::numeric_limits<int64_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t l = std::numeric_limits<int64_t>::max();
+  int64_t l = 9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being utilized. Consider using std::numeric_limits<uint8_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t m = std::numeric_limits<uint8_t>::max();
+  uint8_t m = 255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being utilized. Consider using std::numeric_limits<uint8_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t n = std::numeric_limits<uint8_t>::max();
+  uint8_t n = +255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being utilized. Consider using std::numeric_limits<uint16_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t o = std::numeric_limits<uint16_t>::max();
+  uint16_t o = 65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being utilized. Consider using std::numeric_limits<uint16_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t p = std::numeric_limits<uint16_t>::max();
+  uint16_t p = +65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is being utilized. Consider using std::numeric_limits<uint32_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t q = std::numeric_limits<uint32_t>::max();
+  uint32_t q = 4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is being utilized. Consider using std::numeric_limits<uint32_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t r = std::numeric_limits<uint32_t>::max();
+  uint32_t r = +4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 18446744073709551615 is being utilized. Consider using std::numeric_limits<uint64_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint64_t s = std::numeric_limits<uint64_t>::max();
+  uint64_t s = 18446744073709551615;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 18446744073709551615 is being utilized. Consider using std::numeric_limits<uint64_t>::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: uint64_t t = std::numeric_limits<uint64_t>::max();
+  uint64_t t = +18446744073709551615;
----------------
vbvictor wrote:

I think it should get caught. `ignoringParenImpCasts` I guess will do the trick.

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


More information about the cfe-commits mailing list