Skip to content

Commit 8f0b985

Browse files
authored
Merge pull request #19061 from michaelnebel/csharp/useless-assignment-to-local
C#: Add `cs/useless-assignment-to-local` to the code quality suite.
2 parents ffca52e + 70a174a commit 8f0b985

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

csharp/ql/src/codeql-suites/csharp-code-quality.qls

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
- cs/constant-condition
1313
- cs/useless-gethashcode-call
1414
- cs/non-short-circuit
15+
- cs/useless-assignment-to-local

csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs

+35-25
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ public class DeadStoreOfLocal
99

1010
public int M1()
1111
{
12-
int x = M2(); // BAD
12+
int x = M2(); // $ Alert
1313
return (x = 1) + x; // GOOD
1414
}
1515

1616
public int M2()
1717
{
1818
int x = 1; // GOOD
19-
return x + (x = 1); // BAD
19+
return x + (x = 1); // $ Alert
2020
}
2121

2222
public int M3()
@@ -41,19 +41,19 @@ public int M4()
4141

4242
public void M5()
4343
{
44-
int x = M3(); // BAD
44+
int x = M3(); // $ Alert
4545
}
4646

4747
public void M6()
4848
{
4949
int x = 42;
50-
x += 1; // BAD
50+
x += 1; // $ Alert
5151
}
5252

5353
public void M7()
5454
{
5555
int x = 42;
56-
x++; // BAD
56+
x++; // $ Alert
5757
}
5858

5959
public IEnumerable<string> M8(IEnumerable<string> source)
@@ -79,8 +79,8 @@ public IEnumerable<string> M9(IEnumerable<string> source)
7979

