Skip to content

Commit

Permalink
AV1551: Ignore CancellationToken parameter in ordering, which always …
Browse files Browse the repository at this point in the history
…comes last by convention (#158)
  • Loading branch information
bkoelman committed Apr 20, 2024
1 parent c8be599 commit ddb2848
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,87 @@ protected virtual bool M(string value, int index, bool flag, params object[] arg
"Parameter order in 'C.M(int, string, params object[])' does not match with the parameter order of the longest overload");
}

[Fact]
internal async Task When_parameter_order_in_overloads_is_consistent_with_CancellationToken_it_must_be_skipped()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.Using(typeof(CancellationToken).Namespace)
.InGlobalScope("""
public class C
{
public bool M()
{
return M(CancellationToken.None);
}

public bool M(CancellationToken cancellationToken)
{
return M(string.Empty, cancellationToken);
}

public bool M(string value, CancellationToken cancellationToken)
{
return M(value, -1, cancellationToken);
}

public bool M(string value, int index, CancellationToken cancellationToken)
{
return M(value, index, false, cancellationToken);
}

protected virtual bool M(string value, int index, bool flag, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
""")
.Build();

await VerifyGuidelineDiagnosticAsync(source);
}

[Fact]
internal async Task When_parameter_order_in_overloads_is_not_consistent_with_CancellationToken_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.Using(typeof(CancellationToken).Namespace)
.InGlobalScope("""
public class C
{
public bool M()
{
return M(CancellationToken.None);
}

public bool M(CancellationToken cancellationToken)
{
return M(string.Empty, cancellationToken);
}

public bool M(string value, CancellationToken cancellationToken)
{
return M(-1, value, cancellationToken);
}

public bool [|M|](int index, string value, CancellationToken cancellationToken)
{
return M(value, index, false, cancellationToken);
}

protected virtual bool M(string value, int index, bool flag, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
""")
.Build();

await VerifyGuidelineDiagnosticAsync(source,
"Parameter order in 'C.M(int, string, CancellationToken)' does not match with the parameter order of the longest overload");
}

[Fact]
internal async Task When_parameter_order_in_overloads_is_consistent_with_optional_parameters_it_must_be_skipped()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ private static void CompareOrderOfParameters([NotNull] IMethodSymbol method, [No

private static bool IsRegularParameter([NotNull] IParameterSymbol parameter)
{
return parameter is { HasExplicitDefaultValue: false, IsParams: false };
return parameter is { HasExplicitDefaultValue: false, IsParams: false } && !IsCancellationToken(parameter.Type);
}

private static bool IsCancellationToken([NotNull] ITypeSymbol type)
{
return type.ToDisplayString() == "System.Threading.CancellationToken";
}

private static bool AreDefaultParametersDeclaredInSameOrder([NotNull] IMethodSymbol method,
Expand Down

0 comments on commit ddb2848

Please sign in to comment.