From 7c680e97d050569cbdbecd3634d711a1cd6cf4cf Mon Sep 17 00:00:00 2001 From: sylvia7788 <1227977886@qq.com> Date: Fri, 12 Aug 2022 12:04:37 +0800 Subject: [PATCH 1/4] contextcheck: re-enable for go1.18 --- go.mod | 2 +- go.sum | 4 ++-- pkg/config/linters_settings.go | 5 +++++ pkg/golinters/contextcheck.go | 16 +++++++++++++--- pkg/lint/lintersdb/manager.go | 7 ++++--- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 4069dee67f7b..1b8eac8bc2b0 100644 --- a/go.mod +++ b/go.mod @@ -90,7 +90,7 @@ require ( github.com/ssgreg/nlreturn/v2 v2.2.1 github.com/stbenjam/no-sprintf-host-port v0.1.1 github.com/stretchr/testify v1.8.0 - github.com/sylvia7788/contextcheck v1.0.4 + github.com/sylvia7788/contextcheck v1.0.6 github.com/tdakkota/asciicheck v0.1.1 github.com/tetafro/godot v1.4.11 github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 diff --git a/go.sum b/go.sum index d615d7da055f..e58a81517d45 100644 --- a/go.sum +++ b/go.sum @@ -518,8 +518,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs= github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo= -github.com/sylvia7788/contextcheck v1.0.4 h1:MsiVqROAdr0efZc/fOCt0c235qm9XJqHtWwM+2h2B04= -github.com/sylvia7788/contextcheck v1.0.4/go.mod h1:vuPKJMQ7MQ91ZTqfdyreNKwZjyUg6KO+IebVyQDedZQ= +github.com/sylvia7788/contextcheck v1.0.6 h1:o2EZgVPyMKE/Mtoqym61DInKEjwEbsmyoxg3VrmjNO4= +github.com/sylvia7788/contextcheck v1.0.6/go.mod h1:9XDxwvxyuKD+8N+a7Gs7bfWLityh5t70g/GjdEt2N2M= github.com/tdakkota/asciicheck v0.1.1 h1:PKzG7JUTUmVspQTDqtkX9eSiLGossXTybutHwTXuO0A= github.com/tdakkota/asciicheck v0.1.1/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM= github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 242a1de1f3d8..d3cebe1f93ab 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -185,6 +185,7 @@ type LintersSettings struct { Whitespace WhitespaceSettings Wrapcheck WrapcheckSettings WSL WSLSettings + ContextCheck ContextCheckSettings Custom map[string]CustomLinterSettings } @@ -661,6 +662,10 @@ type WSLSettings struct { ForceCaseTrailingWhitespaceLimit int `mapstructure:"force-case-trailing-whitespace"` } +type ContextCheckSettings struct { + DisableFact bool `mapstructure:"disable-fact"` +} + // CustomLinterSettings encapsulates the meta-data of a private linter. // For example, a private linter may be added to the golangci config file as shown below. // diff --git a/pkg/golinters/contextcheck.go b/pkg/golinters/contextcheck.go index 38ede810dec9..3254935712e9 100644 --- a/pkg/golinters/contextcheck.go +++ b/pkg/golinters/contextcheck.go @@ -4,14 +4,24 @@ import ( "github.com/sylvia7788/contextcheck" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" + "github.com/golangci/golangci-lint/pkg/lint/linter" ) -func NewContextCheck() *goanalysis.Linter { +func NewContextCheck(settings *config.ContextCheckSettings) *goanalysis.Linter { + conf := contextcheck.Configuration{} + if settings != nil { + conf.DisableFact = settings.DisableFact + } + analyzer := contextcheck.NewAnalyzer(conf) return goanalysis.NewLinter( "contextcheck", "check the function whether use a non-inherited context", - []*analysis.Analyzer{contextcheck.NewAnalyzer()}, + []*analysis.Analyzer{analyzer}, nil, - ).WithLoadMode(goanalysis.LoadModeTypesInfo) + ).WithLoadMode(goanalysis.LoadModeTypesInfo). + WithContextSetter(func(lintCtx *linter.Context) { + analyzer.Run = contextcheck.NewRun(lintCtx.Packages, conf.DisableFact) + }) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 9a1f1fa4d5f0..826e6996ee02 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -171,6 +171,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { whitespaceCfg *config.WhitespaceSettings wrapcheckCfg *config.WrapcheckSettings wslCfg *config.WSLSettings + contextcheckCfg *config.ContextCheckSettings ) if m.cfg != nil { @@ -243,6 +244,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { whitespaceCfg = &m.cfg.LintersSettings.Whitespace wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck wslCfg = &m.cfg.LintersSettings.WSL + contextcheckCfg = &m.cfg.LintersSettings.ContextCheck if govetCfg != nil { govetCfg.Go = m.cfg.Run.Go @@ -298,12 +300,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/sivchari/containedctx"), - linter.NewConfig(golinters.NewContextCheck()). + linter.NewConfig(golinters.NewContextCheck(contextcheckCfg)). WithSince("v1.43.0"). WithPresets(linter.PresetBugs). WithLoadForGoAnalysis(). - WithURL("https://github.com/sylvia7788/contextcheck"). - WithNoopFallback(m.cfg), + WithURL("https://github.com/sylvia7788/contextcheck"), linter.NewConfig(golinters.NewCyclop(cyclopCfg)). WithSince("v1.37.0"). From b6b99b53f08b4f1f93b7d05cd980b6aae7743c60 Mon Sep 17 00:00:00 2001 From: sylvia7788 <1227977886@qq.com> Date: Wed, 17 Aug 2022 11:36:54 +0800 Subject: [PATCH 2/4] docs(contextcheck): add `disable-fact` config --- .golangci.reference.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index e02897245170..0f72623d3bc1 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1854,6 +1854,11 @@ linters-settings: # Default: true strict-append: true + contextcheck: + # If you use contextcheck in docker ci, and feel a little bit slow, you can turn on this option. + # Default: false + disable-fact: true + # The custom section can be used to define linter plugins to be loaded at runtime. # See README documentation for more info. custom: @@ -1974,6 +1979,7 @@ linters: - whitespace - wrapcheck - wsl + - contextcheck # Enable all available linters. # Default: false @@ -2078,6 +2084,7 @@ linters: - whitespace - wrapcheck - wsl + - contextcheck # Enable presets. # https://golangci-lint.run/usage/linters From 94a9c43d4486cf432e793ba39037a3b56d9ea2be Mon Sep 17 00:00:00 2001 From: sylvia7788 <1227977886@qq.com> Date: Sat, 20 Aug 2022 14:19:00 +0800 Subject: [PATCH 3/4] contextcheck: remove `disable-fact` config --- .golangci.reference.yml | 7 ------- pkg/config/linters_settings.go | 5 ----- pkg/golinters/contextcheck.go | 6 +----- pkg/lint/lintersdb/manager.go | 4 +--- 4 files changed, 2 insertions(+), 20 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 0f72623d3bc1..e02897245170 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1854,11 +1854,6 @@ linters-settings: # Default: true strict-append: true - contextcheck: - # If you use contextcheck in docker ci, and feel a little bit slow, you can turn on this option. - # Default: false - disable-fact: true - # The custom section can be used to define linter plugins to be loaded at runtime. # See README documentation for more info. custom: @@ -1979,7 +1974,6 @@ linters: - whitespace - wrapcheck - wsl - - contextcheck # Enable all available linters. # Default: false @@ -2084,7 +2078,6 @@ linters: - whitespace - wrapcheck - wsl - - contextcheck # Enable presets. # https://golangci-lint.run/usage/linters diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index d3cebe1f93ab..242a1de1f3d8 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -185,7 +185,6 @@ type LintersSettings struct { Whitespace WhitespaceSettings Wrapcheck WrapcheckSettings WSL WSLSettings - ContextCheck ContextCheckSettings Custom map[string]CustomLinterSettings } @@ -662,10 +661,6 @@ type WSLSettings struct { ForceCaseTrailingWhitespaceLimit int `mapstructure:"force-case-trailing-whitespace"` } -type ContextCheckSettings struct { - DisableFact bool `mapstructure:"disable-fact"` -} - // CustomLinterSettings encapsulates the meta-data of a private linter. // For example, a private linter may be added to the golangci config file as shown below. // diff --git a/pkg/golinters/contextcheck.go b/pkg/golinters/contextcheck.go index 3254935712e9..bfc55d250d2a 100644 --- a/pkg/golinters/contextcheck.go +++ b/pkg/golinters/contextcheck.go @@ -4,16 +4,12 @@ import ( "github.com/sylvia7788/contextcheck" "golang.org/x/tools/go/analysis" - "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/lint/linter" ) -func NewContextCheck(settings *config.ContextCheckSettings) *goanalysis.Linter { +func NewContextCheck() *goanalysis.Linter { conf := contextcheck.Configuration{} - if settings != nil { - conf.DisableFact = settings.DisableFact - } analyzer := contextcheck.NewAnalyzer(conf) return goanalysis.NewLinter( "contextcheck", diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 826e6996ee02..7086427af8ce 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -171,7 +171,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { whitespaceCfg *config.WhitespaceSettings wrapcheckCfg *config.WrapcheckSettings wslCfg *config.WSLSettings - contextcheckCfg *config.ContextCheckSettings ) if m.cfg != nil { @@ -244,7 +243,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { whitespaceCfg = &m.cfg.LintersSettings.Whitespace wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck wslCfg = &m.cfg.LintersSettings.WSL - contextcheckCfg = &m.cfg.LintersSettings.ContextCheck if govetCfg != nil { govetCfg.Go = m.cfg.Run.Go @@ -300,7 +298,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/sivchari/containedctx"), - linter.NewConfig(golinters.NewContextCheck(contextcheckCfg)). + linter.NewConfig(golinters.NewContextCheck()). WithSince("v1.43.0"). WithPresets(linter.PresetBugs). WithLoadForGoAnalysis(). From 85767462e45030da0e2cf13bbf63e6fe72ab544b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 23 Aug 2022 13:22:58 +0200 Subject: [PATCH 4/4] review --- pkg/golinters/contextcheck.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/golinters/contextcheck.go b/pkg/golinters/contextcheck.go index bfc55d250d2a..9ec35796b282 100644 --- a/pkg/golinters/contextcheck.go +++ b/pkg/golinters/contextcheck.go @@ -9,15 +9,14 @@ import ( ) func NewContextCheck() *goanalysis.Linter { - conf := contextcheck.Configuration{} - analyzer := contextcheck.NewAnalyzer(conf) + analyzer := contextcheck.NewAnalyzer(contextcheck.Configuration{}) + return goanalysis.NewLinter( - "contextcheck", - "check the function whether use a non-inherited context", + analyzer.Name, + analyzer.Doc, []*analysis.Analyzer{analyzer}, nil, - ).WithLoadMode(goanalysis.LoadModeTypesInfo). - WithContextSetter(func(lintCtx *linter.Context) { - analyzer.Run = contextcheck.NewRun(lintCtx.Packages, conf.DisableFact) - }) + ).WithContextSetter(func(lintCtx *linter.Context) { + analyzer.Run = contextcheck.NewRun(lintCtx.Packages, false) + }).WithLoadMode(goanalysis.LoadModeTypesInfo) }