Skip to content

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

@ayushman2609

Description

@ayushman2609

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions