Modify rule S2114: LaYC format (#2316)

This commit is contained in:
Gregory Paidis 2023-06-29 14:07:20 +02:00 committed by GitHub
parent e16d3e9ff0
commit dd9def2840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 45 deletions

View File

@ -1,6 +1,9 @@
== Why is this an issue?
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
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.
[source,csharp]
----
@ -10,23 +13,30 @@ list.AddRange(list); // Noncompliant
list.Concat(list); // Noncompliant
list.Union(list); // Noncompliant: always returns list
list.Except(list); // Noncompliant: always empty
list.Intersect(list); // Noncompliant: always list
list.SequenceEqual(list); // Noncompliant: always true
list.Intersect(list); // Noncompliant: always returns list
list.Except(list); // Noncompliant: always returns empty
list.SequenceEqual(list); // Noncompliant: always returns true
var set = new HashSet<int>();
set.UnionWith(set); // Noncompliant: no changes
set.ExceptWith(set); // Noncompliant: always empty
set.IntersectWith(set); // Noncompliant: no changes
set.IsProperSubsetOf(set); // Noncompliant: always false
set.IsProperSupersetOf(set); // Noncompliant: always false
set.IsSubsetOf(set); // Noncompliant: always true
set.IsSupersetOf(set); // Noncompliant: always true
set.Overlaps(set); // Noncompliant: always true
set.SetEquals(set); // Noncompliant: always true
set.SymmetricExceptWith(set); // Noncompliant: always empty
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
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections[Collections]
ifdef::env-github,rspecator-view[]
'''

View File

@ -1,4 +0,0 @@
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
Further, because some methods require that the argument remain unmodified during the execution, passing a collection to itself can result in undefined behavior.

View File

@ -1,4 +1,22 @@
include::../rule.adoc[]
== Why is this an issue?
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
Further, because some methods require that the argument remain unmodified during the execution, passing a collection to itself can result in undefined behavior.
=== Noncompliant code example
[source,java]
----
List <Object> objs = new ArrayList<Object>();
objs.add("Hello");
objs.add(objs); // Noncompliant; StackOverflowException if objs.hashCode() called
objs.addAll(objs); // Noncompliant; behavior undefined
objs.containsAll(objs); // Noncompliant; always true
objs.removeAll(objs); // Noncompliant; confusing. Use clear() instead
objs.retainAll(objs); // Noncompliant; NOOP
----
ifdef::env-github,rspecator-view[]
@ -6,7 +24,9 @@ ifdef::env-github,rspecator-view[]
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
=== Message
Remove or correct this "xxx" call.
'''
== Comments And Links

View File

@ -1,10 +1,16 @@
== Why is this an issue?
include::../description.adoc[]
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
=== Noncompliant code example
Further, because some methods require that the argument remain unmodified during the execution, passing a collection to itself can result in undefined behavior.
[source,kotlin]
== How to fix it
=== Code examples
==== Noncompliant code example
[source,kotlin,diff-id=1,diff-type=noncompliant]
----
val objs = mutableListOf<Any>()
objs.add("Hello")
@ -16,9 +22,9 @@ objs.removeAll(objs) // Noncompliant; confusing. Use clear() instead
objs.retainAll(objs) // Noncompliant; NOOP
----
=== Compliant solution
==== Compliant solution
[source,kotlin]
[source,kotlin,diff-id=1,diff-type=compliant]
----
val newList = mutableListOf<Any>()
val objs = mutableListOf<Any>()

View File

@ -1,4 +0,0 @@
=== Message
Remove or correct this "xxx" call.

View File

@ -1,18 +0,0 @@
== Why is this an issue?
include::description.adoc[]
=== Noncompliant code example
[source,text]
----
List <Object> objs = new ArrayList<Object>();
objs.add("Hello");
objs.add(objs); // Noncompliant; StackOverflowException if objs.hashCode() called
objs.addAll(objs); // Noncompliant; behavior undefined
objs.containsAll(objs); // Noncompliant; always true
objs.removeAll(objs); // Noncompliant; confusing. Use clear() instead
objs.retainAll(objs); // Noncompliant; NOOP
----