Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: Warn UnusedVar for unused assign #56598

Closed
igoracmelo opened this issue Nov 5, 2022 · 2 comments
Closed

x/tools/gopls: Warn UnusedVar for unused assign #56598

igoracmelo opened this issue Nov 5, 2022 · 2 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@igoracmelo
Copy link

gopls version

Build info
----------
golang.org/x/tools/gopls v0.10.1
    golang.org/x/tools/gopls@v0.10.1 h1:JoHe17pdZ8Vsa24/GUO8iTVTKPh0EOBiWpPop7XJybI=
    github.com/BurntSushi/toml@v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
    github.com/google/go-cmp@v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
    golang.org/x/exp/typeparams@v0.0.0-20220722155223-a9213eeb770e h1:7Xs2YCOpMlNqSQSmrrnhlzBXIE/bpMecZplbLePTJvE=
    golang.org/x/mod@v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
    golang.org/x/sync@v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
    golang.org/x/sys@v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
    golang.org/x/text@v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
    golang.org/x/tools@v0.2.1-0.20221101170700-b5bc717366b2 h1:KBm+UwBaO/tdQ35tfGvxH1FUCiXRg4MoTzkznsdeab8=
    golang.org/x/vuln@v0.0.0-20221010193109-563322be2ea9 h1:KaYZQUtEEaV8aVADIHAuYBTjo77aUcCvC7KTGKM3J1I=
    honnef.co/go/tools@v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
    mvdan.cc/gofumpt@v0.3.1 h1:avhhrOmv0IuvQVK7fvwV91oFSGAk5/6Po8GXTzICeu8=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.18.1

go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/igor/.cache/go-build"
GOENV="/home/igor/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/igor/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/igor/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3498574676=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Changing the value of a variable won't warn me that the new value is not being used.

_, err := os.Stat("somefile")

// can't forget to handle error, because compiler forces me to use the variable
if err != nil {
	// ...
}	

_, err = os.Stat("anotherfile")
// forgets to handle error, because compiler doesn't force me nor warn that the new value is not used

doOtherStuff()

Workaround 1: declaring err inside an if scope:

if _, err := os.Stat("somefile"); err != nil {
	// ...
}

if _, err := os.Stat("somefile"); false {
	// err declared but not used
}

This solution can get very messy. Example:

if a, err := funcThatReturnsTwoThings("arg1", "arg2"); err != nil {
	if b, err := funcThatUsesA(a); err != nil {
		if c, err := funcThatUsesA(b); err != nil {
			fmt.Println(c)
		}	
	}
}

Workaround 2: different error variables:

a, err1 := funcThatReturnsTwoThings("arg1", "arg2")
if err1 != nil {
}

b, err2 := funcThatUsesA(a)
if err2 != nil {
}

c, err3 := funcThatUsesA(b)
if err3 != nil {
}

fmt.Println(c)

It is cleaner, and may be the best workaround, but makes me creates more variables.

And it is not just for error handling. Warning variables that are reassigned and not used can help catching many bugs in existing code.

What did you expect to see?

Could be a warn or an error:

value assigned to err is not used (UnusedVar)

Example in Rust:
image

What did you see instead?

Nothing.

Editor and settings

I don't believe its necessary for this issue.

Logs

I don't believe its necessary for this issue.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 5, 2022
@gopherbot gopherbot added this to the Unreleased milestone Nov 5, 2022
@igoracmelo
Copy link
Author

I just found out that the golangci-lint has this warning.
It fixes my problem, but I still think it is a valid request for gopls.

image

@hyangah
Copy link
Contributor

hyangah commented Nov 7, 2022

Related: #23142 (supporting from vet, not gopls though).

This feature is already available in VSCode through staticcheck, the default linter used by vscode go extension also detects it. If it didn't detect it, please make sure the staticcheck is installed and run.

Screen Shot 2022-11-07 at 2 12 45 PM

If the proposal #23142 is accepted and implemented in vet, gopls will get it for free.
If the proposal #23142 is not accepted, gopls still can get it through its staticcheck integration already
(see setting; in vscode-go "gopls": { "ui.diagnostic.staticcheck": true })
Screen Shot 2022-11-07 at 2 14 57 PM

@hyangah hyangah closed this as completed Nov 7, 2022
@hyangah hyangah modified the milestones: Unreleased, gopls/unplanned Nov 9, 2022
@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2022
@golang golang locked and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants