1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/testdata/range-val-address.go
Bernhard Reisenberger 406b1ce110
range-val-address: fix false positive (#554)
range-val-address: fix false positive (closes #554)
2021-08-26 08:41:58 +02:00

141 lines
3.0 KiB
Go

package fixtures
func rangeValAddress() {
m := map[string]*string{}
mySlice := []string{"A", "B", "C"}
for _, value := range mySlice {
m["address"] = &value // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress2() {
m := map[string]*string{}
mySlice := []string{"A", "B", "C"}
for i := range mySlice {
m["address"] = &mySlice[i]
}
}
func rangeValAddress3() {
m := map[string]*string{}
mySlice := []string{"A", "B", "C"}
for _, value := range mySlice {
a := &value // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
m["address"] = a
}
}
func rangeValAddress4() {
m := []*string{}
mySlice := []string{"A", "B", "C"}
for _, value := range mySlice {
m = append(m, &value) // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress5() {
m := map[*string]string{}
mySlice := []string{"A", "B", "C"}
for _, value := range mySlice {
m[&value] = value // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress6() {
type v struct {
id string
}
m := []*string{}
mySlice := []v{{id: "A"}, {id: "B"}, {id: "C"}}
for _, value := range mySlice {
m = append(m, &value.id) // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress7() {
type v struct {
id string
}
m := []*string{}
for _, value := range []v{{id: "A"}, {id: "B"}, {id: "C"}} {
m = append(m, &value.id) // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress8() {
type v struct {
id string
}
m := []*string{}
mySlice := []*v{{id: "A"}, {id: "B"}, {id: "C"}}
for _, value := range mySlice {
m = append(m, &value.id)
}
}
func rangeValAddress9() {
type v struct {
id string
}
m := []*string{}
mySlice := map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}}
for _, value := range mySlice {
m = append(m, &value.id)
}
}
func rangeValAddress10() {
type v struct {
id string
}
m := []*string{}
for _, value := range map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
m = append(m, &value.id)
}
}
func rangeValAddress11() {
type v struct {
id string
}
m := map[string]*string{}
for key, value := range map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
m[key] = &value.id
}
}
func rangeValAddress12() {
type v struct {
id string
}
m := map[string]*string{}
for key, value := range map[string]v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
m[key] = &value.id // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
}
}
func rangeValAddress13() {
type v struct {
id string
}
m := []*string{}
otherSlice := map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}}
mySlice := otherSlice
for _, value := range mySlice {
m = append(m, &value.id)
}
}