From 79222096916e2e3a71ac396cf695a29c9d026df7 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 11 Mar 2025 17:15:20 +0100 Subject: [PATCH] dev: remove IdentifierMarker --- pkg/golinters/unparam/testdata/unparam.go | 2 +- pkg/golinters/unparam/testdata/unparam_cgo.go | 2 +- pkg/golinters/unused/testdata/unused.go | 12 +- pkg/golinters/unused/testdata/unused_cgo.go | 2 +- pkg/lint/runner.go | 3 - pkg/result/processors/identifier_marker.go | 145 ------------------ .../processors/identifier_marker_test.go | 139 ----------------- test/run_test.go | 2 +- 8 files changed, 10 insertions(+), 297 deletions(-) delete mode 100644 pkg/result/processors/identifier_marker.go delete mode 100644 pkg/result/processors/identifier_marker_test.go diff --git a/pkg/golinters/unparam/testdata/unparam.go b/pkg/golinters/unparam/testdata/unparam.go index 18eb8e2b39e6..ac62ee65cc18 100644 --- a/pkg/golinters/unparam/testdata/unparam.go +++ b/pkg/golinters/unparam/testdata/unparam.go @@ -1,7 +1,7 @@ //golangcitest:args -Eunparam package testdata -func unparamUnused(a, b uint) uint { // want "`unparamUnused` - `b` is unused" +func unparamUnused(a, b uint) uint { // want "unparamUnused - b is unused" a++ return a } diff --git a/pkg/golinters/unparam/testdata/unparam_cgo.go b/pkg/golinters/unparam/testdata/unparam_cgo.go index 5cc81e0958b5..ebf8d092f047 100644 --- a/pkg/golinters/unparam/testdata/unparam_cgo.go +++ b/pkg/golinters/unparam/testdata/unparam_cgo.go @@ -25,7 +25,7 @@ func _() { C.free(unsafe.Pointer(cs)) } -func unparamUnusedCGO(a, b uint) uint { // want "`unparamUnusedCGO` - `b` is unused" +func unparamUnusedCGO(a, b uint) uint { // want "unparamUnusedCGO - b is unused" a++ return a } diff --git a/pkg/golinters/unused/testdata/unused.go b/pkg/golinters/unused/testdata/unused.go index d8b78d1a3109..5b4288c0c3cd 100644 --- a/pkg/golinters/unused/testdata/unused.go +++ b/pkg/golinters/unused/testdata/unused.go @@ -1,19 +1,19 @@ //golangcitest:args -Eunused package testdata -func fn1() {} // want "func `fn1` is unused" +func fn1() {} // want "func fn1 is unused" //nolint:unused func fn2() { fn3() } -func fn3() {} // want "func `fn3` is unused" +func fn3() {} // want "func fn3 is unused" -func fn4() { fn5() } // want "func `fn4` is unused" +func fn4() { fn5() } // want "func fn4 is unused" -func fn5() {} // want "func `fn5` is unused" +func fn5() {} // want "func fn5 is unused" -func fn6() { fn4() } // want "func `fn6` is unused" +func fn6() { fn4() } // want "func fn6 is unused" -type unusedStruct struct{} // want "type `unusedStruct` is unused" +type unusedStruct struct{} // want "type unusedStruct is unused" type unusedStructNolintUnused struct{} //nolint:unused diff --git a/pkg/golinters/unused/testdata/unused_cgo.go b/pkg/golinters/unused/testdata/unused_cgo.go index 6a8becf4d895..83f2b60967a9 100644 --- a/pkg/golinters/unused/testdata/unused_cgo.go +++ b/pkg/golinters/unused/testdata/unused_cgo.go @@ -25,4 +25,4 @@ func _() { C.free(unsafe.Pointer(cs)) } -func fn1() {} // want "func `fn1` is unused" +func fn1() {} // want "func fn1 is unused" diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index a0928a41ae9d..516fe8b3af9a 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -90,9 +90,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env, processors.NewGeneratedFileFilter(cfg.Linters.Exclusions.Generated), - // Must be before exclude because users see already marked output and configure excluding by it. - processors.NewIdentifierMarker(), - processors.NewExclusionRules(log.Child(logutils.DebugKeyExclusionRules), lineCache, &cfg.Linters.Exclusions), diff --git a/pkg/result/processors/identifier_marker.go b/pkg/result/processors/identifier_marker.go deleted file mode 100644 index 9f332705e1f1..000000000000 --- a/pkg/result/processors/identifier_marker.go +++ /dev/null @@ -1,145 +0,0 @@ -package processors - -import ( - "regexp" - - "github.com/golangci/golangci-lint/pkg/result" -) - -var _ Processor = (*IdentifierMarker)(nil) - -type replacePattern struct { - exp *regexp.Regexp - repl string -} - -// IdentifierMarker modifies report text. -// It must be before [Exclude] and [ExcludeRules]: -// users configure exclusions based on the modified text. -type IdentifierMarker struct { - patterns map[string][]replacePattern -} - -func NewIdentifierMarker() *IdentifierMarker { - return &IdentifierMarker{ - patterns: map[string][]replacePattern{ - "unparam": { - { - exp: regexp.MustCompile(`^(\S+) - (\S+) is unused$`), - repl: "`${1}` - `${2}` is unused", - }, - { - exp: regexp.MustCompile(`^(\S+) - (\S+) always receives (\S+) \((.*)\)$`), - repl: "`${1}` - `${2}` always receives `${3}` (`${4}`)", - }, - { - exp: regexp.MustCompile(`^(\S+) - (\S+) always receives (.*)$`), - repl: "`${1}` - `${2}` always receives `${3}`", - }, - { - exp: regexp.MustCompile(`^(\S+) - result (\S+) is always (\S+)`), - repl: "`${1}` - result `${2}` is always `${3}`", - }, - }, - "govet": { - { - // printf - exp: regexp.MustCompile(`^printf: (\S+) arg list ends with redundant newline$`), - repl: "printf: `${1}` arg list ends with redundant newline", - }, - }, - "gosec": { - { - exp: regexp.MustCompile(`^TLS InsecureSkipVerify set true.$`), - repl: "TLS `InsecureSkipVerify` set true.", - }, - }, - "gosimple": { - { - // s1011 - exp: regexp.MustCompile(`should replace loop with (.*)$`), - repl: "should replace loop with `${1}`", - }, - { - // s1000 - exp: regexp.MustCompile(`should use a simple channel send/receive instead of select with a single case`), - repl: "should use a simple channel send/receive instead of `select` with a single case", - }, - { - // s1002 - exp: regexp.MustCompile(`should omit comparison to bool constant, can be simplified to (.+)$`), - repl: "should omit comparison to bool constant, can be simplified to `${1}`", - }, - { - // s1023 - exp: regexp.MustCompile(`redundant return statement$`), - repl: "redundant `return` statement", - }, - { - // s1017 - exp: regexp.MustCompile(`should replace this if statement with an unconditional strings.TrimPrefix`), - repl: "should replace this `if` statement with an unconditional `strings.TrimPrefix`", - }, - }, - "staticcheck": { - { - // sa4006 - exp: regexp.MustCompile(`this value of (\S+) is never used$`), - repl: "this value of `${1}` is never used", - }, - { - // s1012 - exp: regexp.MustCompile(`should use time.Since instead of time.Now\(\).Sub$`), - repl: "should use `time.Since` instead of `time.Now().Sub`", - }, - { - // sa5001 - exp: regexp.MustCompile(`should check returned error before deferring response.Close\(\)$`), - repl: "should check returned error before deferring `response.Close()`", - }, - { - // sa4003 - exp: regexp.MustCompile(`no value of type uint is less than 0$`), - repl: "no value of type `uint` is less than `0`", - }, - }, - "unused": { - { - exp: regexp.MustCompile(`(func|const|field|type|var) (\S+) is unused$`), - repl: "${1} `${2}` is unused", - }, - }, - }, - } -} - -func (*IdentifierMarker) Name() string { - return "identifier_marker" -} - -func (p *IdentifierMarker) Process(issues []result.Issue) ([]result.Issue, error) { - return transformIssues(issues, func(issue *result.Issue) *result.Issue { - re, ok := p.patterns[issue.FromLinter] - if !ok { - return issue - } - - newIssue := *issue - newIssue.Text = markIdentifiers(re, newIssue.Text) - - return &newIssue - }), nil -} - -func (*IdentifierMarker) Finish() {} - -func markIdentifiers(re []replacePattern, text string) string { - for _, rr := range re { - rs := rr.exp.ReplaceAllString(text, rr.repl) - if rs != text { - return rs - } - } - - return text -} diff --git a/pkg/result/processors/identifier_marker_test.go b/pkg/result/processors/identifier_marker_test.go deleted file mode 100644 index b03465b22445..000000000000 --- a/pkg/result/processors/identifier_marker_test.go +++ /dev/null @@ -1,139 +0,0 @@ -package processors - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/golangci/golangci-lint/pkg/result" -) - -func TestIdentifierMarker_Process(t *testing.T) { - testCases := []struct { - desc string - linter string - in string - out string - }{ - // unparam - { - linter: "unparam", - in: "foo - bar is unused", - out: "`foo` - `bar` is unused", - }, - { - linter: "unparam", - in: "foo - bar always receives fii (abc)", - out: "`foo` - `bar` always receives `fii` (`abc`)", - }, - { - linter: "unparam", - in: "foo - bar always receives fii", - out: "`foo` - `bar` always receives `fii`", - }, - { - linter: "unparam", - in: "createEntry - result err is always nil", - out: "`createEntry` - result `err` is always `nil`", - }, - - // govet - { - linter: "govet", - in: "printf: foo arg list ends with redundant newline", - out: "printf: `foo` arg list ends with redundant newline", - }, - - // gosec - { - linter: "gosec", - in: "TLS InsecureSkipVerify set true.", - out: "TLS `InsecureSkipVerify` set true.", - }, - - // gosimple - { - linter: "gosimple", - in: "should replace loop with foo", - out: "should replace loop with `foo`", - }, - { - linter: "gosimple", - in: "should use a simple channel send/receive instead of select with a single case", - out: "should use a simple channel send/receive instead of `select` with a single case", - }, - { - linter: "gosimple", - in: "should omit comparison to bool constant, can be simplified to !projectIntegration.Model.Storage", - out: "should omit comparison to bool constant, can be simplified to `!projectIntegration.Model.Storage`", - }, - { - linter: "gosimple", - in: "redundant return statement", - out: "redundant `return` statement", - }, - { - linter: "gosimple", - in: "S1017: should replace this if statement with an unconditional strings.TrimPrefix", - out: "S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix`", - }, - - // staticcheck - { - linter: "staticcheck", - in: "this value of foo is never used", - out: "this value of `foo` is never used", - }, - { - linter: "staticcheck", - in: "should use time.Since instead of time.Now().Sub", - out: "should use `time.Since` instead of `time.Now().Sub`", - }, - { - linter: "staticcheck", - in: "should check returned error before deferring response.Close()", - out: "should check returned error before deferring `response.Close()`", - }, - { - linter: "staticcheck", - in: "no value of type uint is less than 0", - out: "no value of type `uint` is less than `0`", - }, - - // unused - { - linter: "unused", - in: "var testInputs is unused", - out: "var `testInputs` is unused", - }, - - // From a linter without patterns. - { - linter: "foo", - in: "var testInputs is unused", - out: "var testInputs is unused", - }, - - // Non-matching text. - { - linter: "unused", - in: "foo is a foo", - out: "foo is a foo", - }, - } - - p := NewIdentifierMarker() - - for _, test := range testCases { - t.Run(fmt.Sprintf("%s: %s", test.linter, test.in), func(t *testing.T) { - t.Parallel() - - out, err := p.Process([]result.Issue{{FromLinter: test.linter, Text: test.in}}) - require.NoError(t, err) - - assert.Equal(t, []result.Issue{{FromLinter: test.linter, Text: test.out}}, out) - }) - } -} diff --git a/test/run_test.go b/test/run_test.go index 67344d9d82c0..e2e97b7ec64c 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -365,7 +365,7 @@ func TestSortedResults(t *testing.T) { Run(). ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq( "testdata/sort_results/main.go:15:13: Error return value is not checked (errcheck)" + "\n" + - "testdata/sort_results/main.go:12:5: var `db` is unused (unused)" + "\n", + "testdata/sort_results/main.go:12:5: var db is unused (unused)" + "\n", ) }