Skip to content

Honor -exclude for calls to same-package functions - #282

Open
Eljees wants to merge 1 commit into
kisielk:masterfrom
Eljees:fix-exclude-same-package-call
Open

Eljees wants to merge 1 commit into
kisielk:masterfrom
Eljees:fix-exclude-same-package-call

Conversation

@Eljees

@Eljees Eljees commented Sep 21, 2026

Copy link
Copy Markdown

Fixes #251.

Bug

-exclude (checker.Exclusions.Symbols) matches a called function's fully-qualified name, but the name was only ever computed for calls that go through a selector expression (pkg.Func() / recv.Method(), handled in selectorAndFunc). A call to a package-level function from within its own package has no selector — it's a plain identifier (FirstFunc(), not first.FirstFunc()) — so namesForExcludeCheck returned nil for it and the exclusion never matched, even though the same function is correctly excluded when called from an importing package.

Fix

When the selector lookup fails, also try resolving the call as a plain identifier via typesInfo.ObjectOf, and compute FullName() the same way the selector branch does. This is purely additive: the existing selector path is untouched, and non-function identifiers (ok stays false) fall through exactly as before.

Testing

Added TestSamePackageFunctionExclude, modeled on the existing TestTypeParameterizedFunctionExclude: two synthetic packages, one (first) declaring FirstFunc and calling it internally (unqualified) from ThirdFunc, the other (second) calling it externally via first.FirstFunc().

Without the fix, excluding samepkgtest/first.FirstFunc still leaves the in-package call flagged — verified this fails for the expected reason:

errcheck_test.go:373: expected 0 errors (both in-package and cross-package calls excluded), got 1: [{first/first.go:12:11 FirstFunc() }]

With the fix, both calls are excluded and the full package test suite (go test ./...) passes.

namesForExcludeCheck only computed a name for calls with an explicit selector (pkg.Func() / recv.Method()). A call to a package-level function from within its own package has no selector -- it is a plain identifier -- so such calls were never matched against -exclude, even though the exact same function called from an importing package (pkg.FirstFunc()) was correctly excluded.

Resolve the identifier directly via types.Info.ObjectOf and compute its FullName() the same way the selector branch does, so in-package calls are excluded on equal footing with cross-package calls.

Fixes kisielk#251
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.

The -exclude option does not seem to work for functions declared in the same package

1 participant