Skip to content

Commit f5ddad1

Browse files
hsutterzaucy
authored andcommitted
Diagnose named-but-unused local variables
This is a case of losing a non-discarded value that wasn't being covered with the discarding rule. I think I like this... although I had to update unused dummy variables in the tests to rename them to `_`, it caught a few cases where I had a loop `counter` variable I wasn't actually using that was just a leftover from a previous incarnation of the test, so I could and did remove it.
1 parent c91580c commit f5ddad1

29 files changed

+121
-79
lines changed

regression-tests/mixed-lifetime-safety-pointer-init-1-error.cpp2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ main: () -> int = {
1515
}
1616

1717
print_and_decorate( p* );
18+
print_and_decorate( x );
1819
}
1920

20-
print_and_decorate: (thing:_) =
21+
print_and_decorate: (thing) =
2122
std::cout << ">> " << thing << "\n";
2223

regression-tests/mixed-lifetime-safety-pointer-init-2-error.cpp2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ main: () -> int = {
1515
}
1616

1717
print_and_decorate( p* );
18+
print_and_decorate( x );
1819
}
1920

20-
print_and_decorate: (thing:_) =
21+
print_and_decorate: (thing) =
2122
std::cout << ">> " << thing << "\n";
2223

regression-tests/mixed-lifetime-safety-pointer-init-3-error.cpp2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ main: () -> int = {
1515
}
1616

1717
print_and_decorate( p* );
18+
print_and_decorate( x );
1819
}
1920

20-
print_and_decorate: (thing:_) =
21+
print_and_decorate: (thing) =
2122
std::cout << ">> " << thing << "\n";
2223

