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
Conversation
{ | ||
public static void Main(string[] args) | ||
{ | ||
Console.WriteLine([|args|]); |
There was a problem hiding this comment.
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"
?
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; | ||
if (expressionSymbol is IParameterSymbol) | ||
{ | ||
return; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; | ||
if (expressionSymbol is IParameterSymbol) |
There was a problem hiding this comment.
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; | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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();
}
}
}
There was a problem hiding this comment.
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 successfully started running 4 pipeline(s). |
Fixes #58893