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

cmd/vet: copylock does not warn when "lock_type_var = *func_return_pointer_to_locktype()" #32550

Open
cracker-ww opened this issue Jun 11, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@cracker-ww
Copy link

commented Jun 11, 2019

I am using Go 1.12.1.

cmd/vet does not report any warning for example code below:

package main

type noCopy struct{}

func (*noCopy) Lock()              {}
func (*noCopy) Unlock()            {}
func (*noCopy) MuteUnusedWarning() {}

func returnNCP() *noCopy {
	var nc noCopy
	return &nc
}

func main() {
	var nc noCopy
	nc = *returnNCP()
	nc.MuteUnusedWarning()
}

I read code of the analyzer copylock.go:

func lockPathRhs(pass *analysis.Pass, x ast.Expr) typePath {
   224     if _, ok := x.(*ast.CompositeLit); ok {
   225         return nil
   226     }
   227     if _, ok := x.(*ast.CallExpr); ok {
   228         // A call may return a zero value.
   229         return nil
   230     }
   231     if star, ok := x.(*ast.StarExpr); ok {
   232         if _, ok := star.X.(*ast.CallExpr); ok {
   233             // A call may return a pointer to a zero value.
   234             return nil
   235     }

my confusion is why line 234 just return nil without check the return type of the function (BTW, in my example code, "return &nc" will pass the "checkCopyLocksReturnStmt" check, b/c the return type is a pointer). All this result to the mute of cmd/vet.

Could this case be caught by cmd/vet in future?

@bcmills

This comment has been minimized.

Copy link
Member

commented Jun 14, 2019

Please provide Playground links instead of bare source files when possible, and format Code blocks using ```go Markdown blocks so that syntax highlighting works.

@bcmills bcmills changed the title copylock in cmd/vet do not warn when "lock_type_var = *func_return_pointer_to_locktype()" cmd/vet: copylock does not warn when "lock_type_var = *func_return_pointer_to_locktype()" Jun 14, 2019

@bcmills

This comment has been minimized.

Copy link
Member

commented Jun 14, 2019

@bcmills bcmills added this to the Unplanned milestone Jun 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.