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 all commits
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
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 @@ -113,7 +113,18 @@
IPropertySymbol property = typeSymbol.GetMembers(IsTrueText).OfType<IPropertySymbol>().FirstOrDefault();
if (property == null || property.Type.SpecialType != SpecialType.System_Boolean)
{
symbolContext.ReportDiagnostic(CreateDiagnostic(PropertyRule, GetSymbolLocation(methodSymbol), AddAlternateText, IsTrueText, operatorName));
// 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 = GetIsTrueProperty(typeSymbol);
if (property == null || property.Type.SpecialType != SpecialType.System_Boolean)
{
symbolContext.ReportDiagnostic(CreateDiagnostic(PropertyRule, GetSymbolLocation(methodSymbol), AddAlternateText, IsTrueText, operatorName));
}
else if (!property.IsPublic())
{
symbolContext.ReportDiagnostic(CreateDiagnostic(VisibilityRule, GetSymbolLocation(property), FixVisibilityText, IsTrueText, operatorName));
}
}
else if (!property.IsPublic())
{
Expand All @@ -136,9 +147,7 @@
unmatchedMethods.Add(expectedGroup.AlternateMethod2);
}

foreach (IMethodSymbol candidateMethod in typeSymbol.GetMembers().OfType<IMethodSymbol>())
{
if (candidateMethod.Name == expectedGroup.AlternateMethod1 || candidateMethod.Name == expectedGroup.AlternateMethod2)
foreach (IMethodSymbol candidateMethod in typeSymbol.GetBaseTypesAndThis().SelectMany(x => x.GetMembers().OfType<IMethodSymbol>()))
{
// found an appropriately-named method
matchedMethods.Add(candidateMethod);
Expand Down Expand Up @@ -176,6 +185,23 @@
}
}
}

return;

Check failure on line 189 in src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Debug)

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs#L189

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs(189,13): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'return' in class, record, struct, or interface member declaration

Check failure on line 189 in src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Release)

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs#L189

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs(189,13): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'return' in class, record, struct, or interface member declaration

// Local functions.

static IPropertySymbol? GetIsTrueProperty(ITypeSymbol type)
{
foreach (var baseType in type.GetBaseTypesAndThis())
{
foreach (var member in baseType.GetMembers(IsTrueText).OfType<IPropertySymbol>())
{
return member;
}
}

return null;
}
}

private static Location GetSymbolLocation(ISymbol symbol)
Expand Down Expand Up @@ -275,3 +301,3 @@
}
}
}
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