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

SA4006: detect unused writes with += #1329

Open
cespare opened this issue Oct 18, 2022 · 0 comments · May be fixed by #1555
Open

SA4006: detect unused writes with += #1329

cespare opened this issue Oct 18, 2022 · 0 comments · May be fixed by #1555

Comments

@cespare
Copy link

cespare commented Oct 18, 2022

$ staticcheck -debug.version
staticcheck 2022.1.3 (v0.3.3)

Compiled with Go version: go1.18.4
Main module:
        honnef.co/go/[email protected] (sum: h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=)
Dependencies:
        github.com/BurntSushi/[email protected] (sum: h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=)
        golang.org/x/exp/[email protected] (sum: h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=)
        golang.org/x/[email protected] (sum: h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=)
        golang.org/x/[email protected] (sum: h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=)
        golang.org/x/[email protected] (sum: h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg=)

Go version is 1.19.2 linux/amd64.

Example program:

package main

func main() {
	dir := "a"
	dir += "/b"
}

Staticcheck doesn't complain about that. But if I tweak it to

package main

func main() {
	dir := "a"
	dir = dir + "/b"
}

Then I get

x/x.go:5:2: this value of dir is never used (SA4006)

Could SA4006 better detect this case with +=?

@cespare cespare added false-negative needs-triage Newly filed issue that needs triage labels Oct 18, 2022
@dominikh dominikh removed the needs-triage Newly filed issue that needs triage label Oct 26, 2022
arp242 added a commit to arp242/go-tools that referenced this issue May 31, 2024
In the case of compound operators such as +=, -=, etc we need to use the
left-hand side to get the value.

For IncDec we need a different codepath from AssignStmt altogether.

Fixes dominikh#1329
arp242 added a commit to arp242/go-tools that referenced this issue Jun 1, 2024
In the case of compound operators such as +=, -=, etc we need to use the
left-hand side to get the value.

For IncDec we need a different codepath from AssignStmt altogether.

Fixes dominikh#1329
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants