[clang-tools-extra] [clang-tidy] Add new check 'misc-scope-reduction' (PR #175429)
Oliver Stöneberg via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 11 10:36:05 PST 2026
================
@@ -0,0 +1,181 @@
+// RUN: %check_clang_tidy %s misc-scope-reduction %t -- --
+
+// Test case 1: Variable can be moved to smaller scope (if-block)
+void test_if_scope() {
+ int x = 42; // CHECK-MESSAGES: :[[@LINE]]:7: warning: variable 'x' can be declared in a smaller scope
+ if (true) {
+ int y = x + 1;
+ }
+}
+
+// Test case 2: Variable used across multiple scopes - should NOT warn
+int test_multiple_scopes(int v) {
+ int y = 0; // Should NOT warn - used in if-block and return
+ if (v) {
+ y = 10;
+ }
+ return y;
+}
+
+// Test case 3: Variable can be moved to nested if-block
+void test_nested_if() {
+ int a = 5; // CHECK-MESSAGES: :[[@LINE]]:7: warning: variable 'a' can be declared in a smaller scope
+ if (true) {
+ if (true) {
+ int b = a * 2;
+ }
+ }
+}
+
+// Test case 4: Variable used in same scope - should NOT warn
+void test_same_scope() {
+ int x = 10; // Should NOT warn - used in same scope
+ int y = x + 5;
+}
+
+// Test case 5: Variable can be moved to while loop body
+void test_while_loop() {
+ int counter = 0; // CHECK-MESSAGES: :[[@LINE]]:7: warning: variable 'counter' can be declared in a smaller scope
----------------
firewave wrote:
> I'm fine with improving this in other PRs.
>
> If you can't fix this FP in this PR, would you please add a `FIXME` comment in the testcase?
This should not introduce known FPs even if only temporarily especially if not clear in what time frame they can be addressed (if even possible). It should rather exclude those cases for now.
https://github.com/llvm/llvm-project/pull/175429
More information about the cfe-commits
mailing list