Skip to content

Commit ddb2848

Browse files
authored
AV1551: Ignore CancellationToken parameter in ordering, which always comes last by convention (#158)
1 parent c8be599 commit ddb2848

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/OverloadShouldCallOtherOverloadSpecs.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,87 @@ await VerifyGuidelineDiagnosticAsync(source,
837837
"Parameter order in 'C.M(int, string, params object[])' does not match with the parameter order of the longest overload");
838838
}
839839

840+
[Fact]
841+
internal async Task When_parameter_order_in_overloads_is_consistent_with_CancellationToken_it_must_be_skipped()
842+
{
843+
// Arrange
844+
ParsedSourceCode source = new TypeSourceCodeBuilder()
845+
.Using(typeof(CancellationToken).Namespace)
846+
.InGlobalScope("""
847+
public class C
848+
{
849+
public bool M()
850+
{
851+
return M(CancellationToken.None);
852+
}
853+
854+
public bool M(CancellationToken cancellationToken)
855+
{
856+
return M(string.Empty, cancellationToken);
857+
}
858+
859+
public bool M(string value, CancellationToken cancellationToken)
860+
{
861+
return M(value, -1, cancellationToken);
862+
}
863+
864+
public bool M(string value, int index, CancellationToken cancellationToken)
865+
{
866+
return M(value, index, false, cancellationToken);
867+
}
868+
869+
protected virtual bool M(string value, int index, bool flag, CancellationToken cancellationToken)
870+
{
871+
throw new NotImplementedException();
872+
}
873+
}
874+
""")
875+
.Build();
876+
877+
await VerifyGuidelineDiagnosticAsync(source);
878+
}
879+
880+
[Fact]
881+
internal async Task When_parameter_order_in_overloads_is_not_consistent_with_CancellationToken_it_must_be_reported()
882+
{
883+
// Arrange
884+
ParsedSourceCode source = new TypeSourceCodeBuilder()
885+
.Using(typeof(CancellationToken).Namespace)
886+
.InGlobalScope("""
887+
public class C
888+
{
889+
public bool M()
890+
{
891+
return M(CancellationToken.None);
892+
}
893+
894+
public bool M(CancellationToken cancellationToken)
895+
{
896+
return M(string.Empty, cancellationToken);
897+
}
898+
899+
public bool M(string value, CancellationToken cancellationToken)
900+
{
901+
return M(-1, value, cancellationToken);
902+
}
903+
904+
public bool [|M|](int index, string value, CancellationToken cancellationToken)
905+
{
906+
return M(value, index, false, cancellationToken);
907+
}
908+
909+
protected virtual bool M(string value, int index, bool flag, CancellationToken cancellationToken)
910+
{
911+
throw new NotImplementedException();
912+
}
913+
}
914+
""")
915+
.Build();
916+
917+
await VerifyGuidelineDiagnosticAsync(source,
918+
"Parameter order in 'C.M(int, string, CancellationToken)' does not match with the parameter order of the longest overload");
919+
}
920+
840921
[Fact]
841922
internal async Task When_parameter_order_in_overloads_is_consistent_with_optional_parameters_it_must_be_skipped()
842923
{

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/OverloadShouldCallOtherOverloadAnalyzer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ private static bool AreRegularParametersDeclaredInSameOrder([NotNull] IMethodSym
216216

217217
private static bool IsRegularParameter([NotNull] IParameterSymbol parameter)
218218
{
219-
return parameter is { HasExplicitDefaultValue: false, IsParams: false };
219+
return parameter is { HasExplicitDefaultValue: false, IsParams: false } && !IsCancellationToken(parameter.Type);
220+
}
221+
222+
private static bool IsCancellationToken([NotNull] ITypeSymbol type)
223+
{
224+
return type.ToDisplayString() == "System.Threading.CancellationToken";
220225
}
221226

222227
private static bool AreDefaultParametersDeclaredInSameOrder([NotNull] IMethodSymbol method,

0 commit comments

Comments
 (0)