Skip to content
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

How to match func called in a defer statement? #482

Open
ayushman2609 opened this issue Sep 16, 2024 · 3 comments
Open

How to match func called in a defer statement? #482

ayushman2609 opened this issue Sep 16, 2024 · 3 comments

Comments

@ayushman2609
Copy link

ayushman2609 commented Sep 16, 2024

I have a usecase where I need to check if a Recover func is called in the defer statement in a go routine. I am currently doing this using this rule:

func checkGoStmtUsedInAnonymousFunc(m dsl.Matcher) {
	m.Match(`go func(){$expr; $*_}()`).
		Where(!(m["expr"].Node.Is(`DeferStmt`) && m["expr"].Contains("safeconc.Recover"))).
		Report("anonymous function called in a go routine doesn't have a defer recover function as the first expression")
}

I am getting the safeconc.Recover func from a different package. Currently this rule is matching this func using text matching, I wanted to check if there is a better way to do that which uses the actual func in the package

@quasilyte
Copy link
Owner

I am getting the safeconc.Recover func from a different package.

There are multiple safeconc.Recover providers, is that what you're saying?

Currently this rule is matching this func using text matching

It's not a text matching, Contains parses the AST node and matches against that (safeconc.Recover is a selector expression). There are ways to use a text-based matching, but you're not using it in the example above.

@ayushman2609
Copy link
Author

ayushman2609 commented Sep 16, 2024

There are multiple safeconc.Recover providers, is that what you're saying?

Its been imported from an internal library

import "x/pkg/safeconc" ... defer safeconc.Recover()

It's not a text matching, Contains parses the AST node and matches against that (safeconc.Recover is a selector expression). There are ways to use a text-based matching, but you're not using it in the example above.

Got it. Is this the best way to write this rule then? I was thinking if there is some way I could import the safeconc pkg in the rule and then get the func lit being called in the defer statement and see if its the same as the one I get from the pkg

@quasilyte
Copy link
Owner

quasilyte commented Sep 16, 2024

There are package-aware filters related to types that require Import, but I'm not sure it can be used here.
I'm not sure how to express "comes from a package" filter as it doesn't make sense for an arbitrary expression, only for identifiers, therefore adding it to a top-level could be weird (as in m["expr"].OriginPackage == "foo", because $expr is a defer statement and it may contain multiple symbols).

If you have any ideas how this can be improved and/or expressed in the DSL, please do describe it.

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

No branches or pull requests

2 participants