rspec/rules/S2114/java/rule.adoc

38 lines
1.0 KiB
Plaintext
Raw Normal View History

2023-06-29 14:07:20 +02:00
== 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[]
'''
== Implementation Specification
(visible only on this page)
2023-06-29 14:07:20 +02:00
=== Message
Remove or correct this "xxx" call.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]