Skip to content

ListBuffer's insert, insertAll, update and updated accept negative positions #6632

@scabug

Description

@scabug

None of the following operations should succeed:

scala> import collection.mutable.ListBuffer
import collection.mutable.ListBuffer

scala> val lb = ListBuffer('a, 'b, 'c, 'd, 'e)
lb: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'b, 'c, 'd, 'e)

scala> lb.insert(-1, 'x)

scala> lb
res19: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'x, 'b, 'c, 'd, 'e)

scala> lb.insertAll(-2, Array('y, 'z))

scala> lb
res21: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'y, 'z, 'x, 'b, 'c, 'd, 'e)

scala> lb.update(-3, 'u)

scala> lb
res23: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'u, 'z, 'x, 'b, 'c, 'd, 'e)

scala> lb.updated(-4, 'd)
res24: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('d, 'u, 'z, 'x, 'b, 'c, 'd, 'e)

The documentation even specifies:

IndexOutOfBoundsException
if the index n is not in the valid range 0 <= n <= length. 

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions