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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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 = ClassHierarchy(typeSymbol).SelectMany(x => x.GetMembers(IsTrueText).OfType<IPropertySymbol>()).FirstOrDefault();
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 ClassHierarchy(typeSymbol).SelectMany(x => x.GetMembers().OfType<IMethodSymbol>()))
{
if (candidateMethod.Name == expectedGroup.AlternateMethod1 || candidateMethod.Name == expectedGroup.AlternateMethod2)
{
Expand Down Expand Up @@ -168,6 +168,15 @@ private static void AnalyzeSymbol(SymbolAnalysisContext symbolContext)
}
}

private static IEnumerable<ITypeSymbol> ClassHierarchy(ITypeSymbol typeSymbol)
{
while (typeSymbol is not null)
{
yield return typeSymbol;
typeSymbol = typeSymbol.BaseType;
}
}

NewellClark marked this conversation as resolved.
Show resolved Hide resolved
private static Location GetSymbolLocation(ISymbol symbol)
{
return symbol.OriginalDefinition.Locations.First();
Expand Down
@@ -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