From 5e17ce22ece18571597ef9a214cdfcd461894e9c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 13 Apr 2018 14:27:33 -0400 Subject: [PATCH] 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 change was initially reviewed as https://go-review.googlesource.com/c/go/+/106915/4 but somehow I messed up and committed patchset 5, which was effectively empty. Change-Id: Ic4250eb6563af9bc734e429aafc7081ca7d0e012 Reviewed-on: https://go-review.googlesource.com/c/148758 Reviewed-by: Alan Donovan --- src/cmd/vet/lostcancel.go | 5 +++++ src/cmd/vet/testdata/lostcancel.go | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/cmd/vet/lostcancel.go b/src/cmd/vet/lostcancel.go index ee0342035f..496e87a5e0 100644 --- a/src/cmd/vet/lostcancel.go +++ b/src/cmd/vet/lostcancel.go @@ -104,6 +104,11 @@ func checkLostCancel(f *File, node ast.Node) { var sig *types.Signature switch node := node.(type) { case *ast.FuncDecl: + if node.Name.Name == "main" && node.Recv == nil && f.file.Name.Name == "main" { + // Returning from main.main terminates the process, + // so there's no need to cancel contexts. + return + } obj := f.pkg.defs[node.Name] if obj == nil { return // type error (e.g. duplicate function declaration) diff --git a/src/cmd/vet/testdata/lostcancel.go b/src/cmd/vet/testdata/lostcancel.go index b7549c0051..408bed5122 100644 --- a/src/cmd/vet/testdata/lostcancel.go +++ b/src/cmd/vet/testdata/lostcancel.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package testdata +package main import ( "context" @@ -33,6 +33,12 @@ func _() { ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak" } +// Return from main is handled specially. +// Since the program exits, there's no need to call cancel. +func main() { + var ctx, cancel = context.WithCancel() +} + func _() { ctx, cancel := context.WithCancel() defer cancel() // ok -- 2.48.1