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

unused: flag unused variables with initializers #507

Closed
dominikh opened this issue Jun 6, 2019 · 0 comments
Closed

unused: flag unused variables with initializers #507

dominikh opened this issue Jun 6, 2019 · 0 comments
Labels
false-negative started Issues we've started working on

Comments

@dominikh
Copy link
Owner

dominikh commented Jun 6, 2019

The rewrite of unused no longer flags the following code:

package pkg
var x = [3]int{1,2,3}

This is caused by the SSA that go/ssa produces:

func init():
0:                                                                entry P:0 S:2
	t0 = *init$guard                                                   bool
	if t0 goto 2 else 1
1:                                                           init.start P:1 S:1
	*init$guard = true:bool
	t1 = &x[0:int]                                                     *int
	t2 = &x[1:int]                                                     *int
	t3 = &x[2:int]                                                     *int
	*t1 = 1:int
	*t2 = 2:int
	*t3 = 3:int
	jump 2
2:                                                            init.done P:2 S:0
	return

Because the init function is taking addresses of values in x, x is clearly used.

This is not a problem for slices, because they generate different code (an array gets constructed, sliced, and assigned to the variable. Assignment alone isn't considered a use.)

This is, however, also a problem for structs, as

var x = t{1}

generates

t1 = &x.x [#0]                                                     *int
*t1 = 1:int

Another annoying side effect of this is that function calls used as part of the initialization are marked as used by the init function, not the variable.

@dominikh dominikh changed the title unused: flag unused variables of type array unused: flag unused variables with initializers Jun 6, 2019
@dominikh dominikh added the started Issues we've started working on label Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-negative started Issues we've started working on
Projects
None yet
Development

No branches or pull requests

1 participant