File: range-over-func-67237.txt

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (82 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

This test verifies that SSA-based analyzers don't run on packages that
use range-over-func. This is an emergency fix of #67237 (for buildssa)
until we land https://go.dev/cl/555075.

Similarly, it is an emergency fix of dominikh/go-tools#1494 (for
buildir) until that package is similarly fixed for go1.23.

Explanation:
- Package p depends on q and r, and analyzers buildssa and buildir
  depend on norangeoverfunc.
- Analysis pass norangeoverfunc@q fails, thus norangeoverfunc@p is not
  executed; but norangeoverfunc@r is ok
- nilness requires buildssa, which is not facty, so it can run on p and r.
- SA4010 (CheckIneffectiveAppend) requires buildir, which is facty,
  so SA4010 can run only on r.

We don't import any std packages because even "fmt" depends on
range-over-func now (which means that in practice, everything does).

-- flags --
-min_go=go1.23

-- settings.json --
{
	"staticcheck": true,
	"analyses": {"SA4010": true}
}

-- go.mod --
module example.com

go 1.23

-- p/p.go --
package p // a dependency uses range-over-func, so nilness runs but SA4010 cannot (buildir is facty)

import (
	_ "example.com/q"
	_ "example.com/r"
)

func f(ptr *int) {
	if ptr == nil {
		println(*ptr) //@diag(re"[*]ptr", re"nil dereference in load")
	}

	var s []int
	s = append(s, 1) // no SA4010 finding
}

-- q/q.go --
package q // uses range-over-func, so no diagnostics from SA4010

type iterSeq[T any] func(yield func(T) bool)

func f(seq iterSeq[int]) {
	for x := range seq {
		println(x)
	}

	var s []int
	s = append(s, 1) // no SA4010 finding
}

func _(ptr *int) {
	if ptr == nil {
		println(*ptr) //@diag(re"[*]ptr", re"nil dereference in load")
	}
}

-- r/r.go --
package r // does not use range-over-func, so nilness and SA4010 report diagnosticcs

func f(ptr *int) {
	if ptr == nil {
		println(*ptr) //@diag(re"[*]ptr", re"nil dereference in load")
	}

	var s []int
	s = append(s, 1) //@ diag(re`s`, re`s is never used`), diag(re`append`, re`append is never used`)
}