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

Modify rule S3878: Add collection expression and collection params to the description #4430

Merged
merged 5 commits into from
Oct 23, 2024
Merged
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
48 changes: 36 additions & 12 deletions rules/S3878/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
== Why is this an issue?

There's no point in creating an array solely for the purpose of passing it to a `params` parameter. Simply pass the elements directly. They will be consolidated into an array automatically.
Creating an array or using a collection expression solely for the purpose of passing it to a `params` parameter is unnecessary. Simply pass the elements directly, and they will be automatically consolidated into the appropriate collection type.

=== Noncompliant code example
== How to fix it

[source,csharp]
=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public void Base()
{
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
Method(new string[] { "s1", "s2" }); // Noncompliant: unnecessary
Method(new string[] { }); // Noncompliant
Method(new string[12]); // Compliant
Method(new string[] { "s1", "s2" }); // Noncompliant: resolves to string[] overload
Method(new string[] { }); // Noncompliant: resolves to string[] overload
Method(["s3", "s4"]); // Noncompliant: resolves to ReadOnlySpan overload
Method(new string[12]); // Compliant: resolves to string[] overload
}

CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
public void Method(params string[] args)
{
// ...
}

public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
// C# 13 params collection
}
----

=== Compliant solution
==== Compliant solution

[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
public void Base()
{
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
Method("s1", "s2");
Method();
Method(new string[12]);
Method("s1", "s2"); // resolves to ReadOnlySpan overload
Method(); // resolves to ReadOnlySpan overload
Method("s3", "s4"); // resolves to ReadOnlySpan overload
Method(new string[12]); // resolves to string[] overload
}

public void Method(params string[] args)
{
// ...
// ..
}

public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
// ..
}
----

== Resources

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#params-modifier[`params` modifier]
* Microsoft Learn - C# 13 https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections[`params` collections]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions[Collection expressions]