regression-tests/mixed-out-destruction.cpp2

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ struct C {
1919

2020
//-------------------------------------------------------
2121
// 0x: Test one level of out and immediate throw
22-
f00: () throws = { c:C="f00"; x: X; f01(out x); }
23-
f01: (out x: X) throws = { c:C="f01"; x=(); throw_1(); }
22+
f00: () throws = { _:C="f00"; x: X; f01(out x); }
23+
f01: (out x: X) throws = { _:C="f01"; x=(); throw_1(); }
2424

2525
//-------------------------------------------------------
2626
// 1x: Test multiple levels of out and intermediate throw
27-
f10: () throws = { c:C="f10"; x: X; f11(out x); }
28-
f11: (out x: X) throws = { c:C="f11"; f12(out x); }
29-
f12: (out x: X) throws = { c:C="f12"; f13(out x); throw_1(); }
30-
f13: (out x: X) throws = { c:C="f13"; f14(out x); }
31-
f14: (out x: X) throws = { c:C="f14"; x=(); }
27+
f10: () throws = { _:C="f10"; x: X; f11(out x); }
28+
f11: (out x: X) throws = { _:C="f11"; f12(out x); }
29+
f12: (out x: X) throws = { _:C="f12"; f13(out x); throw_1(); }
30+
f13: (out x: X) throws = { _:C="f13"; f14(out x); }
31+
f14: (out x: X) throws = { _:C="f14"; x=(); }
3232

3333
int main() {
3434
C c("main");

regression-tests/pure2-break-continue.cpp2

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ do_break_outer: () =
160160
for_continue_inner: () =
161161
{
162162
vi: std::vector = ( 0, 1, 2 );
163-
counter := 0;
164-
for vi next counter++ do (i) {
163+
for vi do (i) {
165164
vj: std::vector = ( 0, 1, 2 );
166165
inner: for vj do (j) {
167166
std::cout << i << j << " ";
@@ -178,8 +177,7 @@ for_continue_inner: () =
178177
for_continue_outer: () =
179178
{
180179
vi: std::vector = ( 0, 1, 2 );
181-
counter := 0;
182-
outer: for vi next counter++ do (i) {
180+
outer: for vi do (i) {
183181
vj: std::vector = ( 0, 1, 2 );
184182
for vj do (j) {
185183
std::cout << i << j << " ";
@@ -196,8 +194,7 @@ for_continue_outer: () =
196194
for_break_inner: () =
197195
{
198196
vi: std::vector = ( 0, 1, 2 );
199-
counter := 0;
200-
for vi next counter++ do (i) {
197+
for vi do (i) {
201198
vj: std::vector = ( 0, 1, 2 );
202199
inner: for vj do (j) {
203200
std::cout << i << j << " ";
@@ -214,8 +211,7 @@ for_break_inner: () =
214211
for_break_outer: () =
215212
{
216213
vi: std::vector = ( 0, 1, 2 );
217-
counter := 0;
218-
outer: for vi next counter++ do (i) {
214+
outer: for vi do (i) {
219215
vj: std::vector = ( 0, 1, 2 );
220216
for vj do (j) {
221217
std::cout << i << j << " ";

regression-tests/pure2-deducing-pointers-error.cpp2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ main: () -> int = {
3838
fp = 0; // caught
3939

4040
f := fun(a)*;
41+
_ = f;
4142

4243
fp2 := fun2(a).result;
4344
fp2--; // not caught :(

regression-tests/pure2-lifetime-safety-pointer-init-1-error.cpp2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ main: () -> int = {
1212
}
1313

1414
print_and_decorate( p* );
15+
print_and_decorate( x );
1516
}
1617

17-
print_and_decorate: (thing:_) =
18+
print_and_decorate: (thing) =
1819
std::cout << ">> " << thing << "\n";
1920

regression-tests/pure2-look-up-parameter-across-unnamed-function.cpp2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
f: () -> (ri : int = 0) = {
33
pred := :(e) e == 1;
44
ri = 42;
5+
pred(ri);
56
// "return;" is implicit"
67
}
78

89
g: () -> (ri : int) = {
910
ri = 0;
1011
pred := :(e) e == 1;
1112
ri = 42;
13+
pred(ri);
1214
return;
1315
}
1416

regression-tests/pure2-more-wildcards.cpp2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ main: () -> int
66
x: const _ = 2;
77
p: * _ = x&;
88
q: * const _ = p&;
9+
assert (q);
910

1011
if x is (less_than(20)) {
1112
std::cout << "yes, less\n";

regression-tests/pure2-requires-clauses.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ f: <T: type, U: type>
1818
v: <T> const T requires std::same_as<T, i32> = 0;
1919

2020
main: () = {
21-
x: X<int,int> = ();
21+
_: X<int,int> = ();
2222
std::cout << f<int,int>(2,5);
2323
}

regression-tests/pure2-types-basics.cpp2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ main: () = {
7171
std::cout << "f2: (x.f2(2,2))$\n";
7272
std::cout << "f3: (x.f3<3,3>())$\n";
7373
std::cout << "f4: (x.f4<4,4>())$\n";
74-
x2: N::myclass = "abracadabra";
75-
x3: N::myclass = ();
76-
x4: N::myclass = (1, "hair");
74+
_: N::myclass = "abracadabra";
75+
_: N::myclass = ();
76+
_: N::myclass = (1, "hair");
7777

7878
// Invoke the single-param operator=s as actual assignments
7979
std::cout << "x's state before assignments: ";

regression-tests/pure2-types-ordering-via-meta-functions.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ main: () = {
4646
std::cout << "more\n";
4747
}
4848

49-
s: mystruct = ();
49+
_: mystruct = ();
5050
}

regression-tests/pure2-ufcs-member-access-and-chaining.cpp2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ main: () -> int = {
1919
_ = (j.i).ufcs();
2020

2121
42.no_return();
22+
23+
res.no_return();
2224
}
2325

2426
no_return: (_) = { }

regression-tests/pure2-variadics.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ make: <T, Args...> (forward args...: Args) :T = args...;
1919

2020
main: ()
2121
= {
22-
a: x<int, long, std::string> = ();
22+
_: x<int, long, std::string> = ();
2323

2424
std::cout << std::string("xyzzy", 3) << "\n";
2525
std::cout << make_string("plugh", :u8=3) << "\n";

regression-tests/pure2-various-string-literals.cpp2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ main: () = {
88
s7 := u8R"(u8R")";
99
s8 := UR"(UR")";
1010
s9 := LR"(LR")";
11+
_ = s1;
12+
_ = s2;
13+
_ = s3;
14+
_ = s4;
15+
_ = s5;
16+
_ = s6;
17+
_ = s7;
18+
_ = s8;
19+
_ = s9;
1120
}

regression-tests/test-results/mixed-out-destruction.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ int main() {
5454

5555

5656
#line 22 "mixed-out-destruction.cpp2"
57-
auto f00() -> void { C c {"f00"}; cpp2::deferred_init<X> x; f01(cpp2::out(&x));}
58-
auto f01(cpp2::out<X> x) -> void{C c {"f01"}; x.construct();throw_1();}
57+
auto f00() -> void { C auto_22_28 {"f00"}; cpp2::deferred_init<X> x; f01(cpp2::out(&x));}
58+
auto f01(cpp2::out<X> x) -> void{C auto_23_28 {"f01"}; x.construct();throw_1();}
5959

6060
#line 27 "mixed-out-destruction.cpp2"
61-
auto f10() -> void { C c {"f10"}; cpp2::deferred_init<X> x; f11(cpp2::out(&x));}
62-
auto f11(cpp2::out<X> x) -> void{C c {"f11"}; f12(cpp2::out(&x));}
63-
auto f12(cpp2::out<X> x) -> void{C c {"f12"}; f13(cpp2::out(&x));throw_1();}
64-
auto f13(cpp2::out<X> x) -> void{C c {"f13"}; f14(cpp2::out(&x));}
65-
auto f14(cpp2::out<X> x) -> void{C c {"f14"}; x.construct();}
61+
auto f10() -> void { C auto_27_28 {"f10"}; cpp2::deferred_init<X> x; f11(cpp2::out(&x));}
62+
auto f11(cpp2::out<X> x) -> void{C auto_28_28 {"f11"}; f12(cpp2::out(&x));}
63+
auto f12(cpp2::out<X> x) -> void{C auto_29_28 {"f12"}; f13(cpp2::out(&x));throw_1();}
64+
auto f13(cpp2::out<X> x) -> void{C auto_30_28 {"f13"}; f14(cpp2::out(&x));}
65+
auto f14(cpp2::out<X> x) -> void{C auto_31_28 {"f14"}; x.construct();}
6666

regression-tests/test-results/pure2-break-continue.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ auto do_break_outer() -> void;
4141
#line 160 "pure2-break-continue.cpp2"
4242
auto for_continue_inner() -> void;
4343

44-
#line 178 "pure2-break-continue.cpp2"
44+
#line 177 "pure2-break-continue.cpp2"
4545
auto for_continue_outer() -> void;
4646

47-
#line 196 "pure2-break-continue.cpp2"
47+
#line 194 "pure2-break-continue.cpp2"
4848
auto for_break_inner() -> void;
4949

50-
#line 214 "pure2-break-continue.cpp2"
50+
#line 211 "pure2-break-continue.cpp2"
5151
auto for_break_outer() -> void;
5252

5353

@@ -252,84 +252,80 @@ auto do_break_outer() -> void
252252
auto for_continue_inner() -> void
253253
{
254254
std::vector vi {0, 1, 2};
255-
auto counter {0};
256-
for ( auto const& i : vi ) { do {
255+
for ( auto const& i : vi ) {
257256
std::vector vj {0, 1, 2};
258257
for ( auto const& j : vj ) {
259-
#line 166 "pure2-break-continue.cpp2"
258+
#line 165 "pure2-break-continue.cpp2"
260259
{
261260
std::cout << i << j << " ";
262261
if (j == 1) {
263-
goto CONTINUE_166_9;
262+
goto CONTINUE_165_9;
264263
}
265264
std::cout << "inner ";
266-
} CPP2_CONTINUE_BREAK(166_9) }
265+
} CPP2_CONTINUE_BREAK(165_9) }
267266

268-
#line 174 "pure2-break-continue.cpp2"
267+
#line 173 "pure2-break-continue.cpp2"
269268
std::cout << "outer ";
270-
} while (false); ++counter; }
269+
}
271270
}
272271

273272
auto for_continue_outer() -> void
274273
{
275274
std::vector vi {0, 1, 2};
276-
auto counter {0};
277275
for ( auto const& i : vi ) {
278-
#line 182 "pure2-break-continue.cpp2"
279-
{ do {
276+
#line 180 "pure2-break-continue.cpp2"
277+
{
280278
std::vector vj {0, 1, 2};
281279
for ( auto const& j : vj ) {
282280
std::cout << i << j << " ";
283281
if (j == 1) {
284-
goto CONTINUE_182_5;
282+
goto CONTINUE_180_5;
285283
}
286284
std::cout << "inner ";
287285
}
288286

289287
std::cout << "outer ";
290-
} while (false); ++counter; } CPP2_CONTINUE_BREAK(182_5) }
291-
#line 194 "pure2-break-continue.cpp2"
288+
} CPP2_CONTINUE_BREAK(180_5) }
289+
#line 192 "pure2-break-continue.cpp2"
292290
}
293291

294292
auto for_break_inner() -> void
295293
{
296294
std::vector vi {0, 1, 2};
297-
auto counter {0};
298-
for ( auto const& i : vi ) { do {
295+
for ( auto const& i : vi ) {
299296
std::vector vj {0, 1, 2};
300297
for ( auto const& j : vj ) {
301-
#line 202 "pure2-break-continue.cpp2"
298+
#line 199 "pure2-break-continue.cpp2"
302299
{
303300
std::cout << i << j << " ";
304301
if (j == 1) {
305-
goto BREAK_202_9;
302+
goto BREAK_199_9;
306303
}
307304
std::cout << "inner ";
308-
} CPP2_CONTINUE_BREAK(202_9) }
305+
} CPP2_CONTINUE_BREAK(199_9) }
309306

310-
#line 210 "pure2-break-continue.cpp2"
307+
#line 207 "pure2-break-continue.cpp2"
311308
std::cout << "outer ";
312-
} while (false); ++counter; }
309+
}
313310
}
314311

315312
auto for_break_outer() -> void
316313
{
317314
std::vector vi {0, 1, 2};
318-
auto counter {0};
319315
for ( auto const& i : vi ) {
320-
#line 218 "pure2-break-continue.cpp2"
321-
{ do {
316+
#line 214 "pure2-break-continue.cpp2"
317+
{
322318
std::vector vj {0, 1, 2};
323319
for ( auto const& j : vj ) {
324320
std::cout << i << j << " ";
325321
if (j == 1) {
326-
goto BREAK_218_5;
322+
goto BREAK_214_5;
327323
}
328324
std::cout << "inner ";
329325
}
330326

331327
std::cout << "outer ";
332-
} while (false); ++counter; } CPP2_CONTINUE_BREAK(218_5) }
333-
#line 230 "pure2-break-continue.cpp2"
328+
} CPP2_CONTINUE_BREAK(214_5) }
329+
#line 226 "pure2-break-continue.cpp2"
334330
}
335331

regression-tests/test-results/pure2-look-up-parameter-across-unnamed-function.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ struct f_ret { int ri; };
1818

1919

2020

21-
#line 8 "pure2-look-up-parameter-across-unnamed-function.cpp2"
21+
#line 9 "pure2-look-up-parameter-across-unnamed-function.cpp2"
2222
[[nodiscard]] auto g() -> g_ret;
2323

2424

25-
#line 15 "pure2-look-up-parameter-across-unnamed-function.cpp2"
25+
#line 17 "pure2-look-up-parameter-across-unnamed-function.cpp2"
2626
[[nodiscard]] auto main() -> int;
2727

2828

@@ -35,15 +35,17 @@ struct f_ret { int ri; };
3535
#line 3 "pure2-look-up-parameter-across-unnamed-function.cpp2"
3636
auto pred {[](auto const& e) -> auto { return e == 1; }};
3737
ri = 42;
38+
std::move(pred)(ri);
3839
return { std::move(ri) }; // "return;" is implicit"
3940
}
4041

4142
[[nodiscard]] auto g() -> g_ret{
4243
cpp2::deferred_init<int> ri;
43-
#line 9 "pure2-look-up-parameter-across-unnamed-function.cpp2"
44+
#line 10 "pure2-look-up-parameter-across-unnamed-function.cpp2"
4445
ri.construct(0);
4546
auto pred {[](auto const& e) -> auto { return e == 1; }};
4647
ri.value() = 42;
48+
std::move(pred)(ri.value());
4749
return { std::move(ri.value()) };
4850
}
4951

regression-tests/test-results/pure2-more-wildcards.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
auto const x {2};
2929
auto* p {&x};
3030
auto const* q {&p};
31+
cpp2::Default.expects(std::move(q), "");
3132

3233
if (cpp2::is(x, (less_than(20)))) {
3334
std::cout << "yes, less\n";

regression-tests/test-results/pure2-requires-clauses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ requires (std::same_as<T,cpp2::i32>)
6767
T const v {0};
6868

6969
auto main() -> int{
70-
X<int,int> x {};
70+
X<int,int> auto_21_5 {};
7171
std::cout << f<int,int>(2, 5);
7272
}
7373

0 commit comments

Comments
 (0)