golang.org/x/tools/gopls@v0.15.3/internal/test/integration/diagnostics/undeclared_test.go (about) 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 diagnostics 6 7 import ( 8 "testing" 9 10 "golang.org/x/tools/gopls/internal/protocol" 11 . "golang.org/x/tools/gopls/internal/test/integration" 12 ) 13 14 func TestUndeclaredDiagnostics(t *testing.T) { 15 src := ` 16 -- go.mod -- 17 module mod.com 18 19 go 1.12 20 -- a/a.go -- 21 package a 22 23 func _() int { 24 return x 25 } 26 -- b/b.go -- 27 package b 28 29 func _() int { 30 var y int 31 y = y 32 return y 33 } 34 ` 35 Run(t, src, func(t *testing.T, env *Env) { 36 isUnnecessary := func(diag protocol.Diagnostic) bool { 37 for _, tag := range diag.Tags { 38 if tag == protocol.Unnecessary { 39 return true 40 } 41 } 42 return false 43 } 44 45 // 'x' is undeclared, but still necessary. 46 env.OpenFile("a/a.go") 47 var adiags protocol.PublishDiagnosticsParams 48 env.AfterChange( 49 Diagnostics(env.AtRegexp("a/a.go", "x")), 50 ReadDiagnostics("a/a.go", &adiags), 51 ) 52 if got := len(adiags.Diagnostics); got != 1 { 53 t.Errorf("len(Diagnostics) = %d, want 1", got) 54 } 55 if diag := adiags.Diagnostics[0]; isUnnecessary(diag) { 56 t.Errorf("%v tagged unnecessary, want necessary", diag) 57 } 58 59 // 'y = y' is pointless, and should be detected as unnecessary. 60 env.OpenFile("b/b.go") 61 var bdiags protocol.PublishDiagnosticsParams 62 env.AfterChange( 63 Diagnostics(env.AtRegexp("b/b.go", "y = y")), 64 ReadDiagnostics("b/b.go", &bdiags), 65 ) 66 if got := len(bdiags.Diagnostics); got != 1 { 67 t.Errorf("len(Diagnostics) = %d, want 1", got) 68 } 69 if diag := bdiags.Diagnostics[0]; !isUnnecessary(diag) { 70 t.Errorf("%v tagged necessary, want unnecessary", diag) 71 } 72 }) 73 }