Skip to content

Commit 5934133

Browse files
committed
[gopls-release-branch.0.6] all: merge master into gopls-release-branch.0.6
b261fe9 go/tools: add vet check for direct comparion of reflect.Values e78b40c internal/lsp: switch to 'go get -u ./...' for transitive upgrades 3ac76b8 gopls/internal/coverage: better error messages and output c602466 Revert "internal/lsp/cache: disable GOPACKAGESDRIVER" 35a9159 go/internal/gcimporter: ensure that imported floats are of float kind 5d33438 go/internal/gcimporter: merge go1.11 tests back into gcimporter_test.go 11c3f83 internal/lsp/source: fix Highlight for std and 3rd-party packages 8c34cc9 internal/lsp/cache: fix race condition in snapshot's initializedErr 0deaffd gopls/internal/regtest: re-enable the gc_details regtest for -short af36406 internal/lsp/source: respond with the underlying type to Type Definition requests for composite types 682c7e6 godoc/static: link to golang.org for content moved out of Go root ca627f8 internal/lsp/semantic: fix some type definitions 0459589 internal/lsp/protocol/typescript: small cleanups and add tsconfig.json b0e994d internal/jsonrpc2_v2: move the idle timeout handling out of the server 9553575 internal/lsp: remove unnecessary call to WorkspacePackages in mod tidy 769264c internal/lsp/source: fix docs for fields of anonymous structs/interfaces cb7d599 internal/lsp/source: skip blank identifiers during the function extraction 94a1942 internal/lsp/completion: move postfix completions behind option 09058ab internal/lsp/source/completion: add postfix snippet completions 2c039f7 internal/event/label: prevent unsafe get of non-string 4c8e4a8 go/analysis/passes/inspect: fix typo in comment 63ea654 internal/lsp: hold the gc details lock when storing diagnostics 7f6d50e internal/lsp/source: fix hover and completion with dot imports b3556f0 internal/lsp: remove some unused parameters, mostly in the cache package 09a00c1 internal/lsp: fix support for SourceFixAll code actions 877f9c4 internal/jsonrpc2_v2: an updated jsonrpc2 library 52cb772 gopls: upgrade mvdan.cc/gofumpt to v0.1.1 a1191f3 gopls: upgrade honnef.co/go/tools to v0.1.3 d8aeb16 internal/lsp: add defaultLibrary mod for basic types aa0c723 internal/lsp/cache: get control of reloadOrphanedFiles d7a25ad gopls/integration: remove obsolete code 9bdb419 gopls/internal: add coverage command to compute test coverage d2e11a2 present: don't drop commands that immediately follow text 695b167 cmd/present2md: allow comments to be retained in translated document a14ff98 present: retain complete caption command when parsing captions 1aac171 Update emacs.md - add .dir-locals.el example 2c4a886 internal/lsp: remove module diagnostics from code actions 9b614f5 internal/lsp/cache: tolerate analysis panics better 9e9211a gopls/internal/regtest: fix race when printing regtest state on falure 6d45e3d internal/lsp: add a temp workspace per folder, and a helper command e409f12 internal/lsp: add snippet completion for t.Fatal errs 8e4f4c8 godoc: delete GoogleCN logic 44abc2a internal/lsp: only load by view when there are no go.mod files Change-Id: Ie5d628f640a8edaeb41c4cdf30aafefbc892ec7c
2 parents 774f453 + b261fe9 commit 5934133

File tree

127 files changed

+5085
-1714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+5085
-1714
lines changed

cmd/godoc/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ func main() {
192192
}
193193
if *templateDir != "" {
194194
fs.Bind("/lib/godoc", vfs.OS(*templateDir), "/", vfs.BindBefore)
195+
fs.Bind("/favicon.ico", vfs.OS(*templateDir), "/favicon.ico", vfs.BindReplace)
195196
} else {
196197
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
198+
fs.Bind("/favicon.ico", mapfs.New(static.Files), "/favicon.ico", vfs.BindReplace)
197199
}
198200

199201
// Get the GOMOD value, use it to determine if godoc is being invoked in module mode.

cmd/present2md/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func convert(r io.Reader, file string, writeBack bool) error {
9393
return fmt.Errorf("%v: already markdown", file)
9494
}
9595

