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

Fix a false-positive from 'unused' #585

Merged
merged 4 commits into from Sep 9, 2019

Conversation

LukeShu
Copy link
Contributor

@LukeShu LukeShu commented Jun 23, 2019

Add failing testcase for false-positive from 'unused'. This false-positive is not present in the upstream stand-alone 'unused' 2019.1.1 program that golangci-lint uses.

I am not (yet?) sure how to fix the problem.

edit: I've managed to fix the issue:

pkg/lint.ContextLoader.filterPackages() did two things:

  1. It removed synthetic "testmain" packages (packages with .Name=="main" and .PkgPath ending with ".test")
  2. It removed pruned subsumed copies of packages; if a package with files "a.go" and "a_test.go", it results in packages.Load giving us two packages:
    • ID=".../a" GoFiles=[a.go]
    • ID=".../a [.../a.test]" GoFiles=[a.go a_test.go]
      The first package is subsumed in the second, and leaving it around results in duplicated work, and confuses the 'deadcode' linter.

However, the 'unused' linter relies on both the ".../a" and ".../a [.../a.test]" packages being present. Pruning them causes it to panic in some situations, which lead to this workaround: golangci/go-tools@af6baa5

While that workaround got it to not panic, it causes incorrect results.

So, split filterPackages() in to two functions: filterTestMainPackages() and filterDuplicatePackages(). The linter.Context.Packages list only gets filterTestMainPackages() called on it, while linter.Context.Program and linter.Context.SSAProgram get both filters applied.

With the source of the panic fixed, roll back a few now-unnecessary commits in go-tools.

This false-positive is not present in the upstream stand-alone 'unused'
2019.1.1 program that golangci-lint uses.
pkg/lint.ContextLoader.filterPackages() did two things:
 1. It removed synthetic "testmain" packages (packages with .Name=="main"
    and .PkgPath ending with ".test")
 2. It removed pruned subsumed copies of packages; if a package with files
    "a.go" and "a_test.go", it results in packages.Load giving us two
    packages:
      - ID=".../a" GoFiles=[a.go]
      - ID=".../a [.../a.test]" GoFiles=[a.go a_test.go]
    The first package is subsumed in the second, and leaving it around
    results in duplicated work, and confuses the 'deadcode' linter.

However, the 'unused' linter relies on both the ".../a" and
".../a [.../a.test]" packages being present.  Pruning them causes it to
panic in some situations, which lead to this workaround:
golangci/go-tools@af6baa5

While that workaround got it to not panic, it causes incorrect results.

So, split filterPackages() in to two functions: filterTestMainPackages()
and filterDuplicatePackages().  The linter.Context.Packages list only
gets filterTestMainPackages() called on it, while linter.Context.Program
and linter.Context.SSAProgram get both filters applied.

With the source of the panic fixed, roll back a few now-unnecessary
commits in go-tools.
@LukeShu LukeShu changed the title Add failing testcase for false-positive from 'unused' Fix a false-positive from 'unused' Jun 23, 2019
@LukeShu
Copy link
Contributor Author

LukeShu commented Aug 9, 2019

Ping

@LukeShu
Copy link
Contributor Author

LukeShu commented Sep 4, 2019

Ping.

@jirfag
Copy link
Member

jirfag commented Sep 9, 2019

@LukeShu sorry for the slow response.
Thank you very much for this contribution, I'll merge it as it passes CI.
Also, I will make optimization in a separate commit for using deduplicated packages for golint and some other linters.

@jirfag jirfag merged commit e87a1cf into golangci:master Sep 9, 2019
jirfag added a commit that referenced this pull request Sep 9, 2019
Use deduplicated packages for all linters except megacheck.
See #585 for details.
jirfag added a commit that referenced this pull request Sep 9, 2019
Use deduplicated packages for all linters except megacheck.
See #585 for details.
@LukeShu LukeShu deleted the lukeshu/fix-unused branch September 9, 2019 18:19
@ldez ldez added this to the v1.18 milestone Mar 6, 2024
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.

None yet

3 participants