8080
public void M10(IEnumerable<string> source)
8181
{
82-
foreach (var val in source)
83-
{ // BAD
82+
foreach (var val in source) // $ Alert
83+
{
8484
}
8585
}
8686
}
@@ -98,10 +98,10 @@ public void F()
9898
message = "Unsuccessful completion"; // GOOD: Used in finally
9999
Process();
100100
info2 = "Finishing"; // GOOD: Used in exception handler
101-
extra = "Dead store here"; // BAD: Dead store
101+
extra = "Dead store here"; // $ Alert Dead store
102102
Process();
103103
message = "Successful completion"; // GOOD: Used in finally
104-
info1 = "Used in handler"; // BAD: Used in handler, but not a reachable handler
104+
info1 = "Used in handler"; // $ Alert Used in handler, but not a reachable handler
105105
}
106106
catch (SystemException ex)
107107
{
@@ -139,7 +139,7 @@ public void FinallyFlow2()
139139
{
140140
Process();
141141
}
142-
catch (Exception ex) // BAD
142+
catch (Exception ex) // $ Alert
143143
{
144144
Console.WriteLine("Stage " + stage);
145145
stage = 3; // GOOD: Used in finally
@@ -157,7 +157,7 @@ public class OutParam
157157
public void Test()
158158
{
159159
int x;
160-
Fn(out x); // BAD
160+
Fn(out x); // $ MISSING: Alert
161161
Fn(out _); // GOOD
162162
}
163163

@@ -194,7 +194,7 @@ void M1()
194194

195195
void M2()
196196
{
197-
var x = M6(); // BAD [FALSE NEGATIVE]
197+
var x = M6(); // $ MISSING: Alert
198198
Action a = () =>
199199
{
200200
x = 1; // GOOD
@@ -208,7 +208,7 @@ void M3()
208208
int x;
209209
Action a = () =>
210210
{
211-
x = 1; // BAD [FALSE NEGATIVE]
211+
x = 1; // $ MISSING: Alert
212212
};
213213
a();
214214
}
@@ -230,7 +230,7 @@ void M4()
230230

231231
void M5()
232232
{
233-
int x = 0; // BAD: NOT DETECTED
233+
int x = 0; // $ MISSING: Alert
234234
Action a = () =>
235235
{
236236
x = 1; // GOOD
@@ -243,22 +243,22 @@ int M6()
243243
{
244244
fn(() =>
245245
{
246-
int y = M6(); // BAD
246+
int y = M6(); // $ Alert
247247
return (y = 1) + y; // GOOD
248248
});
249249

250250
int captured = 0; // GOOD: Variable captured variable
251251
fn(() => { return captured; });
252252

253-
return captured = 1; // BAD: NOT DETECTED
253+
return captured = 1; // $ MISSING: Alert
254254
}
255255

256256
void M7()
257257
{
258258
var y = 12; // GOOD: Not a dead store (used in delegate)
259259
fn(() =>
260260
{
261-
var x = y; // BAD: Dead store in lambda
261+
var x = y; // $ Alert Dead store in lambda
262262
return 0;
263263
});
264264
}
@@ -297,8 +297,8 @@ void Test()
297297
{ // GOOD
298298
Console.WriteLine($"int {i1}");
299299
}
300-
else if (o is var v1)
301-
{ // BAD
300+
else if (o is var v1) // $ Alert
301+
{
302302
}
303303

304304
switch (o)
@@ -311,7 +311,7 @@ void Test()
311311
case int i3: // GOOD
312312
Console.WriteLine($"int {i3}");
313313
break;
314-
case var v2: // BAD
314+
case var v2: // $ Alert
315315
break;
316316
default:
317317
Console.WriteLine("Something else");
@@ -328,7 +328,7 @@ void M()
328328
Use(x);
329329
Use(b);
330330
Use(s);
331-
(x, (b, s)) = GetTuple(); // BAD: `b`
331+
(x, (b, s)) = GetTuple(); // $ Alert on `b`
332332
Use(x);
333333
Use(s);
334334
(x, (_, s)) = GetTuple(); // GOOD
@@ -369,7 +369,7 @@ string M3()
369369

370370
string M4()
371371
{
372-
var s = M3(); // BAD
372+
var s = M3(); // $ Alert
373373
s = "";
374374
return s;
375375
}
@@ -395,7 +395,7 @@ string M7(bool b)
395395
{
396396
var s = "";
397397
if (b)
398-
s = "abc"; // BAD
398+
s = "abc"; // $ Alert
399399
if (!b)
400400
return s;
401401
return null;
@@ -469,8 +469,18 @@ public static void M()
469469
using var x = new System.IO.FileStream("", System.IO.FileMode.Open); // GOOD
470470
using var _ = new System.IO.FileStream("", System.IO.FileMode.Open); // GOOD
471471

472-
using (var y = new System.IO.FileStream("", System.IO.FileMode.Open)) // BAD
472+
using (var y = new System.IO.FileStream("", System.IO.FileMode.Open)) // $ Alert
473473
{
474474
}
475475
}
476-
}
476+
}
477+
478+
class StringInterpolation
479+
{
480+
void Pi()
481+
{
482+
float pi = 3.14159f; // GOOD
483+
const int align = 6; // GOOD
484+
Console.WriteLine($"Pi, {pi,align:F3}");
485+
}
486+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Dead Code/DeadStoreOfLocal.ql
1+
query: Dead Code/DeadStoreOfLocal.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocalBad.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Bad
44
{
55
double ParseInt(string s)
66
{
7-
var success = int.TryParse(s, out int i);
7+
var success = int.TryParse(s, out int i); // $ Alert
88
return i;
99
}
1010

@@ -20,7 +20,7 @@ double ParseDouble(string s)
2020
{
2121
return double.Parse(s);
2222
}
23-
catch (FormatException e)
23+
catch (FormatException e) // $ Alert
2424
{
2525
return double.NaN;
2626
}
@@ -29,14 +29,14 @@ double ParseDouble(string s)
2929
int Count(string[] ss)
3030
{
3131
int count = 0;
32-
foreach (var s in ss)
32+
foreach (var s in ss) // $ Alert
3333
count++;
3434
return count;
3535
}
3636

3737
string IsInt(object o)
3838
{
39-
if (o is int i)
39+
if (o is int i) // $ Alert
4040
return "yes";
4141
else
4242
return "no";
@@ -46,7 +46,7 @@ string IsString(object o)
4646
{
4747
switch (o)
4848
{
49-
case string s:
49+
case string s: // $ Alert
5050
return "yes";
5151
default:
5252
return "no";

0 commit comments

Comments
 (0)