提交 5f2cfc4b 编写于 作者: C Cyrus Najmabadi

Do not add 'new' when there is no conflict in implement-interface.

上级 6140b500
...@@ -8234,5 +8234,42 @@ public int Baz([AllowNull] int bar) ...@@ -8234,5 +8234,42 @@ public int Baz([AllowNull] int bar)
}} }}
}}"); }}");
} }
[WorkItem(13427, "https://github.com/dotnet/roslyn/issues/13427")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestDoNotAddNewWithGenericAndNonGenericMethods()
{
await TestWithAllCodeStyleOptionsOffAsync(
@"class B
{
public void M<T>() { }
}
interface I
{
void M();
}
class D : B, [|I|]
{
}",
@"class B
{
public void M<T>() { }
}
interface I
{
void M();
}
class D : B, I
{
public void M()
{
throw new System.NotImplementedException();
}
}");
}
} }
} }
...@@ -440,30 +440,19 @@ private ISymbol GenerateEvent(Compilation compilation, string memberName, bool g ...@@ -440,30 +440,19 @@ private ISymbol GenerateEvent(Compilation compilation, string memberName, bool g
return return
baseTypes.Any(ts => ts.GetMembers(memberName) baseTypes.Any(ts => ts.GetMembers(memberName)
.Where(m => m.IsAccessibleWithin(State.ClassOrStructType)) .Where(m => m.IsAccessibleWithin(State.ClassOrStructType))
.Any(m => HasNameConflict(member, memberName, m))); .Any(m => HasNameConflict(member, m)));
} }
private static bool HasNameConflict( private static bool HasNameConflict(ISymbol member, ISymbol baseMember)
ISymbol member,
string memberName,
ISymbol baseMember)
{ {
Debug.Assert(memberName == baseMember.Name); if (member is IMethodSymbol method1 && baseMember is IMethodSymbol method2)
if (member.Kind == SymbolKind.Method && baseMember.Kind == SymbolKind.Method)
{ {
// A method only conflicts with another method if they have the same parameter // A method only conflicts with another method if they have the same parameter
// signature (return type is irrelevant). // signature (return type is irrelevant).
var method1 = (IMethodSymbol)member; return method1.MethodKind == MethodKind.Ordinary &&
var method2 = (IMethodSymbol)baseMember; method2.MethodKind == MethodKind.Ordinary &&
method1.TypeParameters.Length == method2.TypeParameters.Length &&
if (method1.MethodKind == MethodKind.Ordinary && method1.Parameters.SequenceEqual(method2.Parameters, SymbolEquivalenceComparer.Instance.ParameterEquivalenceComparer);
method2.MethodKind == MethodKind.Ordinary &&
method1.TypeParameters.Length == method2.TypeParameters.Length)
{
return method1.Parameters.Select(p => p.Type)
.SequenceEqual(method2.Parameters.Select(p => p.Type));
}
} }
// Any non method members with the same name simple name conflict. // Any non method members with the same name simple name conflict.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册