Skip to content

Fixed incorrect suggestion for collection initialization simplification#23721

Merged
sharwell merged 5 commits intodotnet:masterfrom
zaytsev-victor:Fixed23672
May 4, 2018
Merged

Fixed incorrect suggestion for collection initialization simplification#23721
sharwell merged 5 commits intodotnet:masterfrom
zaytsev-victor:Fixed23672

Conversation

@zaytsev-victor
Copy link
Copy Markdown
Contributor

Fixes #23672

Ask Mode template not completed

Customer scenario

What does the customer do to get into this situation, and why do we think this
is common enough to address for this release. (Granted, sometimes this will be
obvious "Open project, VS crashes" but in general, I need to understand how
common a scenario is)

Bugs this fixes

(either VSO or GitHub links)

Workarounds, if any

Also, why we think they are insufficient for RC vs. RC2, RC3, or RTW

Risk

This is generally a measure our how central the affected code is to adjacent
scenarios and thus how likely your fix is to destabilize a broader area of code

Performance impact

(with a brief justification for that assessment (e.g. "Low perf impact because no extra allocations/no complexity changes" vs. "Low")

Is this a regression from a previous update?

Root cause analysis

How did we miss it? What tests are we adding to guard against it in the future?

How was the bug found?

(E.g. customer reported it vs. ad hoc testing)

Test documentation updated?

If this is a new non-compiler feature or a significant improvement to an existing feature, update https://github.com/dotnet/roslyn/wiki/Manual-Testing once you know which release it is targeting.

@zaytsev-victor zaytsev-victor requested a review from a team as a code owner December 11, 2017 14:26
_syntaxFacts.GetNameAndArityOfSimpleName(memberName, out var name, out var arity);

if (arity != 0 || !name.Equals(nameof(IList.Add)))
if (arity != 0 || !name.Equals(nameof(IList.Add)) || IsExplicitlyImplemented(memberName))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Will this run for cases where the local variable has the same type as the constructed object, e.g. when using var? If so, can we special case the behavior for this to avoid expensive calls to GetSymbolInfo?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll check it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharwell Should we provide suggestion in this case:

var obj = new ExpandoObject();
obj.Add("string", "v");
obj.Add("int", 1);
obj.Add("object", new { X=1, Y=2 });

This code won't compile but we have suggestion to use collection initialization

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with either way in that scenario, so whatever's more efficient.


protected override bool ShouldAnalize()
=> _semanticModel.GetTypeInfo(_objectCreationExpression, default).Type
.GetMembers(WellKnownMemberNames.CollectionInitializerAddMethodName)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. .Type may return null, so this would crash on GetMembers.
  2. Do not pass default for the cancellation token. We should have an actual cancellation token that can be passsed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This won't pick up inherited .Add methods, or extension .Add methods.

@jcouv jcouv added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Mar 31, 2018
return null;
}

if (!ShouldAnalize())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: ShouldAnalyze

@jcouv jcouv added the Area-IDE label Mar 31, 2018
@jcouv jcouv added this to the 15.8 milestone Mar 31, 2018
@jinujoseph
Copy link
Copy Markdown
Contributor

test windows_debug_spanish_unit32_prtest

.Any(x => x is IMethodSymbol m && m.DeclaredAccessibility == Accessibility.Public);
protected override bool ShouldAnalyze()
{
var type = _semanticModel.GetTypeInfo(_objectCreationExpression, _cancellationToken).Type;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This should not cause a performance problem because this expression was already being used on this this code path:

var objectType = context.SemanticModel.GetTypeInfo(objectCreationExpression, cancellationToken);
if (objectType.Type == null || !objectType.Type.AllInterfaces.Contains(ienumerableType))

@sharwell
Copy link
Copy Markdown
Contributor

sharwell commented May 4, 2018

@jinujoseph for approval to merge

@jinujoseph
Copy link
Copy Markdown
Contributor

Approved to merge for 15.8.Preview2

@sharwell sharwell merged commit 6a4acdb into dotnet:master May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved to merge Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalid IDE0028 (explicit interface implementation)

5 participants