Skip to content
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
Expand Up @@ -64,7 +64,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (targetOverload is not null)
{
TryGetComparerParameterInfo(currentMethod, targetOverload, equalityComparerOpenType, comparerOpenType, out insertionIndex, out parameterName);
// The operations use the method that is not reduced, so the form of the call is only known from the symbol of the syntax
var isReducedCall = semanticModel.GetSymbolInfo(nodeToFix, context.CancellationToken).Symbol is IMethodSymbol { MethodKind: MethodKind.ReducedExtension };
TryGetComparerParameterInfo(currentMethod, targetOverload, isReducedCall, equalityComparerOpenType, comparerOpenType, out insertionIndex, out parameterName);
}
}

Expand All @@ -88,12 +90,13 @@ void RegisterCodeFix(string comparerName)
}
}

private static bool TryGetComparerParameterInfo(IMethodSymbol method, IMethodSymbol overload, INamedTypeSymbol? equalityComparerOpenType, INamedTypeSymbol? comparerOpenType, out int insertionIndex, out string parameterName)
/// <param name="isReducedCall">Indicates whether the extension method is called as an extension method (<c>source.Contains(value)</c>). When it is called as a static method (<c>Enumerable.Contains(source, value)</c>), the instance is its first argument.</param>
private static bool TryGetComparerParameterInfo(IMethodSymbol method, IMethodSymbol overload, bool isReducedCall, INamedTypeSymbol? equalityComparerOpenType, INamedTypeSymbol? comparerOpenType, out int insertionIndex, out string parameterName)
{
// Use comparable parameters to correctly handle extension methods:
// strip the implicit 'this' parameter so indices align with the argument list.
var methodParams = GetComparableParameters(method);
var overloadParams = GetComparableParameters(overload);
var methodParams = GetComparableParameters(method, isReducedCall);
var overloadParams = GetComparableParameters(overload, isReducedCall);

for (var i = 0; i < overloadParams.Length; i++)
{
Expand Down Expand Up @@ -122,14 +125,14 @@ private static bool TryGetComparerParameterInfo(IMethodSymbol method, IMethodSym
/// Returns the parameters that correspond to actual arguments in the invocation/creation argument list,
/// i.e. excluding the implicit 'this' receiver for extension methods.
/// </summary>
private static ImmutableArray<IParameterSymbol> GetComparableParameters(IMethodSymbol method)
private static ImmutableArray<IParameterSymbol> GetComparableParameters(IMethodSymbol method, bool isReducedCall)
{
// Reduced extension method: Parameters already exclude 'this'.
if (method.MethodKind is MethodKind.ReducedExtension)
return method.Parameters;

// Non-reduced extension method: the first parameter is 'this' and is not an argument.
if (method.IsExtensionMethod && method.Parameters.Length > 0)
// Non-reduced extension method called as an extension method: the first parameter is 'this' and is not an argument.
if (isReducedCall && method.IsExtensionMethod && method.Parameters.Length > 0)
return method.Parameters.RemoveAt(0);

return method.Parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2195,4 +2195,28 @@ sealed class CustomList : System.Collections.Generic.List<string>, IContainer

return test.RunAsync();
}

[Fact]
public Task EnumerableContains_StaticCall_CodeFix()
{
var test = CreateTest();
test.TestCode = """
using System.Collections.Generic;
using System.Linq;
class Sample
{
bool Test(HashSet<string> set, string value) => Enumerable.[|Contains(set, value)|];
}
""";
test.FixedCode = """
using System.Collections.Generic;
using System.Linq;
class Sample
{
bool Test(HashSet<string> set, string value) => Enumerable.Contains(set, value, System.StringComparer.Ordinal);
}
""";

return test.RunAsync();
}
}
Loading