From 50bf659d3e41db79439ef080e427f90c6e4b8a26 Mon Sep 17 00:00:00 2001 From: Morgan Date: Sun, 26 May 2024 12:43:35 +0200 Subject: [PATCH] chore(lint): forbid usage of `regexp.Match(String)?` (#2066) Just to avoid [this comment](https://github.com/gnolang/gno/pull/875#discussion_r1595553714) again :) --- .github/golangci.yml | 5 ++++ tm2/pkg/amino/genproto/bindings.go | 48 ++++++++++++++---------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.github/golangci.yml b/.github/golangci.yml index 8b38691ec7..e78d09a582 100644 --- a/.github/golangci.yml +++ b/.github/golangci.yml @@ -38,6 +38,7 @@ linters: - gofumpt # Stricter gofmt - unused # Checks Go code for unused constants, variables, functions and types - gomodguard # Enforces an allow and block list for direct Go module dependencies + - forbidigo # Forbids some custom-set identifiers, like regexp.MatchString linters-settings: gofmt: @@ -60,6 +61,10 @@ linters-settings: - opinionated - performance - style + forbidigo: + forbid: + - p: '^regexp\.(Match|MatchString)$' + msg: it will re-compile the regexp for each execution; compile the regexp with regexp.Compile and store it as a singleton issues: whole-files: true diff --git a/tm2/pkg/amino/genproto/bindings.go b/tm2/pkg/amino/genproto/bindings.go index 5d3b46c59f..c1de79caa5 100644 --- a/tm2/pkg/amino/genproto/bindings.go +++ b/tm2/pkg/amino/genproto/bindings.go @@ -1100,6 +1100,27 @@ func _fields(args ...interface{}) *ast.FieldList { } } +const ( + reDGTS = `(?:[0-9]+)` + reHExX = `(?:0[xX][0-9a-fA-F]+)` + rePSCI = `(?:[eE]+?[0-9]+)` + reNSCI = `(?:[eE]-[1-9][0-9]+)` + reASCI = `(?:[eE][-+]?[0-9]+)` +) + +var ( + reIsInt = regexp.MustCompile( + `^-?(?:` + + reDGTS + `|` + + reHExX + `)` + rePSCI + `?$`, + ) + reIsFloat = regexp.MustCompile( + `^-?(?:` + + reDGTS + `\.` + reDGTS + reASCI + `?|` + + reDGTS + reNSCI + `)$`, + ) +) + // Parses simple expressions (but not all). // Useful for parsing strings to ast nodes, like foo.bar["qwe"](), // new(bytes.Buffer), *bytes.Buffer, package.MyStruct{FieldA:1}, numeric @@ -1282,22 +1303,7 @@ func _x(expr string, args ...interface{}) ast.Expr { } } // Numeric int? We do these before dots, because dots are legal in numbers. - const ( - DGTS = `(?:[0-9]+)` - HExX = `(?:0[xX][0-9a-fA-F]+)` - PSCI = `(?:[eE]+?[0-9]+)` - NSCI = `(?:[eE]-[1-9][0-9]+)` - ASCI = `(?:[eE][-+]?[0-9]+)` - ) - isInt, err := regexp.Match( - `^-?(?:`+ - DGTS+`|`+ - HExX+`)`+PSCI+`?$`, - []byte(expr), - ) - if err != nil { - panic("should not happen") - } + isInt := reIsInt.MatchString(expr) if isInt { return &ast.BasicLit{ Kind: token.INT, @@ -1305,15 +1311,7 @@ func _x(expr string, args ...interface{}) ast.Expr { } } // Numeric float? We do these before dots, because dots are legal in floats. - isFloat, err := regexp.Match( - `^-?(?:`+ - DGTS+`\.`+DGTS+ASCI+`?|`+ - DGTS+NSCI+`)$`, - []byte(expr), - ) - if err != nil { - panic("should not happen") - } + isFloat := reIsFloat.MatchString(expr) if isFloat { return &ast.BasicLit{ Kind: token.FLOAT,