Skip to content
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

Fix #6640 #6670

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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,4 +1,4 @@
// 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.Collections.Generic;
using System.Collections.Immutable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -99,7 +99,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext symbolContext)
// don't report a diagnostic on the `op_False` method because then the user would see two diagnostics for what is really one error
// special-case looking for `IsTrue` instance property
// named properties can't be overloaded so there will only ever be 0 or 1
IPropertySymbol property = typeSymbol.GetMembers(IsTrueText).OfType<IPropertySymbol>().FirstOrDefault();
IPropertySymbol property = typeSymbol.GetBaseTypesAndThis().SelectMany(x => x.GetMembers(IsTrueText).OfType<IPropertySymbol>()).FirstOrDefault();
Copy link
Contributor

@mavasani mavasani Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is the case where using a loop with a break on first found match would be more efficient than linq. A local function would also work.

if (property == null || property.Type.SpecialType != SpecialType.System_Boolean)
{
symbolContext.ReportDiagnostic(CreateDiagnostic(PropertyRule, GetSymbolLocation(methodSymbol), AddAlternateText, IsTrueText, operatorName));
Expand All @@ -125,7 +125,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext symbolContext)
unmatchedMethods.Add(expectedGroup.AlternateMethod2);
}

foreach (IMethodSymbol candidateMethod in typeSymbol.GetMembers().OfType<IMethodSymbol>())
foreach (IMethodSymbol candidateMethod in typeSymbol.GetBaseTypesAndThis().SelectMany(x => x.GetMembers().OfType<IMethodSymbol>()))
{
if (candidateMethod.Name == expectedGroup.AlternateMethod1 || candidateMethod.Name == expectedGroup.AlternateMethod2)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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;
Expand Down Expand Up @@ -68,6 +68,24 @@ public class C
");
}

[Fact]
public Task HasAlternateMethodOnBaseClass_CSharpAsync()
{
return VerifyCS.VerifyAnalyzerAsync(@"
public class B
{
public static D Add(D left, D right) { return new D(); }
public bool IsTrue => true;
}

public class D : B
{
public static D operator +(D left, D right) { return new D(); }
public static bool operator true(D d) => true;
public static bool operator false(D d) => false;
}");
}

[Fact]
public async Task HasMultipleAlternatePrimary_CSharpAsync()
{
Expand Down