diff --git a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs index b15aac7223..28caf01ab4 100644 --- a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs +++ b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs @@ -102,14 +102,12 @@ public virtual int CompareTo(QualityQuery? other) } // LUCENENET specific - provide Equals and GetHashCode due to providing operator overrides - protected bool Equals(QualityQuery? other) => queryID == other?.queryID; - public override bool Equals(object? obj) { if (obj is null) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != GetType()) return false; - return Equals((QualityQuery)obj); + return queryID == ((QualityQuery)obj).queryID; } public override int GetHashCode() => queryID.GetHashCode(); diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs b/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs index 5badc34b34..0260601bd6 100644 --- a/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs +++ b/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs @@ -471,26 +471,28 @@ public override bool Equals(object obj) } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator <(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator <=(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator >(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator >=(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator ==(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(WeightedPhraseInfo left, WeightedPhraseInfo right) + public static bool operator !=(WeightedPhraseInfo? left, WeightedPhraseInfo? right) => !(left == right); + #nullable restore #endregion /// @@ -567,26 +569,28 @@ public override string ToString() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(Toffs left, Toffs right) + public static bool operator <(Toffs? left, Toffs? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(Toffs left, Toffs right) + public static bool operator <=(Toffs? left, Toffs? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(Toffs left, Toffs right) + public static bool operator >(Toffs? left, Toffs? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(Toffs left, Toffs right) + public static bool operator >=(Toffs? left, Toffs? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(Toffs left, Toffs right) + public static bool operator ==(Toffs? left, Toffs? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(Toffs left, Toffs right) + public static bool operator !=(Toffs? left, Toffs? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/FieldTermStack.cs b/src/Lucene.Net.Highlighter/VectorHighlight/FieldTermStack.cs index 4393af3931..974ec52f22 100644 --- a/src/Lucene.Net.Highlighter/VectorHighlight/FieldTermStack.cs +++ b/src/Lucene.Net.Highlighter/VectorHighlight/FieldTermStack.cs @@ -286,26 +286,28 @@ public override bool Equals(object obj) } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(TermInfo left, TermInfo right) + public static bool operator <(TermInfo? left, TermInfo? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(TermInfo left, TermInfo right) + public static bool operator <=(TermInfo? left, TermInfo? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(TermInfo left, TermInfo right) + public static bool operator >(TermInfo? left, TermInfo? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(TermInfo left, TermInfo right) + public static bool operator >=(TermInfo? left, TermInfo? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(TermInfo left, TermInfo right) + public static bool operator ==(TermInfo? left, TermInfo? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(TermInfo left, TermInfo right) + public static bool operator !=(TermInfo? left, TermInfo? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs b/src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs index 69641bd73f..81e306ea11 100644 --- a/src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs +++ b/src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs @@ -2,6 +2,7 @@ using Lucene.Net.Index; using System; using System.Text; +#pragma warning disable CS0660, CS0661 - CompareTo is deprecated, so skipping implementing equality members (lucenenet#683) namespace Lucene.Net.QueryParsers.Surround.Query { @@ -117,30 +118,32 @@ public override Search.Query MakeLuceneQueryFieldNoBoost(string fieldName, Basic } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators // NOTE: The CompareTo method is marked as obsolete, but we still need to implement the comparison operators // since this is public in 4.8. Suppressing the obsolete warning here. #pragma warning disable CS0618 // Type or member is obsolete - public static bool operator <(SimpleTerm left, SimpleTerm right) + public static bool operator <(SimpleTerm? left, SimpleTerm? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(SimpleTerm left, SimpleTerm right) + public static bool operator <=(SimpleTerm? left, SimpleTerm? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(SimpleTerm left, SimpleTerm right) + public static bool operator >(SimpleTerm? left, SimpleTerm? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(SimpleTerm left, SimpleTerm right) + public static bool operator >=(SimpleTerm? left, SimpleTerm? right) => left is null ? right is null : left.CompareTo(right) >= 0; #pragma warning restore CS0618 // Type or member is obsolete - public static bool operator ==(SimpleTerm left, SimpleTerm right) + public static bool operator ==(SimpleTerm? left, SimpleTerm? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(SimpleTerm left, SimpleTerm right) + public static bool operator !=(SimpleTerm? left, SimpleTerm? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs b/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs index a18ffe7aa3..04fef1c15c 100644 --- a/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs +++ b/src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs @@ -46,10 +46,10 @@ public sealed class Completion : IComparable { /// /// UTF-8 bytes of the suggestion - public BytesRef Utf8 { get; private set; } + public BytesRef Utf8 { get; } /// /// source bucket (weight) of the suggestion - public int Bucket { get; private set; } + public int Bucket { get; } internal Completion(BytesRef key, int bucket) { @@ -68,27 +68,40 @@ public int CompareTo(Completion o) return this.Utf8.CompareTo(o.Utf8); } + // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode + public override bool Equals(object obj) => ReferenceEquals(this, obj); + + public override int GetHashCode() + { + unchecked + { + return (Utf8.GetHashCode() * 397) ^ Bucket; + } + } + #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(Completion left, Completion right) + public static bool operator <(Completion? left, Completion? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(Completion left, Completion right) + public static bool operator <=(Completion? left, Completion? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(Completion left, Completion right) + public static bool operator >(Completion? left, Completion? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(Completion left, Completion right) + public static bool operator >=(Completion? left, Completion? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(Completion left, Completion right) + public static bool operator ==(Completion? left, Completion? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(Completion left, Completion right) + public static bool operator !=(Completion? left, Completion? right) => !(left == right); + #nullable restore #endregion } diff --git a/src/Lucene.Net.Suggest/Suggest/Lookup.cs b/src/Lucene.Net.Suggest/Suggest/Lookup.cs index 2920ca3c05..6b023c401c 100644 --- a/src/Lucene.Net.Suggest/Suggest/Lookup.cs +++ b/src/Lucene.Net.Suggest/Suggest/Lookup.cs @@ -39,25 +39,25 @@ public sealed class LookupResult : IComparable { /// /// the key's text - public string Key { get; private set; } + public string Key { get; } /// /// Expert: custom Object to hold the result of a /// highlighted suggestion. /// - public object HighlightKey { get; private set; } + public object HighlightKey { get; } /// /// the key's weight - public long Value { get; private set; } + public long Value { get; } /// /// the key's payload (null if not present) - public BytesRef Payload { get; private set; } + public BytesRef Payload { get; } /// /// the key's contexts (null if not present) - public IEnumerable Contexts { get; private set; } + public IEnumerable Contexts { get; } /// /// Create a new result from a key+weight pair. @@ -123,6 +123,22 @@ public int CompareTo(LookupResult o) return CHARSEQUENCE_COMPARER.Compare(Key, o.Key); } + // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode + public override bool Equals(object obj) => ReferenceEquals(this, obj); + + public override int GetHashCode() + { + unchecked + { + var hashCode = (Key != null ? Key.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (HighlightKey != null ? HighlightKey.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ Value.GetHashCode(); + hashCode = (hashCode * 397) ^ (Payload != null ? Payload.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (Contexts != null ? Contexts.GetHashCode() : 0); + return hashCode; + } + } + #region Operator overrides // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators diff --git a/src/Lucene.Net/Index/IndexCommit.cs b/src/Lucene.Net/Index/IndexCommit.cs index 8d85907696..85abd478ac 100644 --- a/src/Lucene.Net/Index/IndexCommit.cs +++ b/src/Lucene.Net/Index/IndexCommit.cs @@ -147,26 +147,28 @@ public virtual int CompareTo(IndexCommit commit) } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(IndexCommit left, IndexCommit right) + public static bool operator <(IndexCommit? left, IndexCommit? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(IndexCommit left, IndexCommit right) + public static bool operator <=(IndexCommit? left, IndexCommit? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(IndexCommit left, IndexCommit right) + public static bool operator >(IndexCommit? left, IndexCommit? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(IndexCommit left, IndexCommit right) + public static bool operator >=(IndexCommit? left, IndexCommit? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(IndexCommit left, IndexCommit right) + public static bool operator ==(IndexCommit? left, IndexCommit? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(IndexCommit left, IndexCommit right) + public static bool operator !=(IndexCommit? left, IndexCommit? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Index/Term.cs b/src/Lucene.Net/Index/Term.cs index dc21ea40d6..1c6895aced 100644 --- a/src/Lucene.Net/Index/Term.cs +++ b/src/Lucene.Net/Index/Term.cs @@ -191,26 +191,28 @@ public override string ToString() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(Term left, Term right) + public static bool operator <(Term? left, Term? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(Term left, Term right) + public static bool operator <=(Term? left, Term? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(Term left, Term right) + public static bool operator >(Term? left, Term? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(Term left, Term right) + public static bool operator >=(Term? left, Term? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(Term left, Term right) + public static bool operator ==(Term? left, Term? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(Term left, Term right) + public static bool operator !=(Term? left, Term? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Util/Automaton/State.cs b/src/Lucene.Net/Util/Automaton/State.cs index 83aab6c0d5..1191dd62a9 100644 --- a/src/Lucene.Net/Util/Automaton/State.cs +++ b/src/Lucene.Net/Util/Automaton/State.cs @@ -376,26 +376,28 @@ public override int GetHashCode() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(State left, State right) + public static bool operator <(State? left, State? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(State left, State right) + public static bool operator <=(State? left, State? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(State left, State right) + public static bool operator >(State? left, State? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(State left, State right) + public static bool operator >=(State? left, State? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(State left, State right) + public static bool operator ==(State? left, State? right) => left is null ? right is null : ReferenceEquals(left, right); - public static bool operator !=(State left, State right) + public static bool operator !=(State? left, State? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Util/BytesRef.cs b/src/Lucene.Net/Util/BytesRef.cs index 78be76cb65..9946756301 100644 --- a/src/Lucene.Net/Util/BytesRef.cs +++ b/src/Lucene.Net/Util/BytesRef.cs @@ -402,26 +402,28 @@ public bool IsValid() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(BytesRef left, BytesRef right) + public static bool operator <(BytesRef? left, BytesRef? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(BytesRef left, BytesRef right) + public static bool operator <=(BytesRef? left, BytesRef? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(BytesRef left, BytesRef right) + public static bool operator >(BytesRef? left, BytesRef? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(BytesRef left, BytesRef right) + public static bool operator >=(BytesRef? left, BytesRef? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(BytesRef left, BytesRef right) + public static bool operator ==(BytesRef? left, BytesRef? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(BytesRef left, BytesRef right) + public static bool operator !=(BytesRef? left, BytesRef? right) => !(left == right); + #nullable restore #endregion } diff --git a/src/Lucene.Net/Util/CharsRef.cs b/src/Lucene.Net/Util/CharsRef.cs index 835a342557..78c1436652 100644 --- a/src/Lucene.Net/Util/CharsRef.cs +++ b/src/Lucene.Net/Util/CharsRef.cs @@ -445,26 +445,28 @@ public bool IsValid() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(CharsRef left, CharsRef right) + public static bool operator <(CharsRef? left, CharsRef? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(CharsRef left, CharsRef right) + public static bool operator <=(CharsRef? left, CharsRef? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(CharsRef left, CharsRef right) + public static bool operator >(CharsRef? left, CharsRef? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(CharsRef left, CharsRef right) + public static bool operator >=(CharsRef? left, CharsRef? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(CharsRef left, CharsRef right) + public static bool operator ==(CharsRef? left, CharsRef? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(CharsRef left, CharsRef right) + public static bool operator !=(CharsRef? left, CharsRef? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Util/IntsRef.cs b/src/Lucene.Net/Util/IntsRef.cs index 803aa04873..53216c4a51 100644 --- a/src/Lucene.Net/Util/IntsRef.cs +++ b/src/Lucene.Net/Util/IntsRef.cs @@ -295,26 +295,28 @@ public bool IsValid() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(Int32sRef left, Int32sRef right) + public static bool operator <(Int32sRef? left, Int32sRef? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(Int32sRef left, Int32sRef right) + public static bool operator <=(Int32sRef? left, Int32sRef? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(Int32sRef left, Int32sRef right) + public static bool operator >(Int32sRef? left, Int32sRef? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(Int32sRef left, Int32sRef right) + public static bool operator >=(Int32sRef? left, Int32sRef? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(Int32sRef left, Int32sRef right) + public static bool operator ==(Int32sRef? left, Int32sRef? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(Int32sRef left, Int32sRef right) + public static bool operator !=(Int32sRef? left, Int32sRef? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Util/LongsRef.cs b/src/Lucene.Net/Util/LongsRef.cs index a90914fbe1..efa43da6db 100644 --- a/src/Lucene.Net/Util/LongsRef.cs +++ b/src/Lucene.Net/Util/LongsRef.cs @@ -294,26 +294,28 @@ public bool IsValid() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(Int64sRef left, Int64sRef right) + public static bool operator <(Int64sRef? left, Int64sRef? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(Int64sRef left, Int64sRef right) + public static bool operator <=(Int64sRef? left, Int64sRef? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(Int64sRef left, Int64sRef right) + public static bool operator >(Int64sRef? left, Int64sRef? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(Int64sRef left, Int64sRef right) + public static bool operator >=(Int64sRef? left, Int64sRef? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(Int64sRef left, Int64sRef right) + public static bool operator ==(Int64sRef? left, Int64sRef? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(Int64sRef left, Int64sRef right) + public static bool operator !=(Int64sRef? left, Int64sRef? right) => !(left == right); + #nullable restore #endregion } } diff --git a/src/Lucene.Net/Util/Mutable/MutableValue.cs b/src/Lucene.Net/Util/Mutable/MutableValue.cs index 3701c36628..a1ec7dadc3 100644 --- a/src/Lucene.Net/Util/Mutable/MutableValue.cs +++ b/src/Lucene.Net/Util/Mutable/MutableValue.cs @@ -93,26 +93,28 @@ public override string ToString() } #region Operator overrides + #nullable enable // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(MutableValue left, MutableValue right) + public static bool operator <(MutableValue? left, MutableValue? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(MutableValue left, MutableValue right) + public static bool operator <=(MutableValue? left, MutableValue? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(MutableValue left, MutableValue right) + public static bool operator >(MutableValue? left, MutableValue? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(MutableValue left, MutableValue right) + public static bool operator >=(MutableValue? left, MutableValue? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(MutableValue left, MutableValue right) + public static bool operator ==(MutableValue? left, MutableValue? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(MutableValue left, MutableValue right) + public static bool operator !=(MutableValue? left, MutableValue? right) => !(left == right); + #nullable restore #endregion } }