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

*Slice: don't mutate the underlying default value #1170

Merged
merged 1 commit into from Aug 26, 2020

Conversation

tych0
Copy link

@tych0 tych0 commented Aug 5, 2020

*Slice: don't mutate the underlying default value

If the default value is present, we shouldn't mutate it. Appending slices
as the code was before mutates the underlying value:

package main

import (
        "fmt"
)

func main() {
        slice := []string{"a", "b", "c"}

        fmt.Println(append(slice[:1], slice[2:]...))
        fmt.Println(slice)
}
~ go run tester2.go
[a c]
[a c c]

Let's fix this by creating and returning an entirely new slice.

Closes #1169

Signed-off-by: Tycho Andersen <tycho@tycho.ws>

If the default value is present, we shouldn't mutate it. Appending slices
as the code was before mutates the underlying value:

package main

import (
	"fmt"
)

func main() {
	slice := []string{"a", "b", "c"}

	fmt.Println(append(slice[:1], slice[2:]...))
	fmt.Println(slice)
}
~ go run tester2.go
[a c]
[a c c]

Let's fix this by creating and returning an entirely new slice.

Closes urfave#1169

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
@tych0 tych0 requested a review from a team as a code owner August 5, 2020 15:24
@tych0
Copy link
Author

tych0 commented Aug 20, 2020

Ping?

Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rliebz rliebz merged commit af7fa3d into urfave:v1 Aug 26, 2020
tych0 added a commit to project-stacker/stacker that referenced this pull request Sep 28, 2020
urfave/cli#1170 was merged, so let's move to that
commit upstream.

Signed-off-by: Tycho Andersen <tycho@tycho.pizza>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants