|
| 1 | +using System; |
| 2 | + |
| 3 | +// The missing matching operators deliberately model an incompletely compiled database. |
| 4 | +class IncompleteOperatorTest |
| 5 | +{ |
| 6 | + // GOOD: Rewriting this as `left != right` could recursively call this operator. |
| 7 | + public static bool operator !=(IncompleteOperatorTest left, IncompleteOperatorTest right) |
| 8 | + { |
| 9 | + // BAD: Rewriting this built-in comparison cannot call the enclosing operator. |
| 10 | + bool valuesDiffer = !(left.Value == right.Value); // $ Alert |
| 11 | + |
| 12 | + // BAD: Explicitly converting both operands prevents a call to the enclosing operator. |
| 13 | + bool referencesDiffer = !((object)left == (object)right); // $ Alert |
| 14 | + |
| 15 | + return valuesDiffer && referencesDiffer && !(left == right); |
| 16 | + } |
| 17 | + |
| 18 | + // GOOD: Rewriting this as `left >= right` could recursively call this operator. |
| 19 | + public static bool operator >=(IncompleteOperatorTest left, IncompleteOperatorTest right) => !(left < right); |
| 20 | + |
| 21 | + int Value { get; } |
| 22 | +} |
| 23 | + |
| 24 | +class IncompleteReverseOperatorTest |
| 25 | +{ |
| 26 | + // GOOD: Rewriting this as `left == right` could recursively call this operator. |
| 27 | + public static bool operator ==(IncompleteReverseOperatorTest left, IncompleteReverseOperatorTest right) => !(left != right); |
| 28 | + |
| 29 | + // GOOD: Rewriting this as `left < right` could recursively call this operator. |
| 30 | + public static bool operator <(IncompleteReverseOperatorTest left, IncompleteReverseOperatorTest right) => !(left >= right); |
| 31 | +} |
| 32 | + |
| 33 | +class IncompleteGreaterOperatorTest |
| 34 | +{ |
| 35 | + // GOOD: Rewriting this as `left <= right` could recursively call this operator. |
| 36 | + public static bool operator <=(IncompleteGreaterOperatorTest left, IncompleteGreaterOperatorTest right) => !(left > right); |
| 37 | + |
| 38 | + // GOOD: Rewriting this as `left > right` could recursively call this operator. |
| 39 | + public static bool operator >(IncompleteGreaterOperatorTest left, IncompleteGreaterOperatorTest right) => !(left <= right); |
| 40 | +} |
| 41 | + |
| 42 | +class IncompleteDifferentOperatorTest |
| 43 | +{ |
| 44 | + // BAD: The suggested operator differs from the enclosing operator. |
| 45 | + public static bool operator >(IncompleteDifferentOperatorTest left, IncompleteDifferentOperatorTest right) => !(left == right); // $ Alert |
| 46 | +} |
| 47 | + |
| 48 | +class IncompleteNestedOperatorTest |
| 49 | +{ |
| 50 | + public static bool operator !=(IncompleteNestedOperatorTest left, IncompleteNestedOperatorTest right) |
| 51 | + { |
| 52 | + // GOOD: The replacement could call the enclosing operator from this lambda. |
| 53 | + Func<bool> lambda = () => !(left == right); |
| 54 | + |
| 55 | + // GOOD: The replacement could call the enclosing operator from this local function. |
| 56 | + bool Local() => !(left == right); |
| 57 | + |
| 58 | + return lambda() || Local(); |
| 59 | + } |
| 60 | +} |
0 commit comments