diff --git a/config/sync-files.py b/config/sync-files.py index 9645d42f1e59..66d4d017ed8c 100644 --- a/config/sync-files.py +++ b/config/sync-files.py @@ -58,7 +58,19 @@ def file_checksum(filename): with open(filename, 'rb') as file_handle: return hashlib.sha1(file_handle.read()).hexdigest() -def check_group(group_name, files, master_file_picker, emit_error): +def accept_prefix(line1, line2): + suffix = line2.removeprefix(line1) + return not suffix or suffix.lstrip().startswith("//") + +def equivalent_lines(lines1, lines2): + if len(lines1) != len(lines2): + return False + for line1, line2 in zip(lines1, lines2): + if not accept_prefix(line1, line2) and not accept_prefix(line2, line1): + return False + return True + +def check_group(group_name, files, master_file_picker, emit_error, accept_prefix): extant_files = [f for f in files if path.isfile(f)] if len(extant_files) == 0: 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): return checksums = {file_checksum(f) for f in extant_files} - - if len(checksums) == 1 and len(extant_files) == len(files): + same_lengths = len(extant_files) == len(files) + if len(checksums) == 1 and same_lengths: # All files are present and identical. return + # In this case we also consider files indentical, if + # (1) The group only containts two files. + # (2) The lines of one file are the same as the lines of another file + # modulo comments. + if accept_prefix and same_lengths and len(extant_files) == 2: + with open(extant_files[0], 'r') as f1: + file1_lines = [l.strip('\n\r') for l in f1.readlines()] + with open(extant_files[1], 'r') as f2: + file2_lines = [l.strip('\n\r') for l in f2.readlines()] + if equivalent_lines(file1_lines, file2_lines): + return + master_file = master_file_picker(extant_files) if master_file is None: emit_error(__file__, 0, @@ -139,9 +163,10 @@ def sync_identical_files(emit_error): raise Exception("Bad command line or file not found") chdir_repo_root() load_if_exists('.', 'config/identical-files.json') - file_groups.update(csharp_test_files()) + for group_name, files in csharp_test_files().items(): + check_group(group_name, files, master_file_picker, emit_error, True) for group_name, files in file_groups.items(): - check_group(group_name, files, master_file_picker, emit_error) + check_group(group_name, files, master_file_picker, emit_error, False) def main(): sync_identical_files(emit_local_error) diff --git a/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs index aa11be14f67b..a2fd6cd50f15 100644 --- a/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs +++ b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs @@ -114,15 +114,6 @@ void TimerProc(object obj) public void Dispose() { } } -class Bad -{ - long GetLength(string file) - { - var stream = new FileStream(file, FileMode.Open); // $ Alert - return stream.Length; - } -} - static class Extensions { public static FileStream Fluent(this FileStream fs) => fs; diff --git a/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.expected b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.expected index f08cf6837c5f..5d82fb99a8de 100644 --- a/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.expected +++ b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.expected @@ -4,4 +4,4 @@ | NoDisposeCallOnLocalIDisposable.cs:76:25:76:71 | call to method Create | Disposable 'XmlReader' is created but not disposed. | | NoDisposeCallOnLocalIDisposable.cs:76:42:76:64 | object creation of type StringReader | Disposable 'StringReader' is created but not disposed. | | NoDisposeCallOnLocalIDisposable.cs:104:23:104:38 | object creation of type HttpClient | Disposable 'HttpClient' is created but not disposed. | -| NoDisposeCallOnLocalIDisposable.cs:121:22:121:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. | +| NoDisposeCallOnLocalIDisposableBad.cs:8:22:8:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. | diff --git a/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposableBad.cs b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposableBad.cs new file mode 100644 index 000000000000..e8f005358906 --- /dev/null +++ b/csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposableBad.cs @@ -0,0 +1,11 @@ +using System; +using System.IO; + +class Bad +{ + long GetLength(string file) + { + var stream = new FileStream(file, FileMode.Open); // $ Alert + return stream.Length; + } +} diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected index 9e0e69edb904..e154d10b9d3a 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -11,6 +11,7 @@ | ConstantCondition.cs:114:13:114:14 | access to parameter b1 | Condition always evaluates to 'true'. | | ConstantCondition.cs:114:19:114:20 | access to parameter b2 | Condition always evaluates to 'true'. | | ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. | +| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. | | ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. | | ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. | | ConstantConditionalExpressionCondition.cs:13:21:13:30 | ... == ... | Condition always evaluates to 'true'. | @@ -19,7 +20,6 @@ | ConstantIfCondition.cs:11:17:11:29 | ... == ... | Condition always evaluates to 'true'. | | ConstantIfCondition.cs:14:17:14:21 | false | Condition always evaluates to 'false'. | | ConstantIfCondition.cs:17:17:17:26 | ... == ... | Condition always evaluates to 'true'. | -| ConstantIfCondition.cs:30:20:30:24 | ... > ... | Condition always evaluates to 'false'. | | ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. | | ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. | | ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. | diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs new file mode 100644 index 000000000000..66af12c057af --- /dev/null +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs @@ -0,0 +1,7 @@ +class Bad +{ + public int Max(int a, int b) + { + return a > a ? a : b; // $ Alert + } +} diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs index 146dbcf56611..04c91cc222da 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs @@ -25,11 +25,6 @@ public void Foo() } } - public int Max(int a, int b) - { - return a > a ? a : b; // $ Alert - } - public int Bar() { return ZERO;