|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +class MissedFirstOrDefaultOpportunity |
| 6 | +{ |
| 7 | + public Operation M1(IEnumerable<Operation> operations, string operationId) |
| 8 | + { |
| 9 | + // BAD: Can be replaced with operations.FirstOrDefault(operation => ...). |
| 10 | + foreach (var operation in operations) |
| 11 | + { |
| 12 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 13 | + return operation; |
| 14 | + } // $ Alert |
| 15 | + |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + public int M2(IEnumerable<int> values) |
| 20 | + { |
| 21 | + // BAD: Can be replaced with values.FirstOrDefault(value => ...). |
| 22 | + foreach (var value in values) |
| 23 | + { |
| 24 | + if (value > 0) |
| 25 | + { |
| 26 | + return value; |
| 27 | + } |
| 28 | + } // $ Alert |
| 29 | + |
| 30 | + return default; |
| 31 | + } |
| 32 | + |
| 33 | + public int? M3(List<int> values) |
| 34 | + { |
| 35 | + // BAD: Can be replaced with values.FirstOrDefault(value => ...). |
| 36 | + foreach (var value in values) |
| 37 | + { |
| 38 | + if (value > 0) |
| 39 | + return value; |
| 40 | + } // $ Alert |
| 41 | + |
| 42 | + return default(int); |
| 43 | + } |
| 44 | + |
| 45 | + public Operation M4(IEnumerable<Operation> operations, string operationId) |
| 46 | + { |
| 47 | + // GOOD: FirstOrDefault does not throw when a match is found. |
| 48 | + foreach (var operation in operations) |
| 49 | + { |
| 50 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 51 | + throw new InvalidOperationException(); |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + public Operation M5(IEnumerable<Operation> operations, string operationId) |
| 58 | + { |
| 59 | + // GOOD: FirstOrDefault would return null/default when no match is found. |
| 60 | + foreach (var operation in operations) |
| 61 | + { |
| 62 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 63 | + return operation; |
| 64 | + } |
| 65 | + |
| 66 | + return new Operation(); |
| 67 | + } |
| 68 | + |
| 69 | + public string M6(IEnumerable<Operation> operations, string operationId) |
| 70 | + { |
| 71 | + // GOOD: FirstOrDefault would return the matching operation, not one of its properties. |
| 72 | + foreach (var operation in operations) |
| 73 | + { |
| 74 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 75 | + return operation.OperationId; |
| 76 | + } |
| 77 | + |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + public Operation M7(IEnumerable<Operation> operations, string operationId) |
| 82 | + { |
| 83 | + // GOOD: The matched case has an additional side effect. |
| 84 | + foreach (var operation in operations) |
| 85 | + { |
| 86 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 87 | + { |
| 88 | + Console.WriteLine(operation.OperationId); |
| 89 | + return operation; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + public async Task<Operation> M8(IEnumerable<Operation> operations, string operationId) |
| 97 | + { |
| 98 | + // GOOD: FirstOrDefault does not support an async predicate. |
| 99 | + foreach (var operation in operations) |
| 100 | + { |
| 101 | + if (await IsMatch(operation, operationId)) |
| 102 | + return operation; |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + public Operation M9(IEnumerable<Operation> operations, string operationId) |
| 109 | + { |
| 110 | + // GOOD: FirstOrDefault does not have an equivalent for an else branch in the loop. |
| 111 | + foreach (var operation in operations) |
| 112 | + { |
| 113 | + if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)) |
| 114 | + return operation; |
| 115 | + else |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + private static Task<bool> IsMatch(Operation operation, string operationId) => |
| 123 | + Task.FromResult(string.Equals(operation.OperationId, operationId, StringComparison.Ordinal)); |
| 124 | +} |
| 125 | + |
| 126 | +class Operation |
| 127 | +{ |
| 128 | + public string OperationId { get; set; } |
| 129 | +} |
0 commit comments