rspec/rules/S2114/csharp/rule.adoc

60 lines
2.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-29 14:07:20 +02:00
Passing a collection as an argument to the collection's own method is a code defect. Doing so might either have unexpected side effects or always have the same result.
Another case is using set-like operations. For example, using https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.union[Union] between a list and itself will always return the same list.
Conversely, using https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except[Except] between a list and itself will always return an empty list.
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
var list = new List<int>();
2023-06-15 11:09:43 +02:00
list.AddRange(list); // Noncompliant
list.Concat(list); // Noncompliant
2020-06-30 12:48:07 +02:00
2023-06-15 11:09:43 +02:00
list.Union(list); // Noncompliant: always returns list
2023-06-29 14:07:20 +02:00
list.Intersect(list); // Noncompliant: always returns list
list.Except(list); // Noncompliant: always returns empty
list.SequenceEqual(list); // Noncompliant: always returns true
2020-06-30 12:48:07 +02:00
var set = new HashSet<int>();
2023-06-15 11:09:43 +02:00
set.UnionWith(set); // Noncompliant: no changes
set.IntersectWith(set); // Noncompliant: no changes
2023-06-29 14:07:20 +02:00
set.ExceptWith(set); // Noncompliant: always returns empty
set.SymmetricExceptWith(set); // Noncompliant: always returns empty
set.IsProperSubsetOf(set); // Noncompliant: always returns false
set.IsProperSupersetOf(set); // Noncompliant: always returns false
set.IsSubsetOf(set); // Noncompliant: always returns true
set.IsSupersetOf(set); // Noncompliant: always returns true
set.Overlaps(set); // Noncompliant: always returns true
set.SetEquals(set); // Noncompliant: always returns true
2020-06-30 12:48:07 +02:00
----
2023-06-29 14:07:20 +02:00
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections[Collections]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Change one instance of '{0}' to a different value; This operation always produces [an empty/the same] collection.
* Change one instance of '{0}' to a different value; comparing '{0}' to itself always returns [true|false].
* Change one instance of '{0}' to a different value; This operation will probably result in an unexpected behavior.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]