cmd/vet: lostcancel: suppress the check in the main.main function

When main.main returns, the process exits, so there's no need to cancel contexts.

This CL was originally reviewed as
  https://go-review.googlesource.com/c/go/+/106915
and then retried in
  https://go-review.googlesource.com/c/go/+/148758
but then reverted due to an embarrassing sequence
of careless moves.

Change-Id: Icdee0650996a442023e030697f10d2c31fd5fdff
Reviewed-on: https://go-review.googlesource.com/c/148877
Run-TryBot: Alan Donovan <adonovan@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Alan Donovan 2018-11-09 13:09:17 -05:00
parent d3a25d70bd
commit 4e34152f16
3 changed files with 35 additions and 3 deletions

View File

@ -132,11 +132,17 @@ func runFunc(pass *analysis.Pass, node ast.Node) {
var sig *types.Signature
switch node := node.(type) {
case *ast.FuncDecl:
g = cfgs.FuncDecl(node)
sig, _ = pass.TypesInfo.Defs[node.Name].Type().(*types.Signature)
if node.Name.Name == "main" && sig.Recv() == nil && pass.Pkg.Name() == "main" {
// Returning from main.main terminates the process,
// so there's no need to cancel contexts.
return
}
g = cfgs.FuncDecl(node)
case *ast.FuncLit:
g = cfgs.FuncLit(node)
sig, _ = pass.TypesInfo.Types[node.Type].Type.(*types.Signature)
g = cfgs.FuncLit(node)
}
if sig == nil {
return // missing type information

View File

@ -13,5 +13,5 @@ import (
func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, lostcancel.Analyzer, "a")
analysistest.Run(t, testdata, lostcancel.Analyzer, "a", "b")
}

View File

@ -0,0 +1,26 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "context"
// Return from main is handled specially.
// Since the program exits, there's no need to call cancel.
func main() {
_, cancel := context.WithCancel(nil)
if maybe {
cancel()
}
}
func notMain() {
_, cancel := context.WithCancel(nil) // want "cancel function.*not used"
if maybe {
cancel()
}
} // want "return statement.*reached without using the cancel"
var maybe bool