Skip to content

Fix NRE in BasicOverrideEqualsOnOverloadingOperatorEqualsAnalyzer #6782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
Microsoft.CodeQuality.VisualBasic.Analyzers.ApiDesignGuidelines.BasicOverrideEqualsOnOverloadingOperatorEqualsAnalyzer,
Expand Down Expand Up @@ -167,6 +168,25 @@ End Function
GetBasicResultAt(8, 7));
}

[Fact, WorkItem(6778, "https://github.com/dotnet/roslyn-analyzers/issues/6778")]
public async Task Bad_Structure_WithNonMethodMember_Async()
{
await VerifyVB.VerifyAnalyzerAsync(@"
Structure C
Public Field As Integer

Public Shared Operator =(a As C, b As C)
Return True
End Operator

Public Shared Operator <>(a As C, b As C)
Return True
End Operator
End Structure",
// Test0.vb(2,11): warning CA2224: Override Equals on overloading operator equals
GetBasicResultAt(2, 11));
}

private static DiagnosticResult GetBasicResultAt(int line, int column)
#pragma warning disable RS0030 // Do not use banned APIs
=> VerifyVB.Diagnostic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,13 @@ Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.ApiDesignGuidelines
End If

' If there's a = operator...
If Not HasEqualityOperator(type) Then
If Not type.GetMembers(WellKnownMemberNames.EqualityOperatorName).OfType(Of IMethodSymbol).Any(Function(m) m.MethodKind = MethodKind.UserDefinedOperator) Then
Return
End If

symbolContext.ReportDiagnostic(type.CreateDiagnostic(Rule))
End Sub,
SymbolKind.NamedType)
End Sub

Private Shared Function HasEqualityOperator(type As INamedTypeSymbol) As Boolean
For Each member In type.GetMembers()
Dim method = TryCast(member, IMethodSymbol)
If method?.MethodKind = MethodKind.UserDefinedOperator AndAlso
CaseInsensitiveComparison.Equals(method.Name, WellKnownMemberNames.EqualityOperatorName) Then

Return True
End If
Next

Return False
End Function
End Class
End Namespace