Skip to content

Commit 35f4646

Browse files
committed
C++: Add test cases for UnusedLocals.
1 parent 5ac8475 commit 35f4646

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
| code2.cpp:4:6:4:7 | v1 | Variable v1 is not used |
2-
| code2.cpp:6:6:6:7 | v3 | Variable v3 is not used |
3-
| code2.cpp:10:16:10:17 | v7 | Variable v7 is not used |
4-
| code2.cpp:25:16:25:17 | v1 | Variable v1 is not used |
5-
| code2.cpp:26:16:26:17 | v2 | Variable v2 is not used |
6-
| code2.cpp:41:11:41:16 | myVar1 | Variable myVar1 is not used |
7-
| code2.cpp:63:7:63:8 | v3 | Variable v3 is not used |
1+
| code2.cpp:5:6:5:7 | v1 | Variable v1 is not used |
2+
| code2.cpp:7:6:7:7 | v3 | Variable v3 is not used |
3+
| code2.cpp:11:16:11:17 | v7 | Variable v7 is not used |
4+
| code2.cpp:26:16:26:17 | v1 | Variable v1 is not used |
5+
| code2.cpp:27:16:27:17 | v2 | Variable v2 is not used |
6+
| code2.cpp:42:11:42:16 | myVar1 | Variable myVar1 is not used |
7+
| code2.cpp:64:7:64:8 | v3 | Variable v3 is not used |
8+
| code2.cpp:108:11:108:12 | v2 | Variable v2 is not used |
9+
| code2.cpp:128:9:128:9 | b | Variable b is not used |
10+
| code2.cpp:141:18:141:18 | b | Variable b is not used |
11+
| code2.cpp:162:14:162:16 | obj | Variable obj is not used |
812
| code.c:10:18:10:18 | y | Variable y is not used |
913
| code.c:11:18:11:18 | z | Variable z is not used |
1014
| code.c:18:7:18:7 | x | Variable x is not used |

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// semmle-extractor-options: -std=c++17
12

23
int test_const_init()
34
{
@@ -76,3 +77,89 @@ void test_expect()
7677
}
7778
}
7879
}
80+
81+
// ---
82+
83+
template<class T>
84+
class MyContainer
85+
{
86+
public:
87+
struct Iterator {
88+
const T& operator*() const;
89+
bool operator!=(const Iterator &rhs) const;
90+
Iterator operator++();
91+
};
92+
93+
Iterator begin();
94+
Iterator end();
95+
};
96+
97+
void output(int value);
98+
99+
void test_range_based_for()
100+
{
101+
MyContainer<int> myContainer;
102+
103+
for (int v1 : myContainer) // GOOD: v1 is used
104+
{
105+
output(v1);
106+
}
107+
108+
for (int v2 : myContainer) // BAD: v2 is not used
109+
{
110+
}
111+
}
112+
113+
// ---
114+
115+
int test_lambdas1()
116+
{
117+
int a, b, c, d, e; // (b is not used, but is explicitly captured)
118+
auto myLambda = [a, b, &c](int x, int y) -> int // (y is not used, but is a parameter)
119+
{
120+
return a + c + x;
121+
};
122+
123+
return myLambda(d, e);
124+
}
125+
126+
int test_lambdas2()
127+
{
128+
int a, b; // BAD: b is not used
129+
auto myLambda = [=]() -> int // BAD: myLambda is not used [NOT DETECTED] (due to containing a Constructor)
130+
{
131+
return a;
132+
};
133+
134+
return 0;
135+
}
136+
137+
// ---
138+
139+
void test_if_initializer()
140+
{
141+
bool a = false, b = true; // GOOD: a, b are both used [FALSE POSITIVE]
142+
143+
if (a = b; a)
144+
{
145+
// ...
146+
}
147+
}
148+
149+
// ---
150+
151+
class MyObj
152+
{
153+
public:
154+
MyObj();
155+
};
156+
157+
template<class T>
158+
void myFunction2(T t);
159+
160+
void test_captured_contructor()
161+
{
162+
const auto &obj = MyObj(); // GOOD: obj is used
163+
164+
myFunction2( [obj](){} );
165+
}

0 commit comments

Comments
 (0)