From 550fdcae66a99719f47abf3187b38c3356b4ffb4 Mon Sep 17 00:00:00 2001 From: serosset Date: Mon, 25 Oct 2021 03:40:07 +0000 Subject: [PATCH 1/9] Add variable for ruleguard config directory --- .golangci.example.yml | 5 ++++- pkg/commands/executor.go | 1 + pkg/commands/linters.go | 1 + pkg/commands/run.go | 2 ++ pkg/config/config.go | 8 +++++++- pkg/config/reader.go | 1 + pkg/config/run.go | 2 +- pkg/golinters/gocritic.go | 16 +++++++++++----- 8 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 836947d5631a..ff3bd46445ab 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -196,7 +196,10 @@ linters-settings: # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` # By default list of stable checks is used. enabled-checks: - - rangeValCopy + - nestingReduce + - unnamedresult + - ruleguard + - truncateCmp # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty disabled-checks: diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 60c44d82f149..0ad37dde6ffc 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -55,6 +55,7 @@ type Executor struct { flock *flock.Flock } +// NewExecutor creates and initializes a new command executor. func NewExecutor(version, commit, date string) *Executor { startedAt := time.Now() e := &Executor{ diff --git a/pkg/commands/linters.go b/pkg/commands/linters.go index 873dab817709..bb096942fd90 100644 --- a/pkg/commands/linters.go +++ b/pkg/commands/linters.go @@ -20,6 +20,7 @@ func (e *Executor) initLinters() { e.initRunConfiguration(e.lintersCmd) } +// executeLinters runs the 'linters' CLI command, which displays the supported linters. func (e *Executor) executeLinters(_ *cobra.Command, args []string) { if len(args) != 0 { e.log.Fatalf("Usage: golangci-lint linters") diff --git a/pkg/commands/run.go b/pkg/commands/run.go index f2ea5415a7fe..fb0fdcb01dff 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -326,6 +326,7 @@ func fixSlicesFlags(fs *pflag.FlagSet) { }) } +// runAnalysis executes the linters that have been enabled in the configuration. func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Issue, error) { e.cfg.Run.Args = args @@ -447,6 +448,7 @@ func (e *Executor) createPrinter() (printers.Printer, error) { return p, nil } +// executeRun executes the 'run' CLI command, which runs the linters. func (e *Executor) executeRun(_ *cobra.Command, args []string) { needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage trackResourcesEndCh := make(chan struct{}) diff --git a/pkg/config/config.go b/pkg/config/config.go index 06fca64b58b2..f41705c8959b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,7 +2,8 @@ package config // Config encapsulates the config data specified in the golangci yaml config file. type Config struct { - Run Run + cfgDir string // The directory containing the golangci config file. + Run Run Output Output @@ -16,6 +17,11 @@ type Config struct { InternalTest bool // Option is used only for testing golangci-lint code, don't use it } +// getConfigDir returns the directory that contains golangci config file. +func (c *Config) GetConfigDir() string { + return c.cfgDir +} + func NewDefault() *Config { return &Config{ LintersSettings: defaultLintersSettings, diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 6e97277daa2a..42e9d3aa0c58 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -75,6 +75,7 @@ func (r *FileReader) parseConfig() error { if err := viper.Unmarshal(r.cfg); err != nil { return fmt.Errorf("can't unmarshal config by viper: %s", err) } + r.cfg.cfgDir = filepath.Dir(usedConfigFile) if err := r.validateConfig(); err != nil { return fmt.Errorf("can't validate config: %s", err) diff --git a/pkg/config/run.go b/pkg/config/run.go index 43a5ff2e37e1..c091ee843381 100644 --- a/pkg/config/run.go +++ b/pkg/config/run.go @@ -12,7 +12,7 @@ type Run struct { Concurrency int PrintResourcesUsage bool `mapstructure:"print-resources-usage"` - Config string + Config string // The path to the golangci config file, as specified with the --config argument. NoConfig bool Args []string diff --git a/pkg/golinters/gocritic.go b/pkg/golinters/gocritic.go index ffb44058c375..9a0a9ae9a364 100644 --- a/pkg/golinters/gocritic.go +++ b/pkg/golinters/gocritic.go @@ -78,7 +78,10 @@ func normalizeCheckerInfoParams(info *gocriticlinter.CheckerInfo) gocriticlinter return ret } -func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string]config.GocriticCheckSettings) error { +func configureCheckerInfo( + lintCtx *linter.Context, + info *gocriticlinter.CheckerInfo, + allParams map[string]config.GocriticCheckSettings) error { params := allParams[strings.ToLower(info.Name)] if params == nil { // no config for this checker return nil @@ -88,7 +91,7 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string for k, p := range params { v, ok := infoParams[k] if ok { - v.Value = normalizeCheckerParamsValue(p) + v.Value = normalizeCheckerParamsValue(lintCtx, p) continue } @@ -117,7 +120,7 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string // then we have to convert value types into the expected value types. // Maybe in the future, this kind of conversion will be done in go-critic itself. //nolint:exhaustive // only 3 types (int, bool, and string) are supported by CheckerParam.Value -func normalizeCheckerParamsValue(p interface{}) interface{} { +func normalizeCheckerParamsValue(lintCtx *linter.Context, p interface{}) interface{} { rv := reflect.ValueOf(p) switch rv.Type().Kind() { case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int: @@ -125,7 +128,10 @@ func normalizeCheckerParamsValue(p interface{}) interface{} { case reflect.Bool: return rv.Bool() case reflect.String: - return rv.String() + v := rv.String() + // Perform variable substitution. + fmt.Printf("Config dir: %s\n", lintCtx.Cfg.GetConfigDir()) + return strings.ReplaceAll(v, "${configDir}", lintCtx.Cfg.GetConfigDir()) default: return p } @@ -141,7 +147,7 @@ func buildEnabledCheckers(lintCtx *linter.Context, linterCtx *gocriticlinter.Con continue } - if err := configureCheckerInfo(info, allParams); err != nil { + if err := configureCheckerInfo(lintCtx, info, allParams); err != nil { return nil, err } From f7ed298356534c937b04f386874237f1ff92fbab Mon Sep 17 00:00:00 2001 From: serosset Date: Mon, 25 Oct 2021 03:57:46 +0000 Subject: [PATCH 2/9] Add variable for ruleguard config directory --- pkg/golinters/gocritic.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/golinters/gocritic.go b/pkg/golinters/gocritic.go index 9a0a9ae9a364..636322246828 100644 --- a/pkg/golinters/gocritic.go +++ b/pkg/golinters/gocritic.go @@ -130,8 +130,10 @@ func normalizeCheckerParamsValue(lintCtx *linter.Context, p interface{}) interfa case reflect.String: v := rv.String() // Perform variable substitution. - fmt.Printf("Config dir: %s\n", lintCtx.Cfg.GetConfigDir()) - return strings.ReplaceAll(v, "${configDir}", lintCtx.Cfg.GetConfigDir()) + if path, err := filepath.Abs(lintCtx.Cfg.GetConfigDir()); err == nil { + v = strings.ReplaceAll(v, "${configDir}", path) + } + return v default: return p } From 1d990d99eb7725db462b2aa21b0e60f7002892b8 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 26 Oct 2021 03:54:18 +0000 Subject: [PATCH 3/9] Add variable for ruleguard config directory --- pkg/golinters/gocritic.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/golinters/gocritic.go b/pkg/golinters/gocritic.go index 636322246828..0c32a8562023 100644 --- a/pkg/golinters/gocritic.go +++ b/pkg/golinters/gocritic.go @@ -128,12 +128,8 @@ func normalizeCheckerParamsValue(lintCtx *linter.Context, p interface{}) interfa case reflect.Bool: return rv.Bool() case reflect.String: - v := rv.String() // Perform variable substitution. - if path, err := filepath.Abs(lintCtx.Cfg.GetConfigDir()); err == nil { - v = strings.ReplaceAll(v, "${configDir}", path) - } - return v + return strings.ReplaceAll(rv.String(), "${configDir}", lintCtx.Cfg.GetConfigDir()) default: return p } From 3f1942ea6d09ba5015477d311e012730cf7eb126 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 26 Oct 2021 03:54:22 +0000 Subject: [PATCH 4/9] Add variable for ruleguard config directory --- pkg/config/reader.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 42e9d3aa0c58..9f368341b292 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -71,11 +71,15 @@ func (r *FileReader) parseConfig() error { r.log.Warnf("Can't pretty print config file path: %s", err) } r.log.Infof("Used config file %s", usedConfigFile) + usedConfigDir := filepath.Dir(usedConfigFile) + if usedConfigDir, err = filepath.Abs(usedConfigDir); err != nil { + return fmt.Errorf("can't get config directory") + } + r.cfg.cfgDir = usedConfigDir if err := viper.Unmarshal(r.cfg); err != nil { return fmt.Errorf("can't unmarshal config by viper: %s", err) } - r.cfg.cfgDir = filepath.Dir(usedConfigFile) if err := r.validateConfig(); err != nil { return fmt.Errorf("can't validate config: %s", err) From 84081e7356548e23f2a1d00f867205dadb8ea330 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 2 Nov 2021 14:44:59 +0000 Subject: [PATCH 5/9] Add unit tests --- test/linters_test.go | 2 +- test/testdata/configs/gocritic.yml | 6 +++++ test/testdata/configs/ruleguard/dup.go | 22 +++++++++++++++++++ .../configs/ruleguard/strings_simplify.go | 17 ++++++++++++++ test/testdata/gocritic.go | 13 +++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/testdata/configs/ruleguard/dup.go create mode 100644 test/testdata/configs/ruleguard/strings_simplify.go diff --git a/test/linters_test.go b/test/linters_test.go index 355370480e91..4431a2882122 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -23,7 +23,7 @@ func runGoErrchk(c *exec.Cmd, defaultExpectedLinter string, files []string, t *t if err != nil { var exitErr *exec.ExitError require.ErrorAs(t, err, &exitErr) - require.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode()) + require.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode(), "Unexpected exit code: %s", string(output)) } fullshort := make([]string, 0, len(files)*2) diff --git a/test/testdata/configs/gocritic.yml b/test/testdata/configs/gocritic.yml index 268f2fb9e9a1..8622a21d08f4 100644 --- a/test/testdata/configs/gocritic.yml +++ b/test/testdata/configs/gocritic.yml @@ -3,6 +3,12 @@ linters-settings: enabled-checks: - rangeValCopy - flagDeref + - ruleguard settings: rangevalcopy: sizethreshold: 2 + ruleguard: + debug: dupSubExpr + failOn: dsl,import + # comma-separated paths to ruleguard files. + rules: '${configDir}/ruleguard/strings_simplify.go,${configDir}/ruleguard/dup.go' \ No newline at end of file diff --git a/test/testdata/configs/ruleguard/dup.go b/test/testdata/configs/ruleguard/dup.go new file mode 100644 index 000000000000..ebb22c70d2b0 --- /dev/null +++ b/test/testdata/configs/ruleguard/dup.go @@ -0,0 +1,22 @@ +package gorules + +import "github.com/quasilyte/go-ruleguard/dsl" + +// Suppose that we want to report the duplicated left and right operands of binary operations. +// +// But if the operand has some side effects, this rule can cause false positives: +// `f() && f()` can make sense (although it's not the best piece of code). +// +// This is where *filters* come to the rescue. + +func dupSubExpr(m dsl.Matcher) { + // All filters are written as a Where() argument. + // In our case, we need to assert that $x is "pure". + // It can be achieved by checking the m["x"] member Pure field. + m.Match(`$x || $x`, + `$x && $x`, + `$x | $x`, + `$x & $x`). + Where(m["x"].Pure). + Report(`suspicious identical LHS and RHS`) +} diff --git a/test/testdata/configs/ruleguard/strings_simplify.go b/test/testdata/configs/ruleguard/strings_simplify.go new file mode 100644 index 000000000000..0ff2ddf6ee90 --- /dev/null +++ b/test/testdata/configs/ruleguard/strings_simplify.go @@ -0,0 +1,17 @@ +package gorules + +import "github.com/quasilyte/go-ruleguard/dsl" + +func stringsSimplify(m dsl.Matcher) { + // Some issues have simple fixes that can be expressed as + // a replacement pattern. Rules can use Suggest() function + // to add a quickfix action for such issues. + m.Match(`strings.Replace($s, $old, $new, -1)`). + Report(`this Replace() call can be simplified`). + Suggest(`strings.ReplaceAll($s, $old, $new)`) + + // Suggest() can be used without Report(). + // It'll print the suggested template to the user. + m.Match(`strings.Count($s1, $s2) == 0`). + Suggest(`!strings.Contains($s1, $s2)`) +} diff --git a/test/testdata/gocritic.go b/test/testdata/gocritic.go index 5e821a4e6553..dc7e7edf2122 100644 --- a/test/testdata/gocritic.go +++ b/test/testdata/gocritic.go @@ -5,6 +5,7 @@ package testdata import ( "flag" "log" + "strings" ) var _ = *flag.Bool("global1", false, "") // ERROR `flagDeref: immediate deref in \*flag.Bool\(.global1., false, ..\) is most likely an error; consider using flag\.BoolVar` @@ -29,3 +30,15 @@ func gocriticRangeValCopySize2(ss []size2) { log.Print(s) } } + +func gocriticStringSimplify() { + s := "Most of the time, travellers worry about their luggage." + s = strings.Replace(s, ",", "", -1) // ERROR "ruleguard: " + log.Print(s) +} + +func gocriticDup(x bool) { + if x && x { // ERROR "ruleguard: " + log.Print("x is true") + } +} From e2a4ab36b96dbc3e12211ebebe0cfbb4a9c7cee3 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 2 Nov 2021 15:07:51 +0000 Subject: [PATCH 6/9] Add unit tests for ruleguard --- go.mod | 1 + go.sum | 2 ++ test/testdata/configs/gocritic.yml | 1 + test/testdata/configs/ruleguard/strings_simplify.go | 2 +- test/testdata/gocritic.go | 4 ++-- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6f6516d02c93..cd2bacb36e01 100644 --- a/go.mod +++ b/go.mod @@ -63,6 +63,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349 github.com/prometheus/procfs v0.6.0 // indirect + github.com/quasilyte/go-ruleguard v0.3.13 // indirect github.com/ryancurrah/gomodguard v1.2.3 github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/sanposhiho/wastedassign/v2 v2.0.6 diff --git a/go.sum b/go.sum index c3667b58ca39..4b7255761148 100644 --- a/go.sum +++ b/go.sum @@ -622,8 +622,10 @@ github.com/quasilyte/go-ruleguard v0.3.1-0.20210203134552-1b5a410e1cc8/go.mod h1 github.com/quasilyte/go-ruleguard v0.3.13 h1:O1G41cq1jUr3cJmqp7vOUT0SokqjzmS9aESWJuIDRaY= github.com/quasilyte/go-ruleguard v0.3.13/go.mod h1:Ul8wwdqR6kBVOCt2dipDBkE+T6vAV/iixkrKuRTN1oQ= github.com/quasilyte/go-ruleguard/dsl v0.3.0/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU= +github.com/quasilyte/go-ruleguard/dsl v0.3.10 h1:4tVlVVcBT+nNWoF+t/zrAMO13sHAqYotX1K12Gc8f8A= github.com/quasilyte/go-ruleguard/dsl v0.3.10/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU= github.com/quasilyte/go-ruleguard/rules v0.0.0-20201231183845-9e62ed36efe1/go.mod h1:7JTjp89EGyU1d6XfBiXihJNG37wB2VRkd125Q1u7Plc= +github.com/quasilyte/go-ruleguard/rules v0.0.0-20210428214800-545e0d2e0bf7 h1:cRLFDAB53r5wIkxYvtQUMnn3+B09uZTAOPmefNfVk5I= github.com/quasilyte/go-ruleguard/rules v0.0.0-20210428214800-545e0d2e0bf7/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50= github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 h1:L8QM9bvf68pVdQ3bCFZMDmnt9yqcMBro1pC7F+IPYMY= github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95/go.mod h1:rlzQ04UMyJXu/aOvhd8qT+hvDrFpiwqp8MRXDY9szc0= diff --git a/test/testdata/configs/gocritic.yml b/test/testdata/configs/gocritic.yml index 8622a21d08f4..200886930cd4 100644 --- a/test/testdata/configs/gocritic.yml +++ b/test/testdata/configs/gocritic.yml @@ -11,4 +11,5 @@ linters-settings: debug: dupSubExpr failOn: dsl,import # comma-separated paths to ruleguard files. + # The ${configDir} is substituted by the directory containing the golangci-lint config file. rules: '${configDir}/ruleguard/strings_simplify.go,${configDir}/ruleguard/dup.go' \ No newline at end of file diff --git a/test/testdata/configs/ruleguard/strings_simplify.go b/test/testdata/configs/ruleguard/strings_simplify.go index 0ff2ddf6ee90..173634f9ff25 100644 --- a/test/testdata/configs/ruleguard/strings_simplify.go +++ b/test/testdata/configs/ruleguard/strings_simplify.go @@ -7,7 +7,7 @@ func stringsSimplify(m dsl.Matcher) { // a replacement pattern. Rules can use Suggest() function // to add a quickfix action for such issues. m.Match(`strings.Replace($s, $old, $new, -1)`). - Report(`this Replace() call can be simplified`). + Report(`this Replace call can be simplified`). Suggest(`strings.ReplaceAll($s, $old, $new)`) // Suggest() can be used without Report(). diff --git a/test/testdata/gocritic.go b/test/testdata/gocritic.go index dc7e7edf2122..1768304d1a7f 100644 --- a/test/testdata/gocritic.go +++ b/test/testdata/gocritic.go @@ -33,12 +33,12 @@ func gocriticRangeValCopySize2(ss []size2) { func gocriticStringSimplify() { s := "Most of the time, travellers worry about their luggage." - s = strings.Replace(s, ",", "", -1) // ERROR "ruleguard: " + s = strings.Replace(s, ",", "", -1) // ERROR "ruleguard: this Replace call can be simplified.*" log.Print(s) } func gocriticDup(x bool) { - if x && x { // ERROR "ruleguard: " + if x && x { // ERROR "ruleguard: suspicious identical LHS and RHS.*" log.Print("x is true") } } From 289238d0ededa871a609e6154ec291650559c5f4 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 2 Nov 2021 15:12:18 +0000 Subject: [PATCH 7/9] Add unit tests for ruleguard --- test/testdata/configs/gocritic.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testdata/configs/gocritic.yml b/test/testdata/configs/gocritic.yml index 200886930cd4..8f0884846dbc 100644 --- a/test/testdata/configs/gocritic.yml +++ b/test/testdata/configs/gocritic.yml @@ -12,4 +12,4 @@ linters-settings: failOn: dsl,import # comma-separated paths to ruleguard files. # The ${configDir} is substituted by the directory containing the golangci-lint config file. - rules: '${configDir}/ruleguard/strings_simplify.go,${configDir}/ruleguard/dup.go' \ No newline at end of file + rules: '${configDir}/ruleguard/strings_simplify.go,${configDir}/ruleguard/dup.go' From 4a96225b7f49edd4f22c79ebde7b8be40d2ed13f Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 2 Nov 2021 17:14:58 +0000 Subject: [PATCH 8/9] Add unit tests for ruleguard --- go.mod | 2 +- go.sum | 1 - test/ruleguard/README.md | 1 + test/{testdata/configs => }/ruleguard/dup.go | 4 ++-- test/{testdata/configs => }/ruleguard/strings_simplify.go | 3 ++- test/testdata/configs/gocritic.yml | 6 +++++- 6 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 test/ruleguard/README.md rename test/{testdata/configs => }/ruleguard/dup.go (92%) rename test/{testdata/configs => }/ruleguard/strings_simplify.go (90%) diff --git a/go.mod b/go.mod index cd2bacb36e01..d6e222bd9990 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349 github.com/prometheus/procfs v0.6.0 // indirect - github.com/quasilyte/go-ruleguard v0.3.13 // indirect + github.com/quasilyte/go-ruleguard/dsl v0.3.10 github.com/ryancurrah/gomodguard v1.2.3 github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/sanposhiho/wastedassign/v2 v2.0.6 diff --git a/go.sum b/go.sum index 4b7255761148..556347d8df1f 100644 --- a/go.sum +++ b/go.sum @@ -625,7 +625,6 @@ github.com/quasilyte/go-ruleguard/dsl v0.3.0/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQP github.com/quasilyte/go-ruleguard/dsl v0.3.10 h1:4tVlVVcBT+nNWoF+t/zrAMO13sHAqYotX1K12Gc8f8A= github.com/quasilyte/go-ruleguard/dsl v0.3.10/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU= github.com/quasilyte/go-ruleguard/rules v0.0.0-20201231183845-9e62ed36efe1/go.mod h1:7JTjp89EGyU1d6XfBiXihJNG37wB2VRkd125Q1u7Plc= -github.com/quasilyte/go-ruleguard/rules v0.0.0-20210428214800-545e0d2e0bf7 h1:cRLFDAB53r5wIkxYvtQUMnn3+B09uZTAOPmefNfVk5I= github.com/quasilyte/go-ruleguard/rules v0.0.0-20210428214800-545e0d2e0bf7/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50= github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 h1:L8QM9bvf68pVdQ3bCFZMDmnt9yqcMBro1pC7F+IPYMY= github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95/go.mod h1:rlzQ04UMyJXu/aOvhd8qT+hvDrFpiwqp8MRXDY9szc0= diff --git a/test/ruleguard/README.md b/test/ruleguard/README.md new file mode 100644 index 000000000000..2e2441698750 --- /dev/null +++ b/test/ruleguard/README.md @@ -0,0 +1 @@ +This directory contains ruleguard files that are used in functional tests. diff --git a/test/testdata/configs/ruleguard/dup.go b/test/ruleguard/dup.go similarity index 92% rename from test/testdata/configs/ruleguard/dup.go rename to test/ruleguard/dup.go index ebb22c70d2b0..9ea35c5b3ab8 100644 --- a/test/testdata/configs/ruleguard/dup.go +++ b/test/ruleguard/dup.go @@ -1,3 +1,4 @@ +// go:build ruleguard package gorules import "github.com/quasilyte/go-ruleguard/dsl" @@ -8,8 +9,7 @@ import "github.com/quasilyte/go-ruleguard/dsl" // `f() && f()` can make sense (although it's not the best piece of code). // // This is where *filters* come to the rescue. - -func dupSubExpr(m dsl.Matcher) { +func DupSubExpr(m dsl.Matcher) { // All filters are written as a Where() argument. // In our case, we need to assert that $x is "pure". // It can be achieved by checking the m["x"] member Pure field. diff --git a/test/testdata/configs/ruleguard/strings_simplify.go b/test/ruleguard/strings_simplify.go similarity index 90% rename from test/testdata/configs/ruleguard/strings_simplify.go rename to test/ruleguard/strings_simplify.go index 173634f9ff25..18942e52e893 100644 --- a/test/testdata/configs/ruleguard/strings_simplify.go +++ b/test/ruleguard/strings_simplify.go @@ -1,8 +1,9 @@ +// go:build ruleguard package gorules import "github.com/quasilyte/go-ruleguard/dsl" -func stringsSimplify(m dsl.Matcher) { +func StringsSimplify(m dsl.Matcher) { // Some issues have simple fixes that can be expressed as // a replacement pattern. Rules can use Suggest() function // to add a quickfix action for such issues. diff --git a/test/testdata/configs/gocritic.yml b/test/testdata/configs/gocritic.yml index 8f0884846dbc..5cdda3736af5 100644 --- a/test/testdata/configs/gocritic.yml +++ b/test/testdata/configs/gocritic.yml @@ -12,4 +12,8 @@ linters-settings: failOn: dsl,import # comma-separated paths to ruleguard files. # The ${configDir} is substituted by the directory containing the golangci-lint config file. - rules: '${configDir}/ruleguard/strings_simplify.go,${configDir}/ruleguard/dup.go' + # Note about the directory structure for functional tests: + # The ruleguard files used in functional tests cannot be under the 'testdata' directory. + # This is because they import the 'github.com/quasilyte/go-ruleguard/dsl' package, + # which needs to be added to go.mod. The testdata directory is ignored by go mod. + rules: '${configDir}/../../ruleguard/strings_simplify.go,${configDir}/../../ruleguard/dup.go' From 79cd0f23c05c6127ff6c32775b514a89ed4451ba Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 2 Nov 2021 17:24:06 +0000 Subject: [PATCH 9/9] Add unit tests for ruleguard, fix package name --- test/ruleguard/dup.go | 2 +- test/ruleguard/strings_simplify.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ruleguard/dup.go b/test/ruleguard/dup.go index 9ea35c5b3ab8..944651b6f041 100644 --- a/test/ruleguard/dup.go +++ b/test/ruleguard/dup.go @@ -1,5 +1,5 @@ // go:build ruleguard -package gorules +package ruleguard import "github.com/quasilyte/go-ruleguard/dsl" diff --git a/test/ruleguard/strings_simplify.go b/test/ruleguard/strings_simplify.go index 18942e52e893..2e73a50b3621 100644 --- a/test/ruleguard/strings_simplify.go +++ b/test/ruleguard/strings_simplify.go @@ -1,5 +1,5 @@ // go:build ruleguard -package gorules +package ruleguard import "github.com/quasilyte/go-ruleguard/dsl"