Skip to content

Commit 01e0b05

Browse files
committed
internal/refactor/inline: fix condition for splice assignment strategy
In the assign statements strategy, if there are no nontrivial result conversions, we can replace a call with its result expression regardless of the AssignStmt.Tok. For golang/go#70599 Change-Id: Ifa2b615c17d58e3bbc708b9ecf98530858915564 Reviewed-on: https://go-review.googlesource.com/c/tools/+/632937 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 557f540 commit 01e0b05

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

gopls/internal/test/marker/testdata/codeaction/moveparam_issue70599.txt

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
This test reproduces various bugs encountered while bug-bashing on the movement
2-
refactoring.
3-
4-
TODO(rfindley): fix these bugs.
1+
This test checks the fixes for bugs encountered while bug-bashing on the
2+
movement refactoring.
53

64
-- go.mod --
75
module example.com
@@ -74,8 +72,7 @@ func Short(y, x int) (int, int) { //@codeaction("x", "refactor.rewrite.moveParam
7472
}
7573

7674
func _() {
77-
var x, y int
78-
x, y = Short(1, 0)
75+
x, y := Short(1, 0)
7976
_, _ = x, y
8077
}
8178

internal/refactor/inline/inline.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3266,10 +3266,9 @@ func (st *state) assignStmts(callerStmt *ast.AssignStmt, returnOperands []ast.Ex
32663266
//
32673267
// This works as long as we don't need to write any additional type
32683268
// information.
3269-
if callerStmt.Tok == token.ASSIGN && // LHS types already determined before call
3270-
len(nonTrivial) == 0 { // no non-trivial conversions to worry about
3269+
if len(nonTrivial) == 0 { // no non-trivial conversions to worry about
32713270

3272-
logf("substrategy: slice assignment")
3271+
logf("substrategy: splice assignment")
32733272
return []ast.Stmt{&ast.AssignStmt{
32743273
Lhs: lhs,
32753274
Tok: callerStmt.Tok,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
This test checks the splice assignment substrategy.
2+
3+
-- go.mod --
4+
module testdata
5+
6+
go 1.20
7+
8+
-- a.go --
9+
package a
10+
11+
func a() (int32, string) {
12+
return b()
13+
}
14+
15+
func b() (int32, string) {
16+
return 0, "a"
17+
}
18+
19+
func c() (int, chan<- int) {
20+
return 0, make(chan int) // nontrivial conversion
21+
}
22+
23+
-- a1.go --
24+
package a
25+
26+
func _() {
27+
x, y := a() //@ inline(re"a", a1)
28+
}
29+
-- a1 --
30+
package a
31+
32+
func _() {
33+
x, y := b() //@ inline(re"a", a1)
34+
}
35+
-- a2.go --
36+
package a
37+
38+
func _() {
39+
var x, y any
40+
x, y = a() //@ inline(re"a", a2)
41+
}
42+
-- a2 --
43+
package a
44+
45+
func _() {
46+
var x, y any
47+
x, y = b() //@ inline(re"a", a2)
48+
}
49+
-- a3.go --
50+
package a
51+
52+
func _() {
53+
var y chan<- int
54+
x, y := c() //@ inline(re"c", a3)
55+
}
56+
-- a3 --
57+
package a
58+
59+
func _() {
60+
var y chan<- int
61+
x, y := 0, make(chan int) //@ inline(re"c", a3)
62+
}

internal/refactor/inline/testdata/assignment.txtar

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Basic tests of inlining a call on the RHS of an assignment.
22

33
-- go.mod --
44
module testdata
5-
go 1.12
5+
6+
go 1.20
67

78
-- a/a1.go --
89
package a
@@ -104,13 +105,11 @@ package a
104105

105106
import (
106107
"testdata/b"
107-
"testdata/c"
108108
)
109109

110110
func _() {
111111
var y int
112-
var x c.C
113-
x, y = b.B1() //@ inline(re"B", b2)
112+
x, y := b.B1() //@ inline(re"B", b2)
114113
_, _ = x, y
115114
}
116115
-- b3 --

0 commit comments

Comments
 (0)