Skip to content

go: copy() can not notice declared and not used #8686

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

Closed
dlintw opened this issue Sep 9, 2014 · 2 comments
Closed

go: copy() can not notice declared and not used #8686

dlintw opened this issue Sep 9, 2014 · 2 comments

Comments

@dlintw
Copy link

dlintw commented Sep 9, 2014

go 1.3.1
http://play.golang.org/p/TgUG4cii2o

1. To prevent type error, is it possible add a check about this

func main() {
    fruits := []string{"peach", "banana", "kiwi"}
    fruits2 := make([]string, len(fruits))
    copy(fruits2, fruits)
    
    fmt.Println(fruits)  // here is a type error, should be 2
    fmt.Println("Origin:", fruits)
}

What happened?

What should have happened instead?

fruits2 declared(or assigned) and not used

Please provide any additional information below.
@cznic
Copy link
Contributor

cznic commented Sep 9, 2014

Comment 1:

fruits2 is used in line 10: `copy(fruits2, fruits)`.
As slices can share the backing array, the side effect of the copy could be what is
intended and the compiler has no way how to prove that in general case.
#WAI

@ianlancetaylor
Copy link
Member

Comment 2:

There is no requirement to use the result of copy or append, as they have a clearly
defined effect when the destination slice has enough capacity.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants