Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 <[email protected]>

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 <[email protected]>
@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 <[email protected]>
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.

3 participants