From d2116e44b5ef75c6eda0ff9ff56dd2048ea5c5f7 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 24 Mar 2025 05:21:20 +0100 Subject: [PATCH 1/2] fix: funlen ignore-comments --- pkg/golinters/funlen/funlen.go | 2 +- pkg/golinters/funlen/testdata/funlen.go | 101 +++++++++++------- pkg/golinters/funlen/testdata/funlen_cgo.go | 87 ++++++++++----- .../funlen/testdata/funlen_custom.go | 42 ++++++++ .../{funlen.yml => funlen_custom.yml} | 0 .../funlen/testdata/funlen_ignore_comments.go | 68 ++++++++++++ .../testdata/funlen_ignore_comments.yml | 6 ++ 7 files changed, 243 insertions(+), 63 deletions(-) create mode 100644 pkg/golinters/funlen/testdata/funlen_custom.go rename pkg/golinters/funlen/testdata/{funlen.yml => funlen_custom.yml} (100%) create mode 100644 pkg/golinters/funlen/testdata/funlen_ignore_comments.go create mode 100644 pkg/golinters/funlen/testdata/funlen_ignore_comments.yml diff --git a/pkg/golinters/funlen/funlen.go b/pkg/golinters/funlen/funlen.go index 8fb662c9ed77..541d4fd36041 100644 --- a/pkg/golinters/funlen/funlen.go +++ b/pkg/golinters/funlen/funlen.go @@ -19,7 +19,7 @@ func New(settings *config.FunlenSettings) *goanalysis.Linter { if settings != nil { cfg.lineLimit = settings.Lines cfg.stmtLimit = settings.Statements - cfg.ignoreComments = !settings.IgnoreComments + cfg.ignoreComments = settings.IgnoreComments } a := funlen.NewAnalyzer(cfg.lineLimit, cfg.stmtLimit, cfg.ignoreComments) diff --git a/pkg/golinters/funlen/testdata/funlen.go b/pkg/golinters/funlen/testdata/funlen.go index 37ce5ceb1bcf..2e531b4adc30 100644 --- a/pkg/golinters/funlen/testdata/funlen.go +++ b/pkg/golinters/funlen/testdata/funlen.go @@ -1,42 +1,67 @@ //golangcitest:args -Efunlen -//golangcitest:config_path testdata/funlen.yml package testdata -func TooManyLines() { // want `Function 'TooManyLines' is too long \(22 > 20\)` - t := struct { - A string - B string - C string - D string - E string - F string - G string - H string - I string - }{ - `a`, - `b`, - `c`, - `d`, - `e`, - `f`, - `g`, - `h`, - `i`, - } - _ = t -} - -func TooManyStatements() { // want `Function 'TooManyStatements' has too many statements \(11 > 10\)` - a := 1 - b := a - c := b - d := c - e := d - f := e - g := f - h := g - i := h - j := i - _ = j +// want +1 "Function 'main' is too long" +func main() { + // Comment 1 + // Comment 2 + // Comment 3 + // Comment 4 + // Comment 5 + // Comment 6 + // Comment 7 + // Comment 8 + // Comment 9 + // Comment 10 + // Comment 11 + // Comment 12 + // Comment 13 + // Comment 14 + // Comment 15 + // Comment 16 + // Comment 17 + // Comment 18 + // Comment 19 + // Comment 20 + // Comment 21 + // Comment 22 + // Comment 23 + // Comment 24 + // Comment 25 + // Comment 26 + // Comment 27 + // Comment 28 + // Comment 29 + // Comment 30 + // Comment 31 + // Comment 32 + // Comment 33 + // Comment 34 + // Comment 35 + // Comment 36 + // Comment 37 + // Comment 38 + // Comment 39 + // Comment 40 + // Comment 41 + // Comment 42 + // Comment 43 + // Comment 44 + // Comment 45 + // Comment 46 + // Comment 47 + // Comment 48 + // Comment 49 + // Comment 50 + // Comment 51 + // Comment 52 + // Comment 53 + // Comment 54 + // Comment 55 + // Comment 56 + // Comment 57 + // Comment 58 + // Comment 59 + // Comment 60 + print("Hello, world!") } diff --git a/pkg/golinters/funlen/testdata/funlen_cgo.go b/pkg/golinters/funlen/testdata/funlen_cgo.go index 9bd4247ee057..dfc9ca15e749 100644 --- a/pkg/golinters/funlen/testdata/funlen_cgo.go +++ b/pkg/golinters/funlen/testdata/funlen_cgo.go @@ -1,5 +1,4 @@ //golangcitest:args -Efunlen -//golangcitest:config_path testdata/funlen.yml package testdata /* @@ -22,27 +21,67 @@ func _() { C.free(unsafe.Pointer(cs)) } -func _() { // want `Function '_' is too long \(22 > 20\)` - t := struct { - A string - B string - C string - D string - E string - F string - G string - H string - I string - }{ - `a`, - `b`, - `c`, - `d`, - `e`, - `f`, - `g`, - `h`, - `i`, - } - _ = t +// want +1 "Function 'main' is too long" +func main() { + // Comment 1 + // Comment 2 + // Comment 3 + // Comment 4 + // Comment 5 + // Comment 6 + // Comment 7 + // Comment 8 + // Comment 9 + // Comment 10 + // Comment 11 + // Comment 12 + // Comment 13 + // Comment 14 + // Comment 15 + // Comment 16 + // Comment 17 + // Comment 18 + // Comment 19 + // Comment 20 + // Comment 21 + // Comment 22 + // Comment 23 + // Comment 24 + // Comment 25 + // Comment 26 + // Comment 27 + // Comment 28 + // Comment 29 + // Comment 30 + // Comment 31 + // Comment 32 + // Comment 33 + // Comment 34 + // Comment 35 + // Comment 36 + // Comment 37 + // Comment 38 + // Comment 39 + // Comment 40 + // Comment 41 + // Comment 42 + // Comment 43 + // Comment 44 + // Comment 45 + // Comment 46 + // Comment 47 + // Comment 48 + // Comment 49 + // Comment 50 + // Comment 51 + // Comment 52 + // Comment 53 + // Comment 54 + // Comment 55 + // Comment 56 + // Comment 57 + // Comment 58 + // Comment 59 + // Comment 60 + print("Hello, world!") } diff --git a/pkg/golinters/funlen/testdata/funlen_custom.go b/pkg/golinters/funlen/testdata/funlen_custom.go new file mode 100644 index 000000000000..ed80a30af87c --- /dev/null +++ b/pkg/golinters/funlen/testdata/funlen_custom.go @@ -0,0 +1,42 @@ +//golangcitest:args -Efunlen +//golangcitest:config_path testdata/funlen_custom.yml +package testdata + +func TooManyLines() { // want `Function 'TooManyLines' is too long \(22 > 20\)` + t := struct { + A string + B string + C string + D string + E string + F string + G string + H string + I string + }{ + `a`, + `b`, + `c`, + `d`, + `e`, + `f`, + `g`, + `h`, + `i`, + } + _ = t +} + +func TooManyStatements() { // want `Function 'TooManyStatements' has too many statements \(11 > 10\)` + a := 1 + b := a + c := b + d := c + e := d + f := e + g := f + h := g + i := h + j := i + _ = j +} diff --git a/pkg/golinters/funlen/testdata/funlen.yml b/pkg/golinters/funlen/testdata/funlen_custom.yml similarity index 100% rename from pkg/golinters/funlen/testdata/funlen.yml rename to pkg/golinters/funlen/testdata/funlen_custom.yml diff --git a/pkg/golinters/funlen/testdata/funlen_ignore_comments.go b/pkg/golinters/funlen/testdata/funlen_ignore_comments.go new file mode 100644 index 000000000000..72a58c531252 --- /dev/null +++ b/pkg/golinters/funlen/testdata/funlen_ignore_comments.go @@ -0,0 +1,68 @@ +//golangcitest:args -Efunlen +//golangcitest:config_path testdata/funlen_ignore_comments.yml +//golangcitest:expected_exitcode 0 +package testdata + +func main() { + // Comment 1 + // Comment 2 + // Comment 3 + // Comment 4 + // Comment 5 + // Comment 6 + // Comment 7 + // Comment 8 + // Comment 9 + // Comment 10 + // Comment 11 + // Comment 12 + // Comment 13 + // Comment 14 + // Comment 15 + // Comment 16 + // Comment 17 + // Comment 18 + // Comment 19 + // Comment 20 + // Comment 21 + // Comment 22 + // Comment 23 + // Comment 24 + // Comment 25 + // Comment 26 + // Comment 27 + // Comment 28 + // Comment 29 + // Comment 30 + // Comment 31 + // Comment 32 + // Comment 33 + // Comment 34 + // Comment 35 + // Comment 36 + // Comment 37 + // Comment 38 + // Comment 39 + // Comment 40 + // Comment 41 + // Comment 42 + // Comment 43 + // Comment 44 + // Comment 45 + // Comment 46 + // Comment 47 + // Comment 48 + // Comment 49 + // Comment 50 + // Comment 51 + // Comment 52 + // Comment 53 + // Comment 54 + // Comment 55 + // Comment 56 + // Comment 57 + // Comment 58 + // Comment 59 + // Comment 60 + print("Hello, world!") +} diff --git a/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml b/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml new file mode 100644 index 000000000000..c81941c43d2b --- /dev/null +++ b/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml @@ -0,0 +1,6 @@ +version: "2" + +linters: + settings: + funlen: + ignore-comments: true From 98cf5965f9939b5b11f8dd2543c9b054840e5890 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 24 Mar 2025 05:49:45 +0100 Subject: [PATCH 2/2] fix: invert default for compatibility --- .golangci.next.reference.yml | 4 +- jsonschema/golangci.jsonschema.json | 2 +- jsonschema/golangci.next.jsonschema.json | 2 +- pkg/config/linters_settings.go | 3 + pkg/golinters/funlen/testdata/funlen.go | 125 +++++++++++++++++- pkg/golinters/funlen/testdata/funlen_cgo.go | 125 +++++++++++++++++- .../funlen/testdata/funlen_ignore_comments.go | 4 +- .../testdata/funlen_ignore_comments.yml | 2 +- 8 files changed, 256 insertions(+), 11 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index f82113f68439..ff5b11d64865 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -544,8 +544,8 @@ linters: # Default: 40 statements: -1 # Ignore comments when counting lines. - # Default false - ignore-comments: true + # Default: true + ignore-comments: false ginkgolinter: # Suppress the wrong length assertion warning. diff --git a/jsonschema/golangci.jsonschema.json b/jsonschema/golangci.jsonschema.json index 8bf37394612c..776a489afd98 100644 --- a/jsonschema/golangci.jsonschema.json +++ b/jsonschema/golangci.jsonschema.json @@ -1319,7 +1319,7 @@ "ignore-comments": { "description": "Ignore comments when counting lines.", "type": "boolean", - "default": false + "default": true } } }, diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 8bf37394612c..776a489afd98 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1319,7 +1319,7 @@ "ignore-comments": { "description": "Ignore comments when counting lines.", "type": "boolean", - "default": false + "default": true } } }, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 498b93dd00b3..86ee4fea48d2 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -41,6 +41,9 @@ var defaultLintersSettings = LintersSettings{ Forbidigo: ForbidigoSettings{ ExcludeGodocExamples: true, }, + Funlen: FunlenSettings{ + IgnoreComments: true, + }, GoChecksumType: GoChecksumTypeSettings{ DefaultSignifiesExhaustive: true, }, diff --git a/pkg/golinters/funlen/testdata/funlen.go b/pkg/golinters/funlen/testdata/funlen.go index 2e531b4adc30..fc4c4503113e 100644 --- a/pkg/golinters/funlen/testdata/funlen.go +++ b/pkg/golinters/funlen/testdata/funlen.go @@ -1,8 +1,129 @@ //golangcitest:args -Efunlen package testdata -// want +1 "Function 'main' is too long" -func main() { +func TooManyLines() { // want `Function 'TooManyLines' is too long \(70 > 60\)` + t := struct { + A0 string + A1 string + A2 string + A3 string + A4 string + A5 string + A6 string + A7 string + A8 string + A9 string + A10 string + A11 string + A12 string + A13 string + A14 string + A15 string + A16 string + A17 string + A18 string + A19 string + A20 string + A21 string + A22 string + A23 string + A24 string + A25 string + A26 string + A27 string + A28 string + A29 string + A30 string + A31 string + A32 string + }{ + A0: "a", + A1: "a", + A2: "a", + A3: "a", + A4: "a", + A5: "a", + A6: "a", + A7: "a", + A8: "a", + A9: "a", + A10: "a", + A11: "a", + A12: "a", + A13: "a", + A14: "a", + A15: "a", + A16: "a", + A17: "a", + A18: "a", + A19: "a", + A20: "a", + A21: "a", + A22: "a", + A23: "a", + A24: "a", + A25: "a", + A26: "a", + A27: "a", + A28: "a", + A29: "a", + A30: "a", + A31: "a", + A32: "a", + } + _ = t +} + +func TooManyStatements() { // want `Function 'TooManyStatements' has too many statements \(46 > 40\)` + a0 := 1 + a1 := 1 + a2 := 1 + a3 := 1 + a4 := 1 + a5 := 1 + a6 := 1 + a7 := 1 + a8 := 1 + a9 := 1 + a10 := 1 + a11 := 1 + a12 := 1 + a13 := 1 + a14 := 1 + a15 := 1 + a16 := 1 + a17 := 1 + a18 := 1 + a19 := 1 + a20 := 1 + a21 := 1 + a22 := 1 + _ = a0 + _ = a1 + _ = a2 + _ = a3 + _ = a4 + _ = a5 + _ = a6 + _ = a7 + _ = a8 + _ = a9 + _ = a10 + _ = a11 + _ = a12 + _ = a13 + _ = a14 + _ = a15 + _ = a16 + _ = a17 + _ = a18 + _ = a19 + _ = a20 + _ = a21 + _ = a22 +} + +func withComments() { // Comment 1 // Comment 2 // Comment 3 diff --git a/pkg/golinters/funlen/testdata/funlen_cgo.go b/pkg/golinters/funlen/testdata/funlen_cgo.go index dfc9ca15e749..b630fed89259 100644 --- a/pkg/golinters/funlen/testdata/funlen_cgo.go +++ b/pkg/golinters/funlen/testdata/funlen_cgo.go @@ -21,8 +21,129 @@ func _() { C.free(unsafe.Pointer(cs)) } -// want +1 "Function 'main' is too long" -func main() { +func TooManyLines() { // want `Function 'TooManyLines' is too long \(70 > 60\)` + t := struct { + A0 string + A1 string + A2 string + A3 string + A4 string + A5 string + A6 string + A7 string + A8 string + A9 string + A10 string + A11 string + A12 string + A13 string + A14 string + A15 string + A16 string + A17 string + A18 string + A19 string + A20 string + A21 string + A22 string + A23 string + A24 string + A25 string + A26 string + A27 string + A28 string + A29 string + A30 string + A31 string + A32 string + }{ + A0: "a", + A1: "a", + A2: "a", + A3: "a", + A4: "a", + A5: "a", + A6: "a", + A7: "a", + A8: "a", + A9: "a", + A10: "a", + A11: "a", + A12: "a", + A13: "a", + A14: "a", + A15: "a", + A16: "a", + A17: "a", + A18: "a", + A19: "a", + A20: "a", + A21: "a", + A22: "a", + A23: "a", + A24: "a", + A25: "a", + A26: "a", + A27: "a", + A28: "a", + A29: "a", + A30: "a", + A31: "a", + A32: "a", + } + _ = t +} + +func TooManyStatements() { // want `Function 'TooManyStatements' has too many statements \(46 > 40\)` + a0 := 1 + a1 := 1 + a2 := 1 + a3 := 1 + a4 := 1 + a5 := 1 + a6 := 1 + a7 := 1 + a8 := 1 + a9 := 1 + a10 := 1 + a11 := 1 + a12 := 1 + a13 := 1 + a14 := 1 + a15 := 1 + a16 := 1 + a17 := 1 + a18 := 1 + a19 := 1 + a20 := 1 + a21 := 1 + a22 := 1 + _ = a0 + _ = a1 + _ = a2 + _ = a3 + _ = a4 + _ = a5 + _ = a6 + _ = a7 + _ = a8 + _ = a9 + _ = a10 + _ = a11 + _ = a12 + _ = a13 + _ = a14 + _ = a15 + _ = a16 + _ = a17 + _ = a18 + _ = a19 + _ = a20 + _ = a21 + _ = a22 +} + +func withComments() { // Comment 1 // Comment 2 // Comment 3 diff --git a/pkg/golinters/funlen/testdata/funlen_ignore_comments.go b/pkg/golinters/funlen/testdata/funlen_ignore_comments.go index 72a58c531252..b57d63536871 100644 --- a/pkg/golinters/funlen/testdata/funlen_ignore_comments.go +++ b/pkg/golinters/funlen/testdata/funlen_ignore_comments.go @@ -1,9 +1,9 @@ //golangcitest:args -Efunlen //golangcitest:config_path testdata/funlen_ignore_comments.yml -//golangcitest:expected_exitcode 0 package testdata -func main() { +// want +1 "Function 'withComments' is too long" +func withComments() { // Comment 1 // Comment 2 // Comment 3 diff --git a/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml b/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml index c81941c43d2b..fc03026ce6a2 100644 --- a/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml +++ b/pkg/golinters/funlen/testdata/funlen_ignore_comments.yml @@ -3,4 +3,4 @@ version: "2" linters: settings: funlen: - ignore-comments: true + ignore-comments: false