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 T0010 FP: Allow "_" discard like parameter name #9257
Fix T0010 FP: Allow "_" discard like parameter name #9257
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/LambdaParameterName.cs
Outdated
Show resolved
Hide resolved
Quality Gate passed for 'Sonar .NET Java Plugin'Issues Measures |
Quality Gate passed for 'SonarAnalyzer for .NET'Issues Measures |
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.
LGTM
@@ -29,7 +29,7 @@ public sealed class LambdaParameterName : StylingAnalyzer | |||
context.RegisterNodeAction(c => | |||
{ | |||
var parameter = ((SimpleLambdaExpressionSyntax)c.Node).Parameter; | |||
if (parameter.Identifier.ValueText != "x" | |||
if (parameter.Identifier.ValueText is not ("x" or "_") |
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.
What about leaving a comment here that in C# 13, it will be a real discard and the "_"
will not be needed anymore?
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.
Even if parsed as a discard, it is still an IdenifierToken:
sharplab.io
So I think it will still be needed.
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.
Do you know if there's a branch available with that feature? I didn't find any in a quick search.
It might not be IdentifierToken, but some kind of discard designator, like here:
https://sharplab.io/#v2:C4LghgzsA0AmIGoA+ABATARgLACgUGYACdQgYUIG9dCbiiUAWQgWQAoBKS62ngNzABOhVgH1ohAB6cAvIQAqHANzcaAXxWENABwEBLfsACmw3QDsYhM8E4KZAPkKxDAMzABXADbBlOVUA===
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 not aware of a branch for it. The closest I could find on SharpLab is "Semi Auto-properties," but that one hasn't been updated since Oct 22.
The DiscardDesignation
is an interesting find. The significant difference here is that the distinguishing happens on the node level, while for parameters, it would likely need to occur on the token level.
There are cases like this already with, e.g., IdentifierNameSyntax
where an Identifier property can be either an IdentiferToken
or a GlobalKeyword
.
There can be IdentifierToken
or the ArgListKeyword
for parameters. Maybe we see an UnderScore
here as well in the future.
<Node Name="ParameterSyntax" Base="BaseParameterSyntax">
<Kind Name="Parameter"/>
<! -- ... -->
<Field Name="Identifier" Type="SyntaxToken">
<PropertyComment>
<summary>Gets the identifier.</summary>
</PropertyComment>
<Kind Name="IdentifierToken"/>
<Kind Name="ArgListKeyword"/>
</Field>
<! -- ... -->
</Node>
_ => 42
should be allowed, because_
is more descriptive thanx
in this case and signals: "I do not care about the parameter." Rational: In(_, _) => 42
_
is a real discard, while in_ => 42
it isn't only for back compatibility reasons.See e.g. here:
https://sonarcloud.io/project/issues?resolved=false&pullRequest=8950&id=sonaranalyzer-dotnet&open=AY9NLK1CHaBGYjI6fPmk
_
is a better name thanx
in such a case.