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

Do not offer to convert object initializers to a compound assignment. #33467

Merged
merged 1 commit into from
Feb 19, 2019
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 @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseCompoundAssignment
Expand Down Expand Up @@ -739,6 +740,26 @@ void M(int a, int b)
{
b = (a += 10);
}
}");
}

[WorkItem(33382, "https://github.com/dotnet/roslyn/issues/33382")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestNotOnObjectInitializer()
{
await TestMissingAsync(
@"
struct InsertionPoint
{
int level;

InsertionPoint Up()
{
return new InsertionPoint
{
level [||]= level - 1,
};
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -107,6 +105,13 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
return;
}

// Don't offer if this is `x = x + 1` inside an obj initializer like:
// `new Point { x = x + 1 }`
if (_syntaxFacts.IsObjectInitializerNamedAssignmentIdentifier(assignmentLeft))
{
return;
}

// Syntactically looks promising. But we can only safely do this if 'expr'
// is side-effect-free since we will be changing the number of times it is
// executed from twice to once.
Expand Down