[clang-tools-extra] [clang-tidy] Add `readability-redundant-nested-if` check (PR #181558)

via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 15 09:51:14 PST 2026


================
@@ -0,0 +1,118 @@
+.. title:: clang-tidy - readability-redundant-nested-if
+
+readability-redundant-nested-if
+===============================
+
+Finds nested ``if`` statements that can be merged by combining their conditions
+with ``&&``.
+
+Example:
+
+.. code-block:: c++
+
+  if (a) {
+    if (b) {
+      work();
+    }
+  }
+
+becomes
+
+.. code-block:: c++
+
+  if ((a) && (b)) {
+    work();
+  }
+
+The check can merge longer chains as well:
+
+.. code-block:: c++
+
+  if (a) {
+    if (b) {
+      if (c) {
+        work();
+      }
+    }
+  }
+
+becomes
+
+.. code-block:: c++
+
+  if ((a) && (b) && (c)) {
+    work();
+  }
+
+The check also supports outer declaration conditions in C++17 and later:
+
+.. code-block:: c++
+
+  if (bool x = ready()) {
+    if (can_run()) {
+      work();
+    }
+  }
+
+becomes
+
+.. code-block:: c++
+
+  if (bool x = ready(); x && (can_run())) {
+    work();
+  }
+
+Safety rules
+------------
+
+The check only transforms chains where:
+
+- Neither outer nor nested ``if`` has an ``else`` branch.
+- Nested merged ``if`` statements do not use condition variables.
----------------
EugeneZelenko wrote:

```suggestion

- Nested merged ``if`` statements do not use condition variables.
```

Same for other list entries.

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


More information about the cfe-commits mailing list