diff --git a/src/Compilers/Core/Portable/CommandLine/CommonCompiler.SuppressionDiagnostic.cs b/src/Compilers/Core/Portable/CommandLine/CommonCompiler.SuppressionDiagnostic.cs index dd2fe205ad0c8..401977bfd76b6 100644 --- a/src/Compilers/Core/Portable/CommandLine/CommonCompiler.SuppressionDiagnostic.cs +++ b/src/Compilers/Core/Portable/CommandLine/CommonCompiler.SuppressionDiagnostic.cs @@ -69,15 +69,15 @@ public override string GetMessage(IFormatProvider formatProvider = null) public override bool Equals(Diagnostic obj) { - var other = obj as SuppressionDiagnostic; - if (other == null) + if (ReferenceEquals(this, obj)) { - return false; + return true; } - if (ReferenceEquals(this, other)) + var other = obj as SuppressionDiagnostic; + if (other == null) { - return true; + return false; } return Equals(_originalDiagnostic, other._originalDiagnostic) && diff --git a/src/Compilers/Core/Portable/Diagnostic/Diagnostic.DiagnosticWithProgrammaticSuppression.cs b/src/Compilers/Core/Portable/Diagnostic/Diagnostic.DiagnosticWithProgrammaticSuppression.cs index eed63550dbc27..75c6af395ec26 100644 --- a/src/Compilers/Core/Portable/Diagnostic/Diagnostic.DiagnosticWithProgrammaticSuppression.cs +++ b/src/Compilers/Core/Portable/Diagnostic/Diagnostic.DiagnosticWithProgrammaticSuppression.cs @@ -87,15 +87,15 @@ public override ImmutableDictionary Properties public override bool Equals(Diagnostic? obj) { - var other = obj as DiagnosticWithProgrammaticSuppression; - if (other == null) + if (ReferenceEquals(this, obj)) { - return false; + return true; } - if (ReferenceEquals(this, other)) + var other = obj as DiagnosticWithProgrammaticSuppression; + if (other == null) { - return true; + return false; } return Equals(_originalUnsuppressedDiagnostic, other._originalUnsuppressedDiagnostic) && diff --git a/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs b/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs index c945e26eebbc6..5dab028d82387 100644 --- a/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs +++ b/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs @@ -173,6 +173,11 @@ internal DiagnosticDescriptor( public bool Equals(DiagnosticDescriptor? other) { + if (ReferenceEquals(this, other)) + { + return true; + } + return other != null && this.Category == other.Category && diff --git a/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs b/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs index 3863e82718e4d..d227221baeada 100644 --- a/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs +++ b/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs @@ -150,7 +150,7 @@ public override bool Equals(object? obj) public override bool Equals(Diagnostic? obj) { - if (this == obj) + if (ReferenceEquals(this, obj)) { return true; } diff --git a/src/Compilers/Core/Portable/Diagnostic/Diagnostic_SimpleDiagnostic.cs b/src/Compilers/Core/Portable/Diagnostic/Diagnostic_SimpleDiagnostic.cs index 1906545d14b9c..0abef2a8b0ed8 100644 --- a/src/Compilers/Core/Portable/Diagnostic/Diagnostic_SimpleDiagnostic.cs +++ b/src/Compilers/Core/Portable/Diagnostic/Diagnostic_SimpleDiagnostic.cs @@ -146,6 +146,11 @@ public override ImmutableDictionary Properties public override bool Equals(Diagnostic? obj) { + if (ReferenceEquals(this, obj)) + { + return true; + } + var other = obj as SimpleDiagnostic; if (other == null) { diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/Suppression.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/Suppression.cs index eb057bbe62a7e..a32c2fc8dbf04 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/Suppression.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/Suppression.cs @@ -3,14 +3,16 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Diagnostics; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Diagnostics { /// /// Programmatic suppression of a by a . /// - public struct Suppression + public struct Suppression : IEquatable { private Suppression(SuppressionDescriptor descriptor, Diagnostic suppressedDiagnostic) { @@ -48,5 +50,34 @@ public static Suppression Create(SuppressionDescriptor descriptor, Diagnostic su /// Diagnostic suppressed by this suppression. /// public Diagnostic SuppressedDiagnostic { get; } + + public static bool operator ==(Suppression left, Suppression right) + { + return left.Equals(right); + } + + public static bool operator !=(Suppression left, Suppression right) + { + return !(left == right); + } + + public override bool Equals(object obj) + { + return obj is Suppression suppression + && Equals(suppression); + } + + public bool Equals(Suppression other) + { + return EqualityComparer.Default.Equals(Descriptor, other.Descriptor) + && EqualityComparer.Default.Equals(SuppressedDiagnostic, other.SuppressedDiagnostic); + } + + public override int GetHashCode() + { + return Hash.Combine( + EqualityComparer.Default.GetHashCode(Descriptor), + EqualityComparer.Default.GetHashCode(SuppressedDiagnostic)); + } } } diff --git a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt index c1e11777aa68b..fee6c670d9e00 100644 --- a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt @@ -1,5 +1,6 @@ Microsoft.CodeAnalysis.CommandLineSourceFile.CommandLineSourceFile(string path, bool isScript, bool isInputRedirected) -> void Microsoft.CodeAnalysis.CommandLineSourceFile.IsInputRedirected.get -> bool +Microsoft.CodeAnalysis.Diagnostics.Suppression.Equals(Microsoft.CodeAnalysis.Diagnostics.Suppression other) -> bool Microsoft.CodeAnalysis.GeneratorAttribute Microsoft.CodeAnalysis.GeneratorAttribute.GeneratorAttribute() -> void Microsoft.CodeAnalysis.GeneratorDriver @@ -25,5 +26,9 @@ Microsoft.CodeAnalysis.SourceGeneratorContext.ReportDiagnostic(Microsoft.CodeAna Microsoft.CodeAnalysis.SourceGeneratorContext.SyntaxReceiver.get -> Microsoft.CodeAnalysis.ISyntaxReceiver Microsoft.CodeAnalysis.SyntaxReceiverCreator override Microsoft.CodeAnalysis.Diagnostics.AnalyzerFileReference.GetGenerators() -> System.Collections.Immutable.ImmutableArray +override Microsoft.CodeAnalysis.Diagnostics.Suppression.Equals(object obj) -> bool +override Microsoft.CodeAnalysis.Diagnostics.Suppression.GetHashCode() -> int +static Microsoft.CodeAnalysis.Diagnostics.Suppression.operator !=(Microsoft.CodeAnalysis.Diagnostics.Suppression left, Microsoft.CodeAnalysis.Diagnostics.Suppression right) -> bool +static Microsoft.CodeAnalysis.Diagnostics.Suppression.operator ==(Microsoft.CodeAnalysis.Diagnostics.Suppression left, Microsoft.CodeAnalysis.Diagnostics.Suppression right) -> bool virtual Microsoft.CodeAnalysis.Diagnostics.AnalyzerReference.GetGenerators() -> System.Collections.Immutable.ImmutableArray const Microsoft.CodeAnalysis.WellKnownDiagnosticTags.CustomObsolete = "CustomObsolete" -> string