Skip to content

Commit 48e5b0a

Browse files
committed
Merge branch 'main' into rust-ti-associated
2 parents 602e617 + d8ca8dd commit 48e5b0a

File tree

137 files changed

+9162
-1431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+9162
-1431
lines changed

config/sync-files.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,19 @@ def file_checksum(filename):
5858
with open(filename, 'rb') as file_handle:
5959
return hashlib.sha1(file_handle.read()).hexdigest()
6060

61-
def check_group(group_name, files, master_file_picker, emit_error):
61+
def accept_prefix(line1, line2):
62+
suffix = line2.removeprefix(line1)
63+
return not suffix or suffix.lstrip().startswith("//")
64+
65+
def equivalent_lines(lines1, lines2):
66+
if len(lines1) != len(lines2):
67+
return False
68+
for line1, line2 in zip(lines1, lines2):
69+
if not accept_prefix(line1, line2) and not accept_prefix(line2, line1):
70+
return False
71+
return True
72+
73+
def check_group(group_name, files, master_file_picker, emit_error, accept_prefix):
6274
extant_files = [f for f in files if path.isfile(f)]
6375
if len(extant_files) == 0:
6476
emit_error(__file__, 0, "No files found from group '" + group_name + "'.")
@@ -70,11 +82,23 @@ def check_group(group_name, files, master_file_picker, emit_error):
7082
return
7183

7284
checksums = {file_checksum(f) for f in extant_files}
73-
74-
if len(checksums) == 1 and len(extant_files) == len(files):
85+
same_lengths = len(extant_files) == len(files)
86+
if len(checksums) == 1 and same_lengths:
7587
# All files are present and identical.
7688
return
7789

90+
# In this case we also consider files indentical, if
91+
# (1) The group only containts two files.
92+
# (2) The lines of one file are the same as the lines of another file
93+
# modulo comments.
94+
if accept_prefix and same_lengths and len(extant_files) == 2:
95+
with open(extant_files[0], 'r') as f1:
96+
file1_lines = [l.strip('\n\r') for l in f1.readlines()]
97+
with open(extant_files[1], 'r') as f2:
98+
file2_lines = [l.strip('\n\r') for l in f2.readlines()]
99+
if equivalent_lines(file1_lines, file2_lines):
100+
return
101+
78102
master_file = master_file_picker(extant_files)
79103
if master_file is None:
80104
emit_error(__file__, 0,
@@ -139,9 +163,10 @@ def sync_identical_files(emit_error):
139163
raise Exception("Bad command line or file not found")
140164
chdir_repo_root()
141165
load_if_exists('.', 'config/identical-files.json')
142-
file_groups.update(csharp_test_files())
166+
for group_name, files in csharp_test_files().items():
167+
check_group(group_name, files, master_file_picker, emit_error, True)
143168
for group_name, files in file_groups.items():
144-
check_group(group_name, files, master_file_picker, emit_error)
169+
check_group(group_name, files, master_file_picker, emit_error, False)
145170

146171
def main():
147172
sync_identical_files(emit_local_error)

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

Lines changed: 1 addition & 0 deletions
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/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,6 @@ void TimerProc(object obj)
114114
public void Dispose() { }
115115
}
116116

117-
class Bad
118-
{
119-
long GetLength(string file)
120-
{
121-
var stream = new FileStream(file, FileMode.Open); // $ Alert
122-
return stream.Length;
123-
}
124-
}
125-
126117
static class Extensions
127118
{
128119
public static FileStream Fluent(this FileStream fs) => fs;

csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
| NoDisposeCallOnLocalIDisposable.cs:76:25:76:71 | call to method Create | Disposable 'XmlReader' is created but not disposed. |
55
| NoDisposeCallOnLocalIDisposable.cs:76:42:76:64 | object creation of type StringReader | Disposable 'StringReader' is created but not disposed. |
66
| NoDisposeCallOnLocalIDisposable.cs:104:23:104:38 | object creation of type HttpClient | Disposable 'HttpClient' is created but not disposed. |
7-
| NoDisposeCallOnLocalIDisposable.cs:121:22:121:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. |
7+
| NoDisposeCallOnLocalIDisposableBad.cs:8:22:8:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.IO;
3+
4+
class Bad
5+
{
6+
long GetLength(string file)
7+
{
8+
var stream = new FileStream(file, FileMode.Open); // $ Alert
9+
return stream.Length;
10+
}
11+
}

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| ConstantCondition.cs:114:13:114:14 | access to parameter b1 | Condition always evaluates to 'true'. |
1212
| ConstantCondition.cs:114:19:114:20 | access to parameter b2 | Condition always evaluates to 'true'. |
1313
| ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. |
14+
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1415
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1516
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |
1617
| ConstantConditionalExpressionCondition.cs:13:21:13:30 | ... == ... | Condition always evaluates to 'true'. |
@@ -19,7 +20,6 @@
1920
| ConstantIfCondition.cs:11:17:11:29 | ... == ... | Condition always evaluates to 'true'. |
2021
| ConstantIfCondition.cs:14:17:14:21 | false | Condition always evaluates to 'false'. |
2122
| ConstantIfCondition.cs:17:17:17:26 | ... == ... | Condition always evaluates to 'true'. |
22-
| ConstantIfCondition.cs:30:20:30:24 | ... > ... | Condition always evaluates to 'false'. |
2323
| ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. |
2424
| ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |
2525
| ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Bad
2+
{
3+
public int Max(int a, int b)
4+
{
5+
return a > a ? a : b; // $ Alert
6+
}
7+
}

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public void Foo()
2525
}
2626
}
2727

28-
public int Max(int a, int b)
29-
{
30-
return a > a ? a : b; // $ Alert
31-
}
32-
3328
public int Bar()
3429
{
3530
return ZERO;

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

Lines changed: 35 additions & 25 deletions
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+
}
Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)