Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bad defer unlock #131

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions analyzer/testdata/src/extra/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,15 @@ func mismatchingUnlock2Struct(x *withMutex, op func()) {
defer x.mu.Unlock()
op()
}

func mismatchingDeferLock1(x *withMutex, op func()) {
x.mu.Lock()
defer x.mu.Lock() // want `\Qmaybe defer x.mu.Unlock() was intended?`
op()
}

func mismatchingDeferLock2(x *withMutex, op func()) {
x.mu.RLock()
defer x.mu.RLock() // want `\Qmaybe defer x.mu.RUnlock() was intended?`
op()
}
12 changes: 12 additions & 0 deletions analyzer/testdata/src/extra/negative_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ func goodRUnlock(mu *sync.RWMutex, op func()) {
op()
}

func goodStrangeLocks(x1, x2 *sync.RWMutex, op func()) {
x1.Lock()
defer x2.Lock()
op()
}

func goodStrangeRLocks(x1, x2 *sync.RWMutex, op func()) {
x1.RLock()
defer x2.RLock()
op()
}

func differentMutexes(mu1, mu2 *sync.RWMutex, op func()) {
{
mu2.RLock()
Expand Down
9 changes: 9 additions & 0 deletions analyzer/testdata/src/extra/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,13 @@ func _(m fluent.Matcher) {

m.Match(`$mu.Lock(); defer $mu.RUnlock()`).Report(`maybe $mu.RLock() was intended?`)
m.Match(`$mu.RLock(); defer $mu.Unlock()`).Report(`maybe $mu.Lock() was intended?`)

m.Match(`$mu1.Lock(); defer $mu2.Lock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.Unlock() was intended?`)
m.Match(`$mu1.RLock(); defer $mu2.RLock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.RUnlock() was intended?`)
}
10 changes: 10 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@ func gocriticFlagDeref(m fluent.Matcher) {
func gocriticBadLock(m fluent.Matcher) {
m.Match(`$mu.Lock(); defer $mu.RUnlock()`).Report(`maybe $mu.RLock() was intended?`)
m.Match(`$mu.RLock(); defer $mu.Unlock()`).Report(`maybe $mu.Lock() was intended?`)

// `mu1` and `mu2` are added to make possible report line were `m2` is used (with defer)
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
m.Match(`$mu1.Lock(); defer $mu2.Lock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.Unlock() was intended?`)
m.Match(`$mu1.RLock(); defer $mu2.RLock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.RUnlock() was intended?`)
}

func reviveBoolLiteralInExpr(m fluent.Matcher) {
Expand Down