-
Notifications
You must be signed in to change notification settings - Fork 491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: using slices.Contains to simplify the code #6234
base: master
Are you sure you want to change the base?
Conversation
Thanks, makes sense. I wonder if there's a tool to find the old code pattern. We've switched to slices.Contains in many places, but missed this one. |
I also search by keywords, and there will be a lot of omissions. But I think I can write such a tool |
I did something with
|
Oh, the one you found can't be changed. That accountsList is a map, not a slice. My semgrep rule had the same issue. I fixed that, so there are only 5 detected now, instead of 11. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove approval, I have to comment. Issue is the map vs slice thing.
Great job. I'll modify the other items you found. |
Signed-off-by: damuzhi0810 <rust@before.tech>
Everything else has been modified, I will keep an eye on ci. But I feel like these are data/transactions/logic/eval.go
❯❯❱ Users.jj.no-reimplement-slices.Contains
Don't write a loop for that, use slices.Contains instead!
5301┆ for _, assetID := range cx.txn.Txn.ForeignAssets {
5302┆ if assetID == aid {
5303┆ return true
5304┆ }
5305┆ }
⋮┆----------------------------------------
5345┆ for _, appID := range cx.txn.Txn.ForeignApps {
5346┆ if appID == aid {
5347┆ return true
5348┆ }
5349┆ } Since there is no return false operation, it may not be possible to use slice.Contains optimization. |
|
break | ||
} | ||
} | ||
isKnown := slices.Contains(recordTypes, recordType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Careful. The loop was iterating over append(recordTypes, "")
. I guess blank matters here.
"strconv" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/algorand/go-deadlock" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that dropping this import is why you're getting the deadlock
complaints.
if !slices.Contains(they, x) { | ||
return append(they, x) | ||
} | ||
return append(they, x) | ||
return they |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer swapping the return
lines and removing the !
in the predicate. All else being equal, I prefer to avoid negation in predicates.
Summary
This is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.
Test Plan