diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 7edfd7469245..bcf0e5f097dc 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2197,6 +2197,10 @@ linters-settings: - suite-thelper - useless-assert + bool-compare: + # To ignore user defined types (over builtin bool). + # Default: false + ignore-custom-types: true expected-actual: # Regexp for expected variable name. # Default: (^(exp(ected)?|want(ed)?)([A-Z]\w*)?$)|(^(\w*[a-z])?(Exp(ected)?|Want(ed)?)$) diff --git a/go.mod b/go.mod index f2609e1c058c..dc432bf94292 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/Abirdcfly/dupword v0.0.14 github.com/Antonboom/errname v0.1.12 github.com/Antonboom/nilnil v0.1.7 - github.com/Antonboom/testifylint v1.1.3 + github.com/Antonboom/testifylint v1.2.0 github.com/BurntSushi/toml v1.3.2 github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 github.com/GaijinEntertainment/go-exhaustruct/v3 v3.2.0 diff --git a/go.sum b/go.sum index 17377a201386..4d3231e3bb95 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,8 @@ github.com/Antonboom/errname v0.1.12 h1:oh9ak2zUtsLp5oaEd/erjB4GPu9w19NyoIskZClD github.com/Antonboom/errname v0.1.12/go.mod h1:bK7todrzvlaZoQagP1orKzWXv59X/x0W0Io2XT1Ssro= github.com/Antonboom/nilnil v0.1.7 h1:ofgL+BA7vlA1K2wNQOsHzLJ2Pw5B5DpWRLdDAVvvTow= github.com/Antonboom/nilnil v0.1.7/go.mod h1:TP+ScQWVEq0eSIxqU8CbdT5DFWoHp0MbP+KMUO1BKYQ= -github.com/Antonboom/testifylint v1.1.3 h1:JowZ7xlzJzZFRUXE4iqoAkENnMv4xRibAxzgy/vIfQw= -github.com/Antonboom/testifylint v1.1.3/go.mod h1:rkmEqjqVnHDRNsinyN6fPSLnoajzFwsCcguJgwADBkw= +github.com/Antonboom/testifylint v1.2.0 h1:015bxD8zc5iY8QwTp4+RG9I4kIbqwvGX9TrBbb7jGdM= +github.com/Antonboom/testifylint v1.2.0/go.mod h1:rkmEqjqVnHDRNsinyN6fPSLnoajzFwsCcguJgwADBkw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 319f2101d9be..e501908ecac8 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -862,6 +862,10 @@ type TestifylintSettings struct { EnabledCheckers []string `mapstructure:"enable"` DisabledCheckers []string `mapstructure:"disable"` + BoolCompare struct { + IgnoreCustomTypes bool `mapstructure:"ignore-custom-types"` + } `mapstructure:"bool-compare"` + ExpectedActual struct { ExpVarPattern string `mapstructure:"pattern"` } `mapstructure:"expected-actual"` diff --git a/pkg/golinters/testifylint.go b/pkg/golinters/testifylint.go index d9cfc04f6e05..32b194f00f8b 100644 --- a/pkg/golinters/testifylint.go +++ b/pkg/golinters/testifylint.go @@ -16,6 +16,8 @@ func NewTestifylint(settings *config.TestifylintSettings) *goanalysis.Linter { cfg[a.Name] = map[string]any{ "enable-all": settings.EnableAll, "disable-all": settings.DisableAll, + + "bool-compare.ignore-custom-types": settings.BoolCompare.IgnoreCustomTypes, } if len(settings.EnabledCheckers) > 0 { cfg[a.Name]["enable"] = settings.EnabledCheckers diff --git a/test/testdata/configs/testifylint_bool_compare_only.yml b/test/testdata/configs/testifylint_bool_compare_only.yml new file mode 100644 index 000000000000..dcf3c7732c71 --- /dev/null +++ b/test/testdata/configs/testifylint_bool_compare_only.yml @@ -0,0 +1,6 @@ +linters-settings: + testifylint: + disable-all: true + enable: bool-compare + bool-compare: + ignore-custom-types: true diff --git a/test/testdata/configs/testifylint.yml b/test/testdata/configs/testifylint_require_error_only.yml similarity index 100% rename from test/testdata/configs/testifylint.yml rename to test/testdata/configs/testifylint_require_error_only.yml diff --git a/test/testdata/testifylint.go b/test/testdata/testifylint.go index 4ec89c61a69a..3de4bad1985b 100644 --- a/test/testdata/testifylint.go +++ b/test/testdata/testifylint.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/suite" ) +type Bool bool + func TestTestifylint(t *testing.T) { var ( predicate bool @@ -20,6 +22,7 @@ func TestTestifylint(t *testing.T) { ) assert.Equal(t, predicate, true) // want "bool-compare: use assert\\.True" + assert.Equal(t, Bool(predicate), false) // want "bool-compare: use assert\\.False" assert.True(t, resultInt == 1) // want "compares: use assert\\.Equal" assert.Equal(t, len(arr), 0) // want "empty: use assert\\.Empty" assert.Error(t, err, io.EOF) // want "error-is-as: invalid usage of assert\\.Error, use assert\\.ErrorIs instead" diff --git a/test/testdata/testifylint_bool_compare.go b/test/testdata/testifylint_bool_compare.go new file mode 100644 index 000000000000..977e823ca5e8 --- /dev/null +++ b/test/testdata/testifylint_bool_compare.go @@ -0,0 +1,17 @@ +//golangcitest:args -Etestifylint +//golangcitest:config_path testdata/configs/testifylint_bool_compare_only.yml +package testdata + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type Bool bool + +func TestTestifylint(t *testing.T) { + var predicate bool + assert.Equal(t, predicate, true) // want "bool-compare: use assert\\.True" + assert.Equal(t, Bool(predicate), false) +} diff --git a/test/testdata/testifylint_config.go b/test/testdata/testifylint_require_error.go similarity index 95% rename from test/testdata/testifylint_config.go rename to test/testdata/testifylint_require_error.go index 512acbfab304..0ba35e8403e9 100644 --- a/test/testdata/testifylint_config.go +++ b/test/testdata/testifylint_require_error.go @@ -1,5 +1,5 @@ //golangcitest:args -Etestifylint -//golangcitest:config_path testdata/configs/testifylint.yml +//golangcitest:config_path testdata/configs/testifylint_require_error_only.yml package testdata import (