-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrules.go
137 lines (123 loc) · 4.74 KB
/
rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
//go:build gorules
package gorules
// 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`,
`$tx, $err := $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginRwNosync($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRwNosync($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 cursorDeferClose(m dsl.Matcher) {
m.Match(
`$c, $err = $db.Cursor($table); $chk; $close`,
`$c, $err := $db.Cursor($table); $chk; $close`,
`$c, $err = $db.RwCursor($table); $chk; $close`,
`$c, $err := $db.RwCursor($table); $chk; $close`,
`$c, $err = $db.CursorDupSort($table); $chk; $close`,
`$c, $err := $db.CursorDupSort($table); $chk; $close`,
`$c, $err = $db.RwCursorDupSort($table); $chk; $close`,
`$c, $err := $db.RwCursorDupSort($table); $chk; $close`,
).
Where(!m["close"].Text.Matches(`defer .*\.Close()`)).
//At(m["rollback"]).
Report(`Add "defer $c.Close()" right after cursor creation error check`)
}
func streamDeferClose(m dsl.Matcher) {
m.Match(
`$c, $err = $db.Range($params); $chk; $close`,
`$c, $err := $db.Range($params); $chk; $close`,
`$c, $err = $db.RangeDupSort($params); $chk; $close`,
`$c, $err := $db.RangeDupSort($params); $chk; $close`,
`$c, $err = $db.Prefix($params); $chk; $close`,
`$c, $err := $db.Prefix($params); $chk; $close`,
).
Where(!m["close"].Text.Matches(`defer .*\.Close()`)).
//At(m["rollback"]).
Report(`Add "defer $c.Close()" right after cursor creation error check`)
}
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 $mu.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.`)
}