96+
// Convert all comments before parsing the document.
97+
// The '//' comment is treated as normal text and so
98+
// is passed through the translation unaltered.
99+
data = bytes.Replace(data, []byte("\n#"), []byte("\n//"), -1)
100+
96101
doc, err := present.Parse(bytes.NewReader(data), file, 0)
97102
if err != nil {
98103
return err

go/analysis/passes/inspect/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// license that can be found in the LICENSE file.
44

55
// Package inspect defines an Analyzer that provides an AST inspector
6-
// (golang.org/x/tools/go/ast/inspect.Inspect) for the syntax trees of a
7-
// package. It is only a building block for other analyzers.
6+
// (golang.org/x/tools/go/ast/inspector.Inspector) for the syntax trees
7+
// of a package. It is only a building block for other analyzers.
88
//
99
// Example of use in another analysis:
1010
//
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package reflectvaluecompare defines an Analyzer that checks for accidentally
6+
// using == or reflect.DeepEqual to compare reflect.Value values.
7+
// See issues 43993 and 18871.
8+
package reflectvaluecompare
9+
10+
import (
11+
"go/ast"
12+
"go/token"
13+
"go/types"
14+
15+
"golang.org/x/tools/go/analysis"
16+
"golang.org/x/tools/go/analysis/passes/inspect"
17+
"golang.org/x/tools/go/ast/inspector"
18+
"golang.org/x/tools/go/types/typeutil"
19+
)
20+
21+
const Doc = `check for comparing reflect.Value values with == or reflect.DeepEqual
22+
23+
The reflectvaluecompare checker looks for expressions of the form:
24+
25+
v1 == v2
26+
v1 != v2
27+
reflect.DeepEqual(v1, v2)
28+
29+
where v1 or v2 are reflect.Values. Comparing reflect.Values directly
30+
is almost certainly not correct, as it compares the reflect package's
31+
internal representation, not the underlying value.
32+
Likely what is intended is:
33+
34+
v1.Interface() == v2.Interface()
35+
v1.Interface() != v2.Interface()
36+
reflect.DeepEqual(v1.Interface(), v2.Interface())
37+
`
38+
39+
var Analyzer = &analysis.Analyzer{
40+
Name: "reflectvaluecompare",
41+
Doc: Doc,
42+
Requires: []*analysis.Analyzer{inspect.Analyzer},
43+
Run: run,
44+
}
45+
46+
func run(pass *analysis.Pass) (interface{}, error) {
47+
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
48+
49+
nodeFilter := []ast.Node{
50+
(*ast.BinaryExpr)(nil),
51+
(*ast.CallExpr)(nil),
52+
}
53+
inspect.Preorder(nodeFilter, func(n ast.Node) {
54+
switch n := n.(type) {
55+
case *ast.BinaryExpr:
56+
if n.Op != token.EQL && n.Op != token.NEQ {
57+
return
58+
}
59+
if isReflectValue(pass, n.X) || isReflectValue(pass, n.Y) {
60+
if n.Op == token.EQL {
61+
pass.ReportRangef(n, "avoid using == with reflect.Value")
62+
} else {
63+
pass.ReportRangef(n, "avoid using != with reflect.Value")
64+
}
65+
}
66+
case *ast.CallExpr:
67+
fn, ok := typeutil.Callee(pass.TypesInfo, n).(*types.Func)
68+
if !ok {
69+
return
70+
}
71+
if fn.FullName() == "reflect.DeepEqual" && (isReflectValue(pass, n.Args[0]) || isReflectValue(pass, n.Args[1])) {
72+
pass.ReportRangef(n, "avoid using reflect.DeepEqual with reflect.Value")
73+
}
74+
}
75+
})
76+
return nil, nil
77+
}
78+
79+
// isReflectValue reports whether the type of e is reflect.Value.
80+
func isReflectValue(pass *analysis.Pass, e ast.Expr) bool {
81+
tv, ok := pass.TypesInfo.Types[e]
82+
if !ok { // no type info, something else is wrong
83+
return false
84+
}
85+
// See if the type is reflect.Value
86+
named, ok := tv.Type.(*types.Named)
87+
if !ok {
88+
return false
89+
}
90+
if obj := named.Obj(); obj == nil || obj.Pkg() == nil || obj.Pkg().Path() != "reflect" || obj.Name() != "Value" {
91+
return false
92+
}
93+
if _, ok := e.(*ast.CompositeLit); ok {
94+
// This is reflect.Value{}. Don't treat that as an error.
95+
// Users should probably use x.IsValid() rather than x == reflect.Value{}, but the latter isn't wrong.
96+
return false
97+
}
98+
return true
99+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package reflectvaluecompare_test
6+
7+
import (
8+
"testing"
9+
10+
"golang.org/x/tools/go/analysis/analysistest"
11+
"golang.org/x/tools/go/analysis/passes/reflectvaluecompare"
12+
)
13+
14+
func TestReflectValueCompare(t *testing.T) {
15+
testdata := analysistest.TestData()
16+
analysistest.Run(t, testdata, reflectvaluecompare.Analyzer, "a")
17+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This file contains tests for the reflectvaluecompare checker.
6+
7+
package a
8+
9+
import (
10+
"reflect"
11+
)
12+
13+
func f() {
14+
var x, y reflect.Value
15+
var a, b interface{}
16+
_ = x == y // want `avoid using == with reflect.Value`
17+
_ = x == a // want `avoid using == with reflect.Value`
18+
_ = a == x // want `avoid using == with reflect.Value`
19+
_ = a == b
20+
21+
// Comparing to reflect.Value{} is ok.
22+
_ = a == reflect.Value{}
23+
_ = reflect.Value{} == a
24+
_ = reflect.Value{} == reflect.Value{}
25+
}
26+
func g() {
27+
var x, y reflect.Value
28+
var a, b interface{}
29+
_ = x != y // want `avoid using != with reflect.Value`
30+
_ = x != a // want `avoid using != with reflect.Value`
31+
_ = a != x // want `avoid using != with reflect.Value`
32+
_ = a != b
33+
34+
// Comparing to reflect.Value{} is ok.
35+
_ = a != reflect.Value{}
36+
_ = reflect.Value{} != a
37+
_ = reflect.Value{} != reflect.Value{}
38+
}
39+
func h() {
40+
var x, y reflect.Value
41+
var a, b interface{}
42+
reflect.DeepEqual(x, y) // want `avoid using reflect.DeepEqual with reflect.Value`
43+
reflect.DeepEqual(x, a) // want `avoid using reflect.DeepEqual with reflect.Value`
44+
reflect.DeepEqual(a, x) // want `avoid using reflect.DeepEqual with reflect.Value`
45+
reflect.DeepEqual(a, b)
46+
47+
// Comparing to reflect.Value{} is ok.
48+
reflect.DeepEqual(reflect.Value{}, a)
49+
reflect.DeepEqual(a, reflect.Value{})
50+
reflect.DeepEqual(reflect.Value{}, reflect.Value{})
51+
}

go/internal/gcimporter/gcimporter11_test.go

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)