Skip to content

Conversation

cristaloleg
Copy link
Member

@cristaloleg cristaloleg commented Sep 4, 2023

Got bitten today.

Covers 2 cases:

  • when sync.OnceFunc result is invoked in-place (can be called many times)
  • when sync.OnceFunc result isn't assigned to a variable (effectively no-op)

//doc:after fooOnce := sync.OnceFunc(foo); ...; fooOnce()
func badSyncOnceFunc(m dsl.Matcher) {
m.Match(`$*_; sync.OnceFunc($x); $*_;`).
Suggest("sync.OnceFunc($x)()").
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm proposing to just invoke the result and after that match below will say "hey, it's still wrong".

Where(m.GoVersion().GreaterEqThan("1.21"))

m.Match(`sync.OnceFunc($x)()`).
Report("possible sync.OnceFunc misuse, consider to assign sync.OnceFunc($x) to a variable").
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this message can be better (and shorter). Better ideas?

@cristaloleg cristaloleg merged commit cdcd0fc into master Sep 5, 2023
@cristaloleg cristaloleg deleted the oncefunc-check branch September 5, 2023 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants