From 4478db00aae5c5e487165f6e768681be5d63bac0 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 3 Jun 2024 12:29:40 -0400 Subject: [PATCH] go/analysis/passes/copylock: suppress error in ill-typed code The copylock analyzer is marked RunDespiteErrors. In ill-typed code such as S{T} where S and T are both types, the compiler will warn that T is not an expression; the copylocks analyzer should not additionally report a diagnostic as if T had been an expression of type T. The fix feels rather ad hoc. In general, analyzers marked RunDespiteErrors are unlikely to be able to anticipate the myriad ways that trees can be ill-formed and avoid spurious diagnostics. Also, add a main.go file for copylock. Fixes golang/go#67787 Change-Id: I07afbed16a4138fe602c22ec42171b4a5e634286 Reviewed-on: https://go-review.googlesource.com/c/tools/+/589895 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/analysis/passes/copylock/copylock.go | 5 ++++- go/analysis/passes/copylock/copylock_test.go | 2 +- go/analysis/passes/copylock/main.go | 16 ++++++++++++++++ .../testdata/src/issue67787/issue67787.go | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 go/analysis/passes/copylock/main.go create mode 100644 go/analysis/passes/copylock/testdata/src/issue67787/issue67787.go diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go index d0c4df091ed..8f6e7db6a27 100644 --- a/go/analysis/passes/copylock/copylock.go +++ b/go/analysis/passes/copylock/copylock.go @@ -240,7 +240,10 @@ func lockPathRhs(pass *analysis.Pass, x ast.Expr) typePath { return nil } } - return lockPath(pass.Pkg, pass.TypesInfo.Types[x].Type, nil) + if tv, ok := pass.TypesInfo.Types[x]; ok && tv.IsValue() { + return lockPath(pass.Pkg, tv.Type, nil) + } + return nil } // lockPath returns a typePath describing the location of a lock value diff --git a/go/analysis/passes/copylock/copylock_test.go b/go/analysis/passes/copylock/copylock_test.go index 5726806dbf9..91bef71979b 100644 --- a/go/analysis/passes/copylock/copylock_test.go +++ b/go/analysis/passes/copylock/copylock_test.go @@ -13,5 +13,5 @@ import ( func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.Run(t, testdata, copylock.Analyzer, "a", "typeparams") + analysistest.Run(t, testdata, copylock.Analyzer, "a", "typeparams", "issue67787") } diff --git a/go/analysis/passes/copylock/main.go b/go/analysis/passes/copylock/main.go new file mode 100644 index 00000000000..77b614ff4f5 --- /dev/null +++ b/go/analysis/passes/copylock/main.go @@ -0,0 +1,16 @@ +// Copyright 2024 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. + +//go:build ignore + +// The copylock command applies the golang.org/x/tools/go/analysis/passes/copylock +// analysis to the specified packages of Go source code. +package main + +import ( + "golang.org/x/tools/go/analysis/passes/copylock" + "golang.org/x/tools/go/analysis/singlechecker" +) + +func main() { singlechecker.Main(copylock.Analyzer) } diff --git a/go/analysis/passes/copylock/testdata/src/issue67787/issue67787.go b/go/analysis/passes/copylock/testdata/src/issue67787/issue67787.go new file mode 100644 index 00000000000..c71773dff9d --- /dev/null +++ b/go/analysis/passes/copylock/testdata/src/issue67787/issue67787.go @@ -0,0 +1,8 @@ +package issue67787 + +import "sync" + +type T struct{ mu sync.Mutex } +type T1 struct{ t *T } + +func NewT1() *T1 { return &T1{T} } // no analyzer diagnostic about T