go/analysis/passes/modernize: don't consider selects for min/max pass
Fixes golang/go#77671.
diff --git a/go/analysis/passes/modernize/minmax.go b/go/analysis/passes/modernize/minmax.go
index a77ed83..b4b8dba 100644
--- a/go/analysis/passes/modernize/minmax.go
+++ b/go/analysis/passes/modernize/minmax.go
@@ -147,6 +147,13 @@
lhs0 := fassign.Lhs[0]
rhs0 := fassign.Rhs[0]
+ // If the assignment occurs within a select
+ // comms clause (like "case lhs0 := <-rhs0:"),
+ // there's no way of rewriting it into a min/max call.
+ if prev.ParentEdgeKind() == edge.CommClause_Comm {
+ return
+ }
+
if astutil.EqualSyntax(lhs, lhs0) {
if astutil.EqualSyntax(rhs, a) && (astutil.EqualSyntax(rhs0, b) || astutil.EqualSyntax(lhs0, b)) {
sign = +sign
diff --git a/go/analysis/passes/modernize/testdata/src/minmax/minmax.go b/go/analysis/passes/modernize/testdata/src/minmax/minmax.go
index 74d84b2..4ef6723 100644
--- a/go/analysis/passes/modernize/testdata/src/minmax/minmax.go
+++ b/go/analysis/passes/modernize/testdata/src/minmax/minmax.go
@@ -169,3 +169,14 @@
}
return x
}
+
+// Regression test for https://go.dev/issue/77671
+func selectComm(ch chan int) int {
+ select {
+ case n := <-ch:
+ if n > 100 {
+ n = 100
+ }
+ return n
+ }
+}
diff --git a/go/analysis/passes/modernize/testdata/src/minmax/minmax.go.golden b/go/analysis/passes/modernize/testdata/src/minmax/minmax.go.golden
index f8dc94b..96b4965 100644
--- a/go/analysis/passes/modernize/testdata/src/minmax/minmax.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/minmax/minmax.go.golden
@@ -168,3 +168,14 @@
}
return x
}
+
+// Regression test for https://go.dev/issue/77671
+func selectComm(ch chan int) int {
+ select {
+ case n := <-ch:
+ if n > 100 {
+ n = 100
+ }
+ return n
+ }
+}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Code-Review | +2 |
Thanks for this fix. I feel like this is one of a whole category of grammar restrictions that we need to document in my (long awaiting) checklist for analyzer writers. Another example (of many) is that `if STMT; EXPR { ... }` can hold only certain kinds of STMT.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Commit-Queue | +1 |
Thanks for this fix. I feel like this is one of a whole category of grammar restrictions that we need to document in my (long awaiting) checklist for analyzer writers. Another example (of many) is that `if STMT; EXPR { ... }` can hold only certain kinds of STMT.
*long awaited
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Code-Review | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
go/analysis/passes/modernize: don't consider selects for min/max pass
Fixes golang/go#77671.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
[internal-branch.go1.26-vendor] go/analysis/passes/modernize: don't consider selects for min/max pass
Updates golang/go#77804.
Change-Id: I704fc5327218c5041fa56c29b92f23376a6a6964
Reviewed-on: https://go-review.googlesource.com/c/tools/+/746541
Auto-Submit: Alan Donovan <adon...@google.com>
Reviewed-by: Alan Donovan <adon...@google.com>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drc...@google.com>
(cherry picked from commit 5c2a45909eac659b93659e0853eb43738b672c33)
diff --git a/go/analysis/passes/modernize/minmax.go b/go/analysis/passes/modernize/minmax.go
index 23a0977..812bad8 100644
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Commit-Queue | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
if prev.ParentEdgeKind() == edge.CommClause_Comm {```suggestion
if ek, _ := prev.ParentEdge(); ek == edge.CommClause_Comm {
```
Since the vendor copy is from before we added `Cursor.ParentEdgeKind`
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Code-Review | +2 |
if prev.ParentEdgeKind() == edge.CommClause_Comm {```suggestion
if ek, _ := prev.ParentEdge(); ek == edge.CommClause_Comm {
```
Since the vendor copy is from before we added `Cursor.ParentEdgeKind`
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |