未验证 提交 6a4acdbd 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #23721 from zaytsev-victor/Fixed23672

Fixed incorrect suggestion for collection initialization simplification
......@@ -1010,6 +1010,27 @@ public void Main()
int horse = 1;
}
}");
}
[WorkItem(23672, "https://github.com/dotnet/roslyn/issues/23672")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCollectionInitializer)]
public async Task TestMissingWithExplicitImplementedAddMethod()
{
await TestMissingInRegularAndScriptAsync(
@"
using System.Collections.Generic;
using System.Dynamic;
public class Goo
{
public void M()
{
IDictionary<string, object> obj = [||]new ExpandoObject();
obj.Add(""string"", ""v"");
obj.Add(""int"", 1);
obj.Add("" object"", new { X = 1, Y = 2 });
}
}");
}
}
......
......@@ -307,6 +307,22 @@ Class C
2
}
End Sub
End Class")
End Function
<WorkItem(23672, "https://github.com/dotnet/roslyn/pull/23672")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCollectionInitializer)>
Public Async Function TestMissingWithExplicitImplementedAddMethod() As Task
Await TestMissingInRegularAndScriptAsync(
"
Imports System.Dynamic
Imports System.Collections.Generic
Class C
Sub M()
Dim obj As IDictionary(Of String, Object) = [||]New ExpandoObject()
obj.Add(""string"", ""v"")
obj.Add(""int"", 1)
End Sub
End Class")
End Function
End Class
......
......@@ -66,6 +66,11 @@ protected void Clear()
return null;
}
if (!ShouldAnalyze())
{
return null;
}
_containingStatement = _objectCreationExpression.FirstAncestorOrSelf<TStatementSyntax>();
if (_containingStatement == null)
{
......@@ -179,5 +184,7 @@ protected bool ExpressionContainsValuePatternOrReferencesInitializedSymbol(Synta
return false;
}
protected abstract bool ShouldAnalyze();
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.UseCollectionInitializer
{
......@@ -120,6 +118,23 @@ protected override void AddMatches(ArrayBuilder<TExpressionStatementSyntax> matc
}
}
protected override bool ShouldAnalyze()
{
var type = _semanticModel.GetTypeInfo(_objectCreationExpression, _cancellationToken).Type;
if (type == null)
{
return false;
}
var addMethods = _semanticModel.LookupSymbols(
_objectCreationExpression.SpanStart,
container: type,
name: WellKnownMemberNames.CollectionInitializerAddMethodName,
includeReducedExtensionMethods: true);
return addMethods.Any(m => m is IMethodSymbol methodSymbol && methodSymbol.Parameters.Any());
}
private bool TryAnalyzeIndexAssignment(
TExpressionStatementSyntax statement,
out SyntaxNode instance)
......@@ -200,7 +215,7 @@ protected override void AddMatches(ArrayBuilder<TExpressionStatementSyntax> matc
_syntaxFacts.GetPartsOfMemberAccessExpression(memberAccess, out var localInstance, out var memberName);
_syntaxFacts.GetNameAndArityOfSimpleName(memberName, out var name, out var arity);
if (arity != 0 || !name.Equals(nameof(IList.Add)))
if (arity != 0 || !name.Equals(WellKnownMemberNames.CollectionInitializerAddMethodName))
{
return false;
}
......
......@@ -164,6 +164,8 @@ protected override void AddMatches(ArrayBuilder<Match<TExpressionSyntax, TStatem
}
}
protected override bool ShouldAnalyze() => true;
private bool ImplicitMemberAccessWouldBeAffected(SyntaxNode node)
{
if (node != null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册