Skip to content

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

Description

@cracker-ww

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions