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

Fix simplify lambda expression with unrelated assignment in other method #71324

Merged
merged 2 commits into from
Dec 18, 2023
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 @@ -183,8 +183,11 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol?
{
// Limit the search space to the outermost code block that could contain references to this expr (or
// fall back to compilation unit for top level statements).
var outermostBody = invokedExpression.AncestorsAndSelf().Last(
n => n is BlockSyntax or ArrowExpressionClauseSyntax or AnonymousFunctionExpressionSyntax or CompilationUnitSyntax);
var outermostBody = invokedExpression.AncestorsAndSelf().LastOrDefault(
n => n is BlockSyntax or ArrowExpressionClauseSyntax or AnonymousFunctionExpressionSyntax or GlobalStatementSyntax);
Copy link
Member

@sharwell sharwell Dec 19, 2023

Choose a reason for hiding this comment

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

Do we need to consider field or property initializers here?

if (outermostBody is null or GlobalStatementSyntax)
outermostBody = syntaxTree.GetRoot(cancellationToken);
Copy link
Member Author

Choose a reason for hiding this comment

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

the prior walk always hit the compilation unit. which meant that we were always checking everything (Even unrelated code). now we keep walking up, and only go to the compilation unit if we were in a global statement.


foreach (var candidate in outermostBody.DescendantNodes().OfType<ExpressionSyntax>())
{
if (candidate != accessedExpression &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryLambdaExpression;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -1971,5 +1970,70 @@ internal Helper(Action<bool> action)

""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71300")]
public async Task TestWithWriteInOtherMethod()
{
await TestInRegularAndScriptAsync("""
using System;
using System.Linq;

public class Repro
{
private readonly MethodProvider _methodProvider;

public Repro(MethodProvider methodProvider)
{
// Assignment that should not block feature.
_methodProvider = methodProvider;
}

public void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
string[] asStrings = numbers.Select([|x => |]_methodProvider.ToStr(x)).ToArray();
Console.WriteLine(asStrings.Length);
}
}

public class MethodProvider
{
public string ToStr(int x)
{
return x.ToString();
}
}
""",
"""
using System;
using System.Linq;

public class Repro
{
private readonly MethodProvider _methodProvider;

public Repro(MethodProvider methodProvider)
{
// Assignment that should not block feature.
_methodProvider = methodProvider;
}

public void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
string[] asStrings = numbers.Select(_methodProvider.ToStr).ToArray();
Console.WriteLine(asStrings.Length);
}
}

public class MethodProvider
{
public string ToStr(int x)
{
return x.ToString();
}
}
""");
}
}
}