[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,58 @@
+.. title:: clang-tidy - modernize-use-structured-binding
+
+modernize-use-structured-binding
+================================
+
+Suggests using C++17 structured bindings to decompose pairs.
+
+This check finds three code patterns and recommends using structured bindings for clearer, more idiomatic C++17 code.
+
+1. Decompose a pair variable by assigning its members to separate variables right after its definition:
+
+.. code-block:: c++
+
+ auto p = getPair<int, int>();
+ int x = p.first;
+ int y = p.second;
+
+ into:
+
+ auto [x, y] = getPair<int, int>();
+
+2. Use `std::tie` to decompose a pair into two predefined variables:
+
+.. code-block:: c++
+
+ int a;
+ int b;
+ std::tie(a, b) = getPair<int, int>();
+
+ into:
+
+ auto [a, b] = getPair<int, int>();
+
+3. Manually decompose a pair by assigning to its members to local variables in a range-based for loop:
+
+.. code-block:: c++
+
+ for (autop : vecOfPairs) {
+ int x = p.first;
+ int y = p.second;
+ // ...
+ }
+
+ into:
+
+ for (auto [x, y] : vecOfPairs) {
+ // use x and y
+ }
+
+The check also supports custom pair-like types via the `PairTypes` option.
+
----------------
vbvictor wrote:
You should add "Limitations" section and place text from PR description there.
https://github.com/llvm/llvm-project/pull/158462
More information about the cfe-commits
mailing list