Skip to content

Commit d19504f

Browse files
committed
C++: Add cpp/unused-local-variable test case with switch initializer
This is similar to the test case with the `if` initializer, and we should not forget about it once we support `if` initialization.
1 parent ccd7bb5 commit d19504f

File tree

2 files changed

+14
-1
lines changed
  • cpp/ql

2 files changed

+14
-1
lines changed

cpp/ql/src/Best Practices/Unused Entities/UnusedLocals.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ where
5858
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
5959
not v.getAnAttribute().getName() = "unused" and
6060
not any(ErrorExpr e).getEnclosingFunction() = f and // unextracted expr may use `v`
61-
not any(ConditionDeclExpr cde).getEnclosingFunction() = f // this case can be removed when the `if (a = b; a)` test case doesn't depend on this exclusion
61+
not any(ConditionDeclExpr cde).getEnclosingFunction() = f // this case can be removed when the `if (a = b; a)` and `switch (a = b; a)` test cases don't depend on this exclusion
6262
select v, "Variable " + v.getName() + " is not used"

cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedLocals/code2.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,16 @@ void test_captured_contructor()
163163

164164
myFunction2( [obj](){} );
165165
}
166+
167+
// ---
168+
169+
void test_switch_initializer()
170+
{
171+
bool a = 42, b = 43; // GOOD: a, b are both used
172+
173+
switch (a = b; a)
174+
{
175+
default:
176+
// ...
177+
}
178+
}

0 commit comments

Comments
 (0)