[clang-tools-extra] [clang-tidy] Add new check `modernize-use-structured-binding` (PR #158462)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 14 01:32:31 PDT 2025


================
@@ -0,0 +1,216 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-use-structured-binding %t -- -- -I %S/Inputs/use-structured-binding/
+
+#include "fake_std_pair_tuple.h"
+
+template<typename T>
+void MarkUsed(T x);
+
+struct TestClass {
+  int a;
+  int b;
+  TestClass() : a(0), b(0) {}
+  TestClass(int x, int y) : a(x), b(y) {}
+};
+
+void DecomposeByAssignWarnCases() {
+  {
+    auto P = getPair<int, int>();
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} auto [x, y] = getPair<int, int>();
+    int x = P.first;
+    int y = P.second;
+  }
+
+  {
+    auto P = getPair<int, int>();
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} auto [x, y] = getPair<int, int>();
----------------
vbvictor wrote:

I'm not sure if it is doable, but we should check that `int x = P.first;` and `auto y = P.second;` is removed after the transformation.
Maybe it is already checked in current tests..? If you remove
```cpp
if (DS1)
      Hints.emplace_back(FixItHint::CreateRemoval(DS1->getSourceRange()));
```
Will the tests fail?

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


More information about the cfe-commits mailing list