From e9a5d505fccd74b1921b19154f990b1d29a16c2d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 20 Mar 2021 15:21:13 +0100 Subject: [PATCH] fix: the randomness of the results of the analysis --- pkg/lint/linter/config.go | 3 +++ pkg/lint/lintersdb/enabled_set.go | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 0bc664732c01..2372a011e37b 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -20,6 +20,9 @@ const ( PresetUnused = "unused" // Related to the detection of unused code. ) +// LastLinter nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives. +const LastLinter = "nolintlint" + type Deprecation struct { Since string Message string diff --git a/pkg/lint/lintersdb/enabled_set.go b/pkg/lint/lintersdb/enabled_set.go index eced95f6552d..9814aa857ea9 100644 --- a/pkg/lint/lintersdb/enabled_set.go +++ b/pkg/lint/lintersdb/enabled_set.go @@ -111,6 +111,15 @@ func (es EnabledSet) GetOptimizedLinters() ([]*linter.Config, error) { // Make order of execution of linters (go/analysis metalinter and unused) stable. sort.Slice(resultLinters, func(i, j int) bool { a, b := resultLinters[i], resultLinters[j] + + if b.Name() == linter.LastLinter { + return true + } + + if a.Name() == linter.LastLinter { + return false + } + if a.DoesChangeTypes != b.DoesChangeTypes { return b.DoesChangeTypes // move type-changing linters to the end to optimize speed } @@ -149,8 +158,19 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config) // Make order of execution of go/analysis analyzers stable. sort.Slice(goanalysisLinters, func(i, j int) bool { - return strings.Compare(goanalysisLinters[i].Name(), goanalysisLinters[j].Name()) <= 0 + a, b := goanalysisLinters[i], goanalysisLinters[j] + + if b.Name() == linter.LastLinter { + return true + } + + if a.Name() == linter.LastLinter { + return false + } + + return strings.Compare(a.Name(), b.Name()) <= 0 }) + ml := goanalysis.NewMetaLinter(goanalysisLinters) var presets []string