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

False positive when ranging over a slice of pointers #2

Closed
dgryski opened this issue Jun 23, 2020 · 1 comment · Fixed by #6
Closed

False positive when ranging over a slice of pointers #2

dgryski opened this issue Jun 23, 2020 · 1 comment · Fixed by #6
Assignees
Labels
bug Something isn't working

Comments

@dgryski
Copy link

dgryski commented Jun 23, 2020

package main

import (
	"fmt"
)

type Node struct {
	name string
}

type Nodes []*Node

type Graph struct {
	nodes Nodes
}

func main() {
	var graph Graph
	var s *string

	graph.nodes = Nodes{&Node{"hello"}, &Node{"world"}, &Node{"foo"}, &Node{"bar"}, &Node{"baz"}}

	for i, n := range graph.nodes {
		if i == 2 {
			s = &n.name // foo.go:23:9: exporting a pointer for the loop variable n
		}
	}

	fmt.Println(*s)
}

This is valid code because n takes new pointer values, so &n.name points correctly to different names.

@kyoh86 kyoh86 self-assigned this Jun 25, 2020
@kyoh86 kyoh86 added the bug Something isn't working label Jun 25, 2020
kyoh86 added a commit that referenced this issue Jun 25, 2020
kyoh86 added a commit that referenced this issue Jun 25, 2020
kyoh86 added a commit that referenced this issue Jun 25, 2020
kyoh86 added a commit that referenced this issue Jun 25, 2020
@kyoh86 kyoh86 closed this as completed in #6 Jun 25, 2020
@hansbogert

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants