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

Conversation

akhera99
Copy link
Member

Fixes #58893

@akhera99 akhera99 requested a review from a team as a code owner January 20, 2022 01:09
@akhera99 akhera99 self-assigned this Jan 20, 2022
{
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"?

Comment on lines 61 to 65
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
if (expressionSymbol is IParameterSymbol)
{
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Is the new behavior expected for this case?

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

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

Or do we need to tighten the condition to make sure the parameter symbol corresponds to the most inner containing method?

Copy link

Choose a reason for hiding this comment

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

Wonder if it's that particular scenario that's the motivation for making the suggestion in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

@Joe4evr The original scenario looks different to me. See the added test.

Copy link
Member

Choose a reason for hiding this comment

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

For this scenario, it should still be possible to explicitly pass the parameter instead of implicit capture. Whether that's part of the current refactoring or some other is of less importance to me.

Comment on lines 61 to 62
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
if (expressionSymbol is IParameterSymbol)
Copy link
Member

Choose a reason for hiding this comment

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

With this check, do we still need the below condition? (line 72 to 76)

            var parameterNode = expression.FirstAncestorOrSelf<SyntaxNode>(node => syntaxFacts.IsParameter(node));
            if (parameterNode is not null)
            {
                return;
            }

Edit: Answer is we need both. The checks have completely different purposes.

if (expressionSymbol is IParameterSymbol && !syntaxFacts.IsLocalFunctionStatement(containingMethod))
{
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.

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@akhera99 akhera99 merged commit 47e3cb5 into dotnet:main Jan 24, 2022
@ghost ghost added this to the Next milestone Jan 24, 2022
@RikkiGibson RikkiGibson modified the milestones: Next, 17.2.P1 Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quick Actions refactoring offers to introduce parameter for variable that is already a parameter
8 participants