Skip to content

False positive when ranging over a slice of pointers #2

@dgryski

Description

@dgryski
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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions