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

WIP: Fix unused linter #914

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions pkg/golinters/goanalysis/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,19 @@ func (loadMode LoadMode) String() string {
}

type Linter struct {
name, desc string
analyzers []*analysis.Analyzer
cfg map[string]map[string]interface{}
issuesReporter func(*linter.Context) []Issue
contextSetter func(*linter.Context)
loadMode LoadMode
needUseOriginalPackages bool
isTypecheckModeOn bool
name, desc string
analyzers []*analysis.Analyzer
cfg map[string]map[string]interface{}
issuesReporter func(*linter.Context) []Issue
contextSetter func(*linter.Context)
loadMode LoadMode
isTypecheckModeOn bool
}

func NewLinter(name, desc string, analyzers []*analysis.Analyzer, cfg map[string]map[string]interface{}) *Linter {
return &Linter{name: name, desc: desc, analyzers: analyzers, cfg: cfg}
}

func (lnt *Linter) UseOriginalPackages() {
lnt.needUseOriginalPackages = true
}

func (lnt *Linter) SetTypecheckMode() {
lnt.isTypecheckModeOn = true
}
Expand Down Expand Up @@ -264,10 +259,6 @@ func (lnt *Linter) getAnalyzers() []*analysis.Analyzer {
return lnt.analyzers
}

func (lnt *Linter) useOriginalPackages() bool {
return lnt.needUseOriginalPackages
}

func (lnt *Linter) isTypecheckMode() bool {
return lnt.isTypecheckModeOn
}
Expand All @@ -287,7 +278,6 @@ type runAnalyzersConfig interface {
getName() string
getLinterNameForDiagnostic(*Diagnostic) string
getAnalyzers() []*analysis.Analyzer
useOriginalPackages() bool
isTypecheckMode() bool
reportIssues(*linter.Context) []Issue
getLoadMode() LoadMode
Expand Down Expand Up @@ -437,9 +427,6 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
runner := newRunner(cfg.getName(), log, lintCtx.PkgCache, lintCtx.LoadGuard, cfg.getLoadMode(), sw)

pkgs := lintCtx.Packages
if cfg.useOriginalPackages() {
pkgs = lintCtx.OriginalPackages
}

issues, pkgsFromCache := loadIssuesFromCache(pkgs, lintCtx, cfg.getAnalyzers())
var pkgsToAnalyze []*packages.Package
Expand Down
4 changes: 0 additions & 4 deletions pkg/golinters/goanalysis/metalinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (ml MetaLinter) getName() string {
return "metalinter"
}

func (ml MetaLinter) useOriginalPackages() bool {
return false // `unused` can't be run by this metalinter
}

func (ml MetaLinter) reportIssues(lintCtx *linter.Context) []Issue {
var ret []Issue
for _, lnt := range ml.linters {
Expand Down
5 changes: 2 additions & 3 deletions pkg/golinters/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewUnused() *goanalysis.Linter {
nil,
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
typesToPkg := map[*types.Package]*packages.Package{}
for _, pkg := range lintCtx.OriginalPackages {
for _, pkg := range lintCtx.Packages {
typesToPkg[pkg.Types] = pkg
}

Expand All @@ -51,7 +51,6 @@ func NewUnused() *goanalysis.Linter {
return issues
}).WithContextSetter(func(lintCtx *linter.Context) {
u.WholeProgram = lintCtx.Settings().Unused.CheckExported
}).WithLoadMode(goanalysis.LoadModeWholeProgram)
lnt.UseOriginalPackages()
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
return lnt
}
4 changes: 0 additions & 4 deletions pkg/lint/linter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ type Context struct {
// Packages are deduplicated (test and normal packages) packages
Packages []*packages.Package

// OriginalPackages aren't deduplicated: they contain both normal and test
// version for each of packages
OriginalPackages []*packages.Package

Cfg *config.Config
FileCache *fsutils.FileCache
LineCache *fsutils.LineCache
Expand Down
7 changes: 1 addition & 6 deletions pkg/lint/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,7 @@ func (cl *ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*l
}

ret := &linter.Context{
Packages: deduplicatedPkgs,

// At least `unused` linters works properly only on original (not deduplicated) packages,
// see https://github.com/golangci/golangci-lint/pull/585.
OriginalPackages: pkgs,

Packages: deduplicatedPkgs,
Cfg: cl.cfg,
Log: cl.log,
FileCache: cl.fileCache,
Expand Down