[clang-tools-extra] [clang-tidy] Extend readability-container-size-empty to std::size() (PR #201231)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 3 03:48:40 PDT 2026


================
@@ -924,6 +930,50 @@ class ReportInTemplateContainerNonEmptyMethod {
   }
 };
 
+namespace GH198494 {
+
+void testStdSize(const std::string &str, std::vector<int> vect) {
+  if (std::size(vect))
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (!vect.empty())
+
+  if (!std::size(vect))
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (vect.empty())
+
+  if (std::size(vect) == 0)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (vect.empty())
+
+  if (std::size(vect) != 0)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (!vect.empty())
+
+  if (std::size(vect) > 0)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (!vect.empty())
+
+  if (std::size(str))
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (!str.empty())
+
+  if (std::size(str) == 0)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
+  // CHECK-FIXES: if (str.empty())
----------------
vbvictor wrote:

Please add tests:

```
using std::size;

size(str) == 0;

#define SIZE std::size

SIZE(str) == 0;
```

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


More information about the cfe-commits mailing list