From 24b51ea825ec02b1353d8c36c1bc364e9d5d9313 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 26 Dec 2024 17:23:44 +0100 Subject: [PATCH] dev: use bool pointer on deprecated bool options (#5254) --- pkg/commands/flagsets.go | 2 -- pkg/config/issues.go | 2 +- pkg/config/linters_settings.go | 24 +++++++++------ pkg/config/loader.go | 31 ++++++++++---------- pkg/config/run.go | 4 +-- pkg/golinters/godot/godot.go | 2 +- pkg/golinters/govet/govet.go | 2 +- pkg/golinters/usestdlibvars/usestdlibvars.go | 4 +-- 8 files changed, 37 insertions(+), 34 deletions(-) diff --git a/pkg/commands/flagsets.go b/pkg/commands/flagsets.go index 4a9f0e970380..9f17018b2fcc 100644 --- a/pkg/commands/flagsets.go +++ b/pkg/commands/flagsets.go @@ -56,8 +56,6 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) { internal.AddDeprecatedHackedStringSlice(fs, "skip-files", color.GreenString("Regexps of files to skip")) internal.AddDeprecatedHackedStringSlice(fs, "skip-dirs", color.GreenString("Regexps of directories to skip")) - internal.AddDeprecatedFlagAndBind(v, fs, fs.Bool, "skip-dirs-use-default", "run.skip-dirs-use-default", true, - formatList("Use or not use default excluded directories:", processors.StdExcludeDirRegexps)) const allowParallelDesc = "Allow multiple parallel golangci-lint instances running.\n" + "If false (default) - golangci-lint acquires file lock on start." diff --git a/pkg/config/issues.go b/pkg/config/issues.go index 49f4dcf88c67..081b87624d1f 100644 --- a/pkg/config/issues.go +++ b/pkg/config/issues.go @@ -128,7 +128,7 @@ type Issues struct { NeedFix bool `mapstructure:"fix"` - ExcludeGeneratedStrict bool `mapstructure:"exclude-generated-strict"` // Deprecated: use ExcludeGenerated instead. + ExcludeGeneratedStrict *bool `mapstructure:"exclude-generated-strict"` // Deprecated: use ExcludeGenerated instead. } func (i *Issues) Validate() error { diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index a5cde6969193..2a0068000174 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -169,7 +169,6 @@ var defaultLintersSettings = LintersSettings{ Unused: UnusedSettings{ FieldWritesAreUses: true, PostStatementsAreReads: false, - ExportedIsUsed: true, ExportedFieldsAreUsed: true, ParametersAreUsed: true, LocalVariablesAreUsed: true, @@ -329,8 +328,10 @@ type BiDiChkSettings struct { } type CopyLoopVarSettings struct { - IgnoreAlias bool `mapstructure:"ignore-alias"` // Deprecated: use CheckAlias - CheckAlias bool `mapstructure:"check-alias"` + CheckAlias bool `mapstructure:"check-alias"` + + // Deprecated: use CheckAlias + IgnoreAlias *bool `mapstructure:"ignore-alias"` } type Cyclop struct { @@ -548,7 +549,7 @@ type GodotSettings struct { Period bool `mapstructure:"period"` // Deprecated: use Scope instead - CheckAll bool `mapstructure:"check-all"` + CheckAll *bool `mapstructure:"check-all"` } type GodoxSettings struct { @@ -641,7 +642,7 @@ type GovetSettings struct { Settings map[string]map[string]any // Deprecated: the linter should be enabled inside Enable. - CheckShadowing bool `mapstructure:"check-shadowing"` + CheckShadowing *bool `mapstructure:"check-shadowing"` } func (cfg *GovetSettings) Validate() error { @@ -857,7 +858,7 @@ type SlogLintSettings struct { ArgsOnSepLines bool `mapstructure:"args-on-sep-lines"` // Deprecated: use Context instead. - ContextOnly bool `mapstructure:"context-only"` + ContextOnly *bool `mapstructure:"context-only"` } type SpancheckSettings struct { @@ -977,11 +978,14 @@ type UseStdlibVarsSettings struct { TimeLayout bool `mapstructure:"time-layout"` CryptoHash bool `mapstructure:"crypto-hash"` DefaultRPCPath bool `mapstructure:"default-rpc-path"` - OSDevNull bool `mapstructure:"os-dev-null"` // Deprecated SQLIsolationLevel bool `mapstructure:"sql-isolation-level"` TLSSignatureScheme bool `mapstructure:"tls-signature-scheme"` ConstantKind bool `mapstructure:"constant-kind"` - SyslogPriority bool `mapstructure:"syslog-priority"` // Deprecated + + // Deprecated + OSDevNull *bool `mapstructure:"os-dev-null"` + // Deprecated + SyslogPriority *bool `mapstructure:"syslog-priority"` } type UseTestingSettings struct { @@ -1007,11 +1011,13 @@ type UnparamSettings struct { type UnusedSettings struct { FieldWritesAreUses bool `mapstructure:"field-writes-are-uses"` PostStatementsAreReads bool `mapstructure:"post-statements-are-reads"` - ExportedIsUsed bool `mapstructure:"exported-is-used"` // Deprecated ExportedFieldsAreUsed bool `mapstructure:"exported-fields-are-used"` ParametersAreUsed bool `mapstructure:"parameters-are-used"` LocalVariablesAreUsed bool `mapstructure:"local-variables-are-used"` GeneratedIsUsed bool `mapstructure:"generated-is-used"` + + // Deprecated + ExportedIsUsed *bool `mapstructure:"exported-is-used"` } type VarnamelenSettings struct { diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 0c90ad41d57a..56f57d9d5dd7 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -304,7 +304,6 @@ func (l *Loader) handleGoVersion() { os.Setenv("GOSECGOVERSION", l.cfg.Run.Go) } -//nolint:gocyclo // The complexity is expected by the cases to handle. func (l *Loader) handleDeprecation() error { if l.cfg.InternalTest || l.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" { return nil @@ -322,19 +321,17 @@ func (l *Loader) handleDeprecation() error { l.cfg.Issues.ExcludeDirs = l.cfg.Run.SkipDirs } - // The 2 options are true by default. // Deprecated since v1.57.0 - if !l.cfg.Run.UseDefaultSkipDirs { + if l.cfg.Run.UseDefaultSkipDirs != nil { l.log.Warnf("The configuration option `run.skip-dirs-use-default` is deprecated, please use `issues.exclude-dirs-use-default`.") + l.cfg.Issues.UseDefaultExcludeDirs = *l.cfg.Run.UseDefaultSkipDirs } - l.cfg.Issues.UseDefaultExcludeDirs = l.cfg.Run.UseDefaultSkipDirs && l.cfg.Issues.UseDefaultExcludeDirs - // The 2 options are false by default. // Deprecated since v1.57.0 - if l.cfg.Run.ShowStats { + if l.cfg.Run.ShowStats != nil { l.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`") + l.cfg.Output.ShowStats = *l.cfg.Run.ShowStats } - l.cfg.Output.ShowStats = l.cfg.Run.ShowStats || l.cfg.Output.ShowStats // Deprecated since v1.63.0 if l.cfg.Output.UniqByLine != nil { @@ -363,9 +360,11 @@ func (l *Loader) handleDeprecation() error { } // Deprecated since v1.59.0 - if l.cfg.Issues.ExcludeGeneratedStrict { + if l.cfg.Issues.ExcludeGeneratedStrict != nil { l.log.Warnf("The configuration option `issues.exclude-generated-strict` is deprecated, please use `issues.exclude-generated`") - l.cfg.Issues.ExcludeGenerated = "strict" // Don't use the constants to avoid cyclic dependencies. + if !*l.cfg.Issues.ExcludeGeneratedStrict { + l.cfg.Issues.ExcludeGenerated = "strict" // Don't use the constants to avoid cyclic dependencies. + } } l.handleLinterOptionDeprecations() @@ -376,12 +375,12 @@ func (l *Loader) handleDeprecation() error { func (l *Loader) handleLinterOptionDeprecations() { // Deprecated since v1.57.0, // but it was unofficially deprecated since v1.19 (2019) (https://github.com/golangci/golangci-lint/pull/697). - if l.cfg.LintersSettings.Govet.CheckShadowing { + if l.cfg.LintersSettings.Govet.CheckShadowing != nil { l.log.Warnf("The configuration option `linters.govet.check-shadowing` is deprecated. " + "Please enable `shadow` instead, if you are not using `enable-all`.") } - if l.cfg.LintersSettings.CopyLoopVar.IgnoreAlias { + if l.cfg.LintersSettings.CopyLoopVar.IgnoreAlias != nil { l.log.Warnf("The configuration option `linters.copyloopvar.ignore-alias` is deprecated and ignored," + "please use `linters.copyloopvar.check-alias`.") } @@ -403,7 +402,7 @@ func (l *Loader) handleLinterOptionDeprecations() { } // Deprecated since v1.33.0. - if l.cfg.LintersSettings.Godot.CheckAll { + if l.cfg.LintersSettings.Godot.CheckAll != nil { l.log.Warnf("The configuration option `linters.godot.check-all` is deprecated, please use `linters.godot.scope: all`.") } @@ -428,23 +427,23 @@ func (l *Loader) handleLinterOptionDeprecations() { } // Deprecated since v1.60.0 - if !l.cfg.LintersSettings.Unused.ExportedIsUsed { + if l.cfg.LintersSettings.Unused.ExportedIsUsed != nil { l.log.Warnf("The configuration option `linters.unused.exported-is-used` is deprecated.") } // Deprecated since v1.58.0 - if l.cfg.LintersSettings.SlogLint.ContextOnly { + if l.cfg.LintersSettings.SlogLint.ContextOnly != nil { l.log.Warnf("The configuration option `linters.sloglint.context-only` is deprecated, please use `linters.sloglint.context`.") l.cfg.LintersSettings.SlogLint.Context = cmp.Or(l.cfg.LintersSettings.SlogLint.Context, "all") } // Deprecated since v1.51.0 - if l.cfg.LintersSettings.UseStdlibVars.OSDevNull { + if l.cfg.LintersSettings.UseStdlibVars.OSDevNull != nil { l.log.Warnf("The configuration option `linters.usestdlibvars.os-dev-null` is deprecated.") } // Deprecated since v1.51.0 - if l.cfg.LintersSettings.UseStdlibVars.SyslogPriority { + if l.cfg.LintersSettings.UseStdlibVars.SyslogPriority != nil { l.log.Warnf("The configuration option `linters.usestdlibvars.syslog-priority` is deprecated.") } } diff --git a/pkg/config/run.go b/pkg/config/run.go index 2f6523c0b964..784e8c2fad94 100644 --- a/pkg/config/run.go +++ b/pkg/config/run.go @@ -29,10 +29,10 @@ type Run struct { // Deprecated: use Issues.ExcludeDirs instead. SkipDirs []string `mapstructure:"skip-dirs"` // Deprecated: use Issues.UseDefaultExcludeDirs instead. - UseDefaultSkipDirs bool `mapstructure:"skip-dirs-use-default"` + UseDefaultSkipDirs *bool `mapstructure:"skip-dirs-use-default"` // Deprecated: use Output.ShowStats instead. - ShowStats bool `mapstructure:"show-stats"` + ShowStats *bool `mapstructure:"show-stats"` } func (r *Run) Validate() error { diff --git a/pkg/golinters/godot/godot.go b/pkg/golinters/godot/godot.go index 2ec7bdab67d7..3194b3d3ac13 100644 --- a/pkg/golinters/godot/godot.go +++ b/pkg/golinters/godot/godot.go @@ -24,7 +24,7 @@ func New(settings *config.GodotSettings) *goanalysis.Linter { } // Convert deprecated setting - if settings.CheckAll { + if settings.CheckAll != nil && *settings.CheckAll { dotSettings.Scope = godot.AllScope } } diff --git a/pkg/golinters/govet/govet.go b/pkg/golinters/govet/govet.go index 00ecc6f9e916..73bf63af73d9 100644 --- a/pkg/golinters/govet/govet.go +++ b/pkg/golinters/govet/govet.go @@ -190,7 +190,7 @@ func isAnalyzerEnabled(name string, cfg *config.GovetSettings, defaultAnalyzers } // Keeping for backward compatibility. - if cfg.CheckShadowing && name == shadow.Analyzer.Name { + if cfg.CheckShadowing != nil && *cfg.CheckShadowing && name == shadow.Analyzer.Name { return true } diff --git a/pkg/golinters/usestdlibvars/usestdlibvars.go b/pkg/golinters/usestdlibvars/usestdlibvars.go index 90ed869486cd..00f7d9742a56 100644 --- a/pkg/golinters/usestdlibvars/usestdlibvars.go +++ b/pkg/golinters/usestdlibvars/usestdlibvars.go @@ -18,10 +18,10 @@ func New(settings *config.UseStdlibVarsSettings) *goanalysis.Linter { analyzer.CryptoHashFlag: settings.CryptoHash, analyzer.HTTPMethodFlag: settings.HTTPMethod, analyzer.HTTPStatusCodeFlag: settings.HTTPStatusCode, - analyzer.OSDevNullFlag: settings.OSDevNull, + analyzer.OSDevNullFlag: settings.OSDevNull != nil && *settings.OSDevNull, analyzer.RPCDefaultPathFlag: settings.DefaultRPCPath, analyzer.SQLIsolationLevelFlag: settings.SQLIsolationLevel, - analyzer.SyslogPriorityFlag: settings.SyslogPriority, + analyzer.SyslogPriorityFlag: settings.SyslogPriority != nil && *settings.SyslogPriority, analyzer.TimeLayoutFlag: settings.TimeLayout, analyzer.TimeMonthFlag: settings.TimeMonth, analyzer.TimeWeekdayFlag: settings.TimeWeekday,