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

Fix T0010 FP: Allow delegate type parameter name #9258

Merged
merged 5 commits into from
May 8, 2024
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 @@ -28,16 +28,29 @@ public sealed class LambdaParameterName : StylingAnalyzer
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
var parameter = ((SimpleLambdaExpressionSyntax)c.Node).Parameter;
if (parameter.Identifier.ValueText is not ("x" or "_")
&& c.Node.Parent.FirstAncestorOrSelf<LambdaExpressionSyntax>() is null
&& !IsSonarContextAction(c))
if (c.Node is SimpleLambdaExpressionSyntax { Parameter: { Identifier.ValueText: not ("x" or "_") } parameter, Parent: { } parent } lambda
&& parent.FirstAncestorOrSelf<LambdaExpressionSyntax>() is null
&& !IsSonarContextAction(c)
&& !MatchesDelegateParameterName(c.SemanticModel, lambda))
{
c.ReportIssue(Rule, parameter);
}
},
SyntaxKind.SimpleLambdaExpression);

private static bool MatchesDelegateParameterName(SemanticModel model, SimpleLambdaExpressionSyntax lambda) =>
model.GetTypeInfo(lambda).ConvertedType is INamedTypeSymbol
{
TypeKind: TypeKind.Delegate,
DelegateInvokeMethod.Parameters: { Length: 1 } parameters
} delegateType
&& !IsFuncOrAction(delegateType)
&& parameters[0] is { Name: { } parameterName }
&& parameterName == lambda.Parameter.Identifier.ValueText;

private static bool IsFuncOrAction(INamedTypeSymbol delegateType) =>
delegateType.IsAny(KnownType.System_Func_T_TResult, KnownType.System_Action_T);

private static bool IsSonarContextAction(SonarSyntaxNodeReportingContext context) =>
context.SemanticModel.GetSymbolInfo(context.Node).Symbol is IMethodSymbol lambda
&& lambda.ReturnsVoid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

public class Sample
Expand Down Expand Up @@ -73,25 +74,52 @@ public void Initialize()
RegisterSonarWhateverAnalysisContext(context => { });
RegisterSonarWhateverAnalysisContext(whateverContext => { });
RegisterSonarWhateverReportingContext(c => { });
RegisterSonarSometing(c => { }); // Noncompliant, wrong suffix
RegisterSometingAnalysisContext(c => { }); // Noncompliant, wrong prefix
RegisterSometingReportingContext(c => { }); // Noncompliant, wrong prefix
RegisterSonarSometingContext(c => { }); // Noncompliant, wrong suffix
RegisterSonarSomething(c => { }); // Noncompliant, wrong suffix
RegisterSomethingAnalysisContext(c => { }); // Noncompliant, wrong prefix
RegisterSomethingReportingContext(c => { }); // Noncompliant, wrong prefix
RegisterSonarSomethingContext(c => { }); // Noncompliant, wrong suffix
}

protected void RegisterSonarWhateverAnalysisContext(Action<SonarWhateverAnalysisContext> action) { }
protected void RegisterSonarWhateverReportingContext(Action<SonarWhateverReportingContext> action) { }
protected void RegisterSonarSometing(Action<SonarSometing> action) { }
protected void RegisterSometingAnalysisContext(Action<SometingAnalysisContext> action) { }
protected void RegisterSometingReportingContext(Action<SometingReportingContext> action) { }
protected void RegisterSonarSometingContext(Action<SonarSometingContext> action) { }
protected void RegisterSonarSomething(Action<SonarSomething> action) { }
protected void RegisterSomethingAnalysisContext(Action<SomethingAnalysisContext> action) { }
protected void RegisterSomethingReportingContext(Action<SomethingReportingContext> action) { }
protected void RegisterSonarSomethingContext(Action<SonarSomethingContext> action) { }

// Well-known expected classes patterns
public class SonarWhateverAnalysisContext { }
public class SonarWhateverReportingContext { }
// Unexpected types
public class SonarSometing { }
public class SometingAnalysisContext { }
public class SometingReportingContext { }
public class SonarSometingContext { }
public class SonarSomething { }
public class SomethingAnalysisContext { }
public class SomethingReportingContext { }
public class SonarSomethingContext { }
}

public class CustomDelegates
{
public delegate void ParameterNamedI(int i);
public delegate void ParameterNamedTest(int test);
public delegate void ParameterNamedCamelCasing(int camelCasing);

public void Test()
{
ParameterNamedI delegate1 = i => { }; // Compliant "i" matches the parameter name of the delegate
ParameterNamedI delegate2 = j => { }; // Noncompliant
ParameterNamedI delegate3 = x => { }; // Compliant

ParameterNamedTest delegate4 = test => { }; // Compliant
ParameterNamedTest delegate5 = someTest => { }; // Noncompliant
ParameterNamedTest delegate6 = testSome => { }; // Noncompliant

ParameterNamedCamelCasing delegate7 = camelCasing => { }; // Compliant
ParameterNamedCamelCasing delegate8 = camelcasing => { }; // Noncompliant
ParameterNamedCamelCasing delegate9 = camel => { }; // Noncompliant
ParameterNamedCamelCasing delegate10 = casing => { }; // Noncompliant

Func<int, int> function = arg => 0; // Noncompliant, the delegate parameter is named "arg" (https://learn.microsoft.com/en-us/dotnet/api/system.func-2) but we do not allow that for Func<T, TResult>
Action<int> action = obj => { }; // Noncompliant, the delegate parameter is named "obj" (https://learn.microsoft.com/en-us/dotnet/api/system.action-1) but we do not allow that for Action<T>
new List<int>().Exists(obj => true); // Compliant. List.Exists uses System.Predicate<T> instead of System.Func<T, bool>
sebastien-marichal marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading