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

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 14 02:06:08 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>();
+    int x = P.first;
+    auto y = P.second;
+  }
+
+  {
+    const auto P = getPair<int, int>();
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} const auto [x, y] = getPair<int, int>();
+    const int x = P.first;
+    const auto y = P.second;
+  }
+
+  {
+    std::pair<int, int> otherP;
+    auto& P = otherP;
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} auto& [x, y] = otherP;
+    int& x = P.first;
+    auto& y = P.second;
+  }
+
+  {
+    std::pair<int, int> otherP;
+    const auto& P = otherP;
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} const auto& [x, y] = otherP;
+    const int& x = P.first;
+    const auto& y = P.second;
+  }
+}
+
+void forRangeWarnCases() {
+  std::pair<int, int> Pairs[10];
+  for (auto P : Pairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (auto [x, y] : Pairs) {
+    int x = P.first;
+    int y = P.second;
+  }
+
+  for (const auto P : Pairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (const auto [x, y] : Pairs) {
+    const int x = P.first;
+    const int y = P.second;
+  }
+
+  for (auto& P : Pairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (auto& [x, y] : Pairs) {
+    int& x = P.first;
+    int& y = P.second;
+  }
+
+  for (const auto& P : Pairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (const auto& [x, y] : Pairs) {
+    const int& x = P.first;
+    const int& y = P.second;
+  }
+
+  std::pair<TestClass, TestClass> ClassPairs[10];
+  for (auto P : ClassPairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (auto [c1, c2] : ClassPairs) {
+    TestClass c1 = P.first;
+    TestClass c2 = P.second;
+  }
+
+  for (const auto P : ClassPairs) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Should use structured binding to decompose pair [modernize-use-structured-binding]
+    // CHECK-FIXES: {{^}} for (const auto [c1, c2] : ClassPairs) {
+    const TestClass c1 = P.first;
+    const TestClass c2 = P.second;
+  }
----------------
flovent wrote:

Add in [49bfcae](https://github.com/llvm/llvm-project/pull/158462/commits/49bfcae8c804af3eed8369dd736f2283644fc7ce)

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


More information about the cfe-commits mailing list