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

slices: inaccurate documentation for the Delete and Replace functions #64013

Closed
gazerro opened this issue Nov 8, 2023 · 1 comment
Closed
Milestone

Comments

@gazerro
Copy link
Contributor

gazerro commented Nov 8, 2023

The documentation for the Delete and Replace functions states

//  ... Delete panics if s[i:j] is not a valid slice of s. ...
//  ... Replace panics if s[i:j] is not a valid slice of s. ...

and the documentation within the body of the functions reinforces the meaning of "valid slice of s":

_ = s[i:j] // verify that i:j is a valid subslice

However, the functions will also panic if s[i:j] is a valid slice of s, which occurs when len(s) < j <= cap(s). For instance:

s := []string{"a", "b", "c", "d", "e"}
s = s[0:2]

_ = s[0:4] // this is a valid slice of s

slices.Delete(s, 0, 4) // panics, but s[0:4] is a valid slice of s

slices.Replace(s, 0, 4) // panics, but s[0:4] is a valid slice of s

The documentation should state:

//  ... Delete panics if j > len(s) or s[i:j] is not a valid slice of s. ...
//  ... Replace panics if j > len(s) or s[i:j] is not a valid slice of s. ...
@gopherbot
Copy link

Change https://go.dev/cl/540955 mentions this issue: slices: update doc for Delete and Replace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants