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

Match method call #39

Closed
arxeiss opened this issue Jul 3, 2023 · 1 comment · Fixed by #40
Closed

Match method call #39

arxeiss opened this issue Jul 3, 2023 · 1 comment · Fixed by #40

Comments

@arxeiss
Copy link

arxeiss commented Jul 3, 2023

Is there a way how to match only method call and not every identifier with the same name?

We have code like this below. On Identifier structure we define ID function, which we should use always. And GID which is more like a helper, which we can use only in Tests, so it can panic easily.

I wanted to use forbidigo to match all usage of GID() outside *_test.go files, but I wasn't successful.

const GID = "gid:"

type URIGidIdentifier struct {
	GID string `uri:"gid" binding:"required,gid"`
}

type Identifier struct {
    // ...
}

func (x *Identifier) GID() string {
	gid, err := x.ID()
	if err != nil {
		panic("use ID() to check error")
	}
	return gid
}

func (x *Identifier) ID() (string, error) {
    // ...
    return "", nil
}

func xx() {
    a := (&Identifier{}).GID() // this is only point, where forbidigo should complain.
}

First try

I'm using GolangCI linter and if I added this, it was complaining about GID constant and GID field in URIGidIdentifier too. And also during GID() declaration.

    forbidigo:
        forbid:
            - p: ^GID$
              msg: Use ID() instead

Second try

I tried to check the function call like this, having escaped () in the regex, but now it is matching nothing. Nor false positives as above, nor valid ocurrences.

    forbidigo:
        forbid:
            - p: ^GID\(\)$
              msg: Use ID() instead

Is there a way how to make this works?

@ashanbrown
Copy link
Owner

To match a specific class, you'd need to use the new somewhat experimental analyze-types option as shown at https://github.com/golangci/golangci-lint/blob/0f31e51b1b82dfeeab6156f0876af58bd168d140/test/testdata/configs/forbidigo_struct.yml#L3.

I feel like the constant declaration and name probably shouldn't be matched, so that is arguably a bug. For now, you could consider using nolint for those lines (although maybe this becomes moot when you add the type prefix).

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 a pull request may close this issue.

2 participants