[clang-tools-extra] [clang-tidy] Add new check 'misc-scope-reduction' (PR #175429)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 14 10:58:46 PST 2026
================
@@ -0,0 +1,75 @@
+.. title:: clang-tidy - misc-scope-reduction
+
+misc-scope-reduction
+====================
+
+Detects local variables in functions whose scopes can be minimized.
+
+Examples:
+
+.. code-block:: cpp
+
+ void test_deep_nesting() {
+ int deep = 1; // 'deep' can be declared in a smaller scope
+ if (true) {
+ if (true) {
+ if (true) {
+ if (true) {
+ int result = deep * 4;
+ }
+ }
+ }
+ }
+ }
+
+ void test_switch_case(int value) {
+ int result = 0; // 'result' can be declared in a smaller scope
+ switch (value) {
+ case 1:
+ result = 10;
+ break;
+ default:
+ break;
+ }
+ }
+
+ void test_for_loop_expressions() {
+ int i; // 'i' can be declared in the for-loop initialization
+ for (i = 0; i < 5; i++) {
+ // loop body
+ }
+ }
+
+Limitations
+-----------
+
+This checker cannot currently detect when a variable's previous value affects
+subsequent iterations, resulting in false positives in some cases. This can
+be addressed by implementing a pattern matcher that recognizes this
+accumulator pattern across loop iterations or by using clang's builtin
----------------
EugeneZelenko wrote:
```suggestion
accumulator pattern across loop iterations or by using Clang's built-in
```
https://github.com/llvm/llvm-project/pull/175429
More information about the cfe-commits
mailing list