Skip to content

x/tools/gopls/internal/analysis/unusedfunc: spurious reports in packages that use build tags (or assembly) #71686

Open

Description

@adonovan

gopls' unusedfunc analysis may report spurious "function f is unused" diagnostics when uses exist but only in build-tagged files of the same package that are ignored in the current configuration. See my incorrect delayed comment on https://go.dev/cl/630415 for an example of the confusion.

@cherrymui suggested that the analyzer should offer to move the declaration into the tagged, ignored file that needs it, but that presupposes that our analysis is aware of the ignored file, which it isn't, and which is in general a challenging problem.

But still we could do better than the status quo: if the package contains ignored files, we could qualify the diagnostic with some reference to the build tag. It could be generic such as "(may depend on build tags)", or specific such as "(assuming !purego tags)", where the latter computes a falsifying tag assignment. (If we go the latter route, the analysis framework should provide helpers to do the heavy lifting.)

In principle this should be done for any analyzer whose correctness depends on the package as a whole, not just the current file.

Related: in runtime, this analyzer reports a number of false positives on unreferenced intrinsics--functions that, though never referenced from Go source in the runtime, are referenced indirectly from compiler-generated object code--and on functions that are referenced only from assembly code. The unusedfuncs analyzer should suppress diagnostics for intrinsics, and parse (all?) assembly files in the package looking for references.

Activity

  1. added
    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)
    gopls/analysisIssues related to running analysis in gopls
    on Feb 12, 2025
  2. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    goplsIssues related to the Go language server, gopls.
    on Feb 12, 2025
  3. added this to the Unreleased milestone on Feb 12, 2025
  4. gabyhelp commented on Feb 12, 2025

    @gabyhelp
  5. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Feb 12, 2025
  6. modified the milestones: Unreleased, gopls/backlog on Feb 13, 2025
  7. changed the title [-]x/tools/gopls/internal/analysis/unusedfunc: spurious reports in packages that use build tags[/-] [+]x/tools/gopls/internal/analysis/unusedfunc: spurious reports in packages that use build tags (or assembly)[/+] on Feb 13, 2025
  8. gopherbot commented on Feb 13, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/649256 mentions this issue: gopls/internal/analysis/unusedfunc: don't report runtime intrinsics

  9. gopherbot commented on Jul 29, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/691496 mentions this issue: gopls/internal/analysis/unusedfunc: skip std

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.gopls/analysisIssues related to running analysis in gopls

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions