Skip to content

Commit bea5215

Browse files
committed
gopls/internalanalysis: DeleteStmt deletes comments
DeleteStmt is called with just the AST so it has trouble deciding if a comment is associated with the statement to be deleted. For instance, for /*A*/ init()/*B*/;/*C/cond()/*D/;/*E*/post() /*F*/ { /*G*/} comment B and C are indistinguishable, as are D and E. That is, as the AST does not say where the semicolons are, B and C could go either with the init() or the cond(), so cannot be removed safely. The same is true for D, E, and the post(). (And there are other similar cases.) But the other comments can be removed as they are unambiguously associated with the statement being deleted. The most common case where the new code differs is that it removes whole lines like stmt // comment Change-Id: I2d0652892241aac3a80f1b0a0cafbfeec2e6274f Reviewed-on: https://go-review.googlesource.com/c/tools/+/710775 Reviewed-by: Alan Donovan <[email protected]> TryBot-Bypass: Peter Weinberger <[email protected]>
1 parent b2d9e77 commit bea5215

File tree

12 files changed

+240
-296
lines changed

12 files changed

+240
-296
lines changed

go/analysis/passes/assign/testdata/src/a/a.go.golden

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ type ST struct {
1515

1616
func (s *ST) SetX(x int, ch chan int) {
1717
// Accidental self-assignment; it should be "s.x = x"
18-
// want "self-assignment of x"
1918
// Another mistake
20-
// want "self-assignment of s.x"
21-
22-
// want "self-assignment of s.l.0."
2319

2420
// Report self-assignment to x but preserve the actual assignment to s.x
2521
s.x = 1 // want "self-assignment of x"
2622
s.x = 1 // want "self-assignment of x"
2723

2824
// Delete multiple self-assignment
29-
// want "self-assignment of x, s.x"
3025
s.l[0] = 1 // want "self-assignment of x, s.x"
3126
s.l[0] = 1 // want "self-assignment of x, s.x"
3227
s.l[0] = 1 // want "self-assignment of x, s.x"
@@ -44,18 +39,14 @@ func num() int { return 2 }
4439

4540
func Index() {
4641
s := []int{1}
47-
// want "self-assignment"
4842

4943
var a [5]int
50-
// want "self-assignment"
5144

5245
pa := &[2]int{1, 2}
53-
// want "self-assignment"
5446

5547
var pss *struct { // report self assignment despite nil dereference
5648
s []int
5749
}
58-
// want "self-assignment"
5950

6051
m := map[int]string{1: "a"}
6152
m[0] = m[0] // bail on map self-assignments due to side effects

go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ type ST[T interface{ ~int }] struct {
1515

1616
func (s *ST[T]) SetX(x T, ch chan T) {
1717
// Accidental self-assignment; it should be "s.x = x"
18-
// want "self-assignment of x"
1918
// Another mistake
20-
// want "self-assignment of s.x"
2119

22-
// want "self-assignment of s.l.0."
2320

2421
// Bail on any potential side effects to avoid false positives
2522
s.l[num()] = s.l[num()]

go/analysis/passes/modernize/testdata/src/forvar/forvar.go.golden

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,28 @@ package forvar
33
func _(m map[int]int, s []int) {
44
// changed
55
for i := range s {
6-
// want "copying variable is unneeded"
76
go f(i)
87
}
98
for _, v := range s {
10-
// want "copying variable is unneeded"
119
go f(v)
1210
}
1311
for k, v := range m {
14-
// want "copying variable is unneeded"
15-
// want "copying variable is unneeded"
1612
go f(k)
1713
go f(v)
1814
}
1915
for k, v := range m {
20-
// want "copying variable is unneeded"
21-
// want "copying variable is unneeded"
2216
go f(k)
2317
go f(v)
2418
}
2519
for k, v := range m {
26-
// want "copying variable is unneeded"
2720
go f(k)
2821
go f(v)
2922
}
3023
for k, v := range m {
31-
// want "copying variable is unneeded"
3224
go f(k)
3325
go f(v)
3426
}
3527
for i := range s {
36-
/* hi */ // want "copying variable is unneeded"
3728
go f(i)
3829
}
3930
// nope

go/analysis/passes/modernize/testdata/src/waitgroup/waitgroup.go.golden

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88
// supported case for pattern 1.
99
func _() {
1010
var wg sync.WaitGroup
11-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1211
wg.Go(func() {
1312
fmt.Println()
1413
})
1514

16-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1715
wg.Go(func() {
1816
})
1917

2018
for range 10 {
21-
// want "Goroutine creation can be simplified using WaitGroup.Go"
2219
wg.Go(func() {
2320
fmt.Println()
2421
})
@@ -28,17 +25,14 @@ func _() {
2825
// supported case for pattern 2.
2926
func _() {
3027
var wg sync.WaitGroup
31-
// want "Goroutine creation can be simplified using WaitGroup.Go"
3228
wg.Go(func() {
3329
fmt.Println()
3430
})
3531

36-
// want "Goroutine creation can be simplified using WaitGroup.Go"
3732
wg.Go(func() {
3833
})
3934

4035
for range 10 {
41-
// want "Goroutine creation can be simplified using WaitGroup.Go"
4236
wg.Go(func() {
4337
fmt.Println()
4438
})
@@ -48,19 +42,16 @@ func _() {
4842
// this function puts some wrong usages but waitgroup modernizer will still offer fixes.
4943
func _() {
5044
var wg sync.WaitGroup
51-
// want "Goroutine creation can be simplified using WaitGroup.Go"
5245
wg.Go(func() {
5346
defer wg.Done()
5447
fmt.Println()
5548
})
5649

57-
// want "Goroutine creation can be simplified using WaitGroup.Go"
5850
wg.Go(func() {
5951
fmt.Println()
6052
wg.Done()
6153
})
6254

63-
// want "Goroutine creation can be simplified using WaitGroup.Go"
6455
wg.Go(func() {
6556
fmt.Println()
6657
wg.Done()
@@ -152,20 +143,17 @@ type ServerContainer struct {
152143

153144
func _() {
154145
var s Server
155-
// want "Goroutine creation can be simplified using WaitGroup.Go"
156146
s.wg.Go(func() {
157147
print()
158148
})
159149

160150
var sc ServerContainer
161-
// want "Goroutine creation can be simplified using WaitGroup.Go"
162151
sc.serv.wg.Go(func() {
163152
print()
164153
})
165154

166155
var wg sync.WaitGroup
167156
arr := [1]*sync.WaitGroup{&wg}
168-
// want "Goroutine creation can be simplified using WaitGroup.Go"
169157
arr[0].Go(func() {
170158
print()
171159
})

go/analysis/passes/modernize/testdata/src/waitgroup/waitgroup_alias.go.golden

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import (
77

88
func _() {
99
var wg sync1.WaitGroup
10-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1110
wg.Go(func() {
1211
fmt.Println()
1312
})
1413

15-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1614
wg.Go(func() {
1715
fmt.Println()
1816
})

go/analysis/passes/modernize/testdata/src/waitgroup/waitgroup_dot.go.golden

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
// supported case for pattern 1.
99
func _() {
1010
var wg WaitGroup
11-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1211
wg.Go(func() {
1312
fmt.Println()
1413
})
1514

16-
// want "Goroutine creation can be simplified using WaitGroup.Go"
1715
wg.Go(func() {
1816
fmt.Println()
1917
})

0 commit comments

Comments
 (0)