mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 13:07:17 +00:00
linter: add rules.go from erigon #418 Open
This commit is contained in:
parent
04337fd090
commit
fcb49c713a
1
go.mod
1
go.mod
@ -16,6 +16,7 @@ require (
|
|||||||
github.com/ledgerwatch/log/v3 v3.4.1
|
github.com/ledgerwatch/log/v3 v3.4.1
|
||||||
github.com/ledgerwatch/secp256k1 v1.0.0
|
github.com/ledgerwatch/secp256k1 v1.0.0
|
||||||
github.com/matryer/moq v0.2.7
|
github.com/matryer/moq v0.2.7
|
||||||
|
github.com/quasilyte/go-ruleguard/dsl v0.3.19
|
||||||
github.com/spaolacci/murmur3 v1.1.0
|
github.com/spaolacci/murmur3 v1.1.0
|
||||||
github.com/stretchr/testify v1.7.1
|
github.com/stretchr/testify v1.7.1
|
||||||
github.com/torquem-ch/mdbx-go v0.23.2
|
github.com/torquem-ch/mdbx-go v0.23.2
|
||||||
|
2
go.sum
2
go.sum
@ -93,6 +93,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
|||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
|
github.com/quasilyte/go-ruleguard/dsl v0.3.19 h1:5+KTKb2YREUYiqZFEIuifFyBxlcCUPWgNZkWy71XS0Q=
|
||||||
|
github.com/quasilyte/go-ruleguard/dsl v0.3.19/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
|
||||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
|
85
rules.go
Normal file
85
rules.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
//go:build gorules
|
||||||
|
// +build gorules
|
||||||
|
|
||||||
|
package gorules
|
||||||
|
|
||||||
|
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
|
||||||
|
// to apply changes in this file, please do: ./build/bin/golangci-lint cache clean
|
||||||
|
import (
|
||||||
|
"github.com/quasilyte/go-ruleguard/dsl"
|
||||||
|
//quasilyterules "github.com/quasilyte/ruleguard-rules-test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
//dsl.ImportRules("qrules", quasilyterules.Bundle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func txDeferRollback(m dsl.Matcher) {
|
||||||
|
// Common pattern for long-living transactions:
|
||||||
|
// tx, err := db.Begin()
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// defer tx.Rollback()
|
||||||
|
//
|
||||||
|
// ... code which uses database in transaction
|
||||||
|
//
|
||||||
|
// err := tx.Commit()
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
m.Match(
|
||||||
|
`$tx, $err := $db.BeginRw($ctx); $chk; $rollback`,
|
||||||
|
`$tx, $err = $db.BeginRw($ctx); $chk; $rollback`,
|
||||||
|
`$tx, $err := $db.Begin($ctx); $chk; $rollback`,
|
||||||
|
`$tx, $err = $db.Begin($ctx); $chk; $rollback`,
|
||||||
|
).
|
||||||
|
Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)).
|
||||||
|
//At(m["rollback"]).
|
||||||
|
Report(`Add "defer $tx.Rollback()" right after transaction creation error check.
|
||||||
|
If you are in the loop - consider use "$db.View" or "$db.Update" or extract whole transaction to function.
|
||||||
|
Without rollback in defer - app can deadlock on error or panic.
|
||||||
|
Rules are in ./rules.go file.
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeCollector(m dsl.Matcher) {
|
||||||
|
m.Match(`$c := etl.NewCollector($*_); $close`).
|
||||||
|
Where(!m["close"].Text.Matches(`defer .*\.Close()`)).
|
||||||
|
Report(`Add "defer $c.Close()" right after collector creation`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeLockedDir(m dsl.Matcher) {
|
||||||
|
m.Match(`$c := dir.OpenRw($*_); $close`).
|
||||||
|
Where(!m["close"].Text.Matches(`defer .*\.Close()`)).
|
||||||
|
Report(`Add "defer $c.Close()" after locked.OpenDir`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func passValuesByContext(m dsl.Matcher) {
|
||||||
|
m.Match(`ctx.WithValue($*_)`).Report(`Don't pass app-level parameters by context, pass them as-is or as typed objects`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mismatchingUnlock(m dsl.Matcher) {
|
||||||
|
// By default, an entire match position is used as a location.
|
||||||
|
// This can be changed by the At() method that binds the location
|
||||||
|
// to the provided named submatch.
|
||||||
|
//
|
||||||
|
// In the rules below text editor would get mismatching method
|
||||||
|
// name locations:
|
||||||
|
//
|
||||||
|
// defer mu.RUnlock()
|
||||||
|
// ^^^^^^^
|
||||||
|
|
||||||
|
m.Match(`$mu.Lock(); defer $mu.$unlock()`).
|
||||||
|
Where(m["unlock"].Text == "RUnlock").
|
||||||
|
At(m["unlock"]).
|
||||||
|
Report(`maybe $2mu.Unlock() was intended?
|
||||||
|
Rules are in ./rules.go file.`)
|
||||||
|
|
||||||
|
m.Match(`$mu.RLock(); defer $mu.$unlock()`).
|
||||||
|
Where(m["unlock"].Text == "Unlock").
|
||||||
|
At(m["unlock"]).
|
||||||
|
Report(`maybe $mu.RUnlock() was intended?
|
||||||
|
Rules are in ./rules.go file.`)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user