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

Introduce Parameter Should Not Try to Introduce on a Parameter #58959

Merged
merged 5 commits into from
Jan 24, 2022
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 @@ -1932,5 +1932,62 @@ public Program(int x)
";
await TestInRegularAndScriptAsync(code, expected, 0);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceParameter)]
public async Task TestIntroduceParameterOnParameter()
{
var code =
@"
using System;

class Program
{
public static void Main(string[] args)
{
Console.WriteLine([|args|]);
Copy link
Contributor

Choose a reason for hiding this comment

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

does this also inhibit things like args[0] and args + "My String"?

}
}
";

await TestMissingInRegularAndScriptAsync(code);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceParameter)]
public async Task TestIntroduceParameterOnExpressionContainingParameter()
{
var code =
@"
public class C
{
public void M(string s)
{
localFunction();

void localFunction()
{
_ = [|s|].ToString();
}
}
}
";

var expected =
@"
public class C
{
public void M(string s)
{
localFunction(s);

void localFunction(string s)
{
_ = {|Rename:s|}.ToString();
}
}
}
";

await TestInRegularAndScriptAsync(code, expected, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

i think the original fix was corrrect. i don't see why we'd want to special case local functions.

note: we should have extra tests here. for example if the user is on a local-func parameter, we shouldn't add that a second time either.

Copy link
Member

Choose a reason for hiding this comment

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

IMO the correct fix is none of the two, but:

            if (expressionSymbol is IParameterSymbol parameterSymbol && parameterSymbol.ContainingSymbol.Equals(containingSymbol))
            {
                return;
            }

Copy link
Member

Choose a reason for hiding this comment

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

i'm ok not adding one parameter to another container. but i don't feel strongly about that.

Copy link
Member

Choose a reason for hiding this comment

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

@CyrusNajmabadi This scenario is important IMO if I want to move from:

public class C
{
    public void M(string s)
    {
        localFunction();

        void localFunction()
        {
            _ = s.ToString();
        }
    }
}

to:

public class C
{
    public void M(string s)
    {
        localFunction(s);

        localFunction("Another string");

        void localFunction(string s)
        {
            _ = s.ToString();
        }
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@Youssef1313 I agree, I like that example.


var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
if (expressionSymbol is IParameterSymbol parameterSymbol && parameterSymbol.ContainingSymbol.Equals(containingSymbol))
{
return;
}

// Code actions for trampoline and overloads will not be offered if the method is a constructor.
// Code actions for overloads will not be offered if the method if the method is a local function.
var methodKind = methodSymbol.MethodKind;
Expand Down