Skip to content

x/tools/gopls: unusedparams analyzer reports false positive due to build tags #65479

@adonovan

Description

@adonovan
xtools$ go run ./gopls/internal/analysis/unusedparams/cmd/main.go -c 3 ./gopls/internal/test/integration/fake
/Users/adonovan/w/xtools/gopls/internal/test/integration/fake/workdir.go:75:38: unused parameter: err
72	
73	// isWindowsErrLockViolation reports whether err is ERROR_LOCK_VIOLATION
74	// on Windows.
75	var isWindowsErrLockViolation = func(err error) bool { return false }
76	

The analysis believes that this function is assigned to a variable that is only ever used in call position--because it is never used at all. In fact it is used in non-call position, for an assignment to the variable, though only in a file tagged for GOOS=windows, which of course the analysis cannot see. Consequently it falsely reports that the parameter can be safely removed.

One possibility would be to observe that pass.IgnoredGoFiles contains one or more files, in particular files with build tags, and to conservatively assume that these extra files contain assignments to all variables such as isWindowsErrLockViolation.

This is really a representative for a whole class of built-tag-related analyzer problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)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