Skip to content
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 @@ -1774,9 +1774,9 @@ public void M(string s)
{
localFunction(s);

void localFunction(string s)
void localFunction(string s1)
{
_ = {|Rename:s|}.ToString();
_ = {|Rename:s1|}.ToString();
}
}
}
Expand Down Expand Up @@ -1874,4 +1874,145 @@ class C
};
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81400")]
public Task TestDuplicateParameterName_Refactor()
=> TestInRegularAndScriptAsync("""
using System;
class C
{
int M(int v)
{
return [|v + 1|];
}
}
""", """
using System;
class C
{
int M(int v, int v1)
{
return {|Rename:v1|};
}
}
""", index: 0);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81400")]
public Task TestDuplicateParameterName_Trampoline()
=> TestInRegularAndScriptAsync("""
using System;
class C
{
int M(int v)
{
return [|v + 1|];
}

void Caller()
{
M(5);
}
}
""", """
using System;
class C
{
private int GetV1(int v)
{
return v + 1;
}

int M(int v, int v1)
{
return {|Rename:v1|};
}

void Caller()
{
M(5, GetV1(5));
}
}
""", index: 1);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81400")]
public Task TestDuplicateParameterName_Overload()
=> TestInRegularAndScriptAsync("""
using System;
class C
{
int M(int v)
{
return [|v + 1|];
}

void Caller()
{
M(5);
}
}
""", """
using System;
class C
{
private int M(int v)
{
return M(v, v + 1);
}

int M(int v, int v1)
{
return {|Rename:v1|};
}

void Caller()
{
M(5);
}
}
""", index: 2);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81400")]
public Task TestDuplicateParameterName_MultipleConflicts()
=> TestInRegularAndScriptAsync("""
using System;
class C
{
int M(int v, int v1)
{
return [|v + v1 + 1|];
}
}
""", """
using System;
class C
{
int M(int v, int v1, int v2)
{
return {|Rename:v2|};
}
}
""", index: 0);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81400")]
public Task TestDuplicateParameterName_WithLocalVariable()
=> TestInRegularAndScriptAsync("""
using System;
class C
{
int M(int v)
{
int result = [|v + 1|];
return result;
}
}
""", """
using System;
class C
{
int M(int v, int result)
{
return result;
}
}
""", index: 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ private async Task<Dictionary<TIdentifierNameSyntax, IParameterSymbol>> MapExpre
/// <summary>
/// Gets the parameter name, if the expression's grandparent is a variable declarator then it just gets the
/// local declarations name. Otherwise, it generates a name based on the context of the expression.
/// Ensures the generated name is unique and does not conflict with existing parameters.
/// </summary>
private async Task<string> GetNewParameterNameAsync(CancellationToken cancellationToken)
{
if (ShouldRemoveVariableDeclaratorContainingExpression(out var varDeclName, out _))
{
return varDeclName;
}

var semanticModel = await _originalDocument.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var semanticFacts = _originalDocument.GetRequiredLanguageService<ISemanticFactsService>();
return semanticFacts.GenerateNameForExpression(semanticModel, _expression, capitalize: false, cancellationToken);
var baseName = _semanticFacts.GenerateNameForExpression(semanticModel, _expression, capitalize: false, cancellationToken);

// Ensure the parameter name doesn't conflict with existing parameters
var uniqueName = _semanticFacts.GenerateUniqueLocalName(
semanticModel,
_expression,
container: null,
baseName,
cancellationToken);

return uniqueName.ValueText;
}

/// <summary>
Expand Down
Loading