Skip to content

Commit

Permalink
update ruleguard (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed Dec 26, 2021
1 parent d9ade26 commit 785d1f6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 85 deletions.
22 changes: 14 additions & 8 deletions checkers/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,17 @@ func stringConcatSimplify(m dsl.Matcher) {
//doc:before t.Unix() / 1000
//doc:after t.UnixMilli()
func timeExprSimplify(m dsl.Matcher) {
isTime := func(v dsl.Var) bool {
return v.Type.Is(`time.Time`) || v.Type.Is(`*time.Time`)
}

m.Match(`$t.Unix() / 1000`).
Where(m.GoVersion().GreaterEqThan("1.17") &&
(m["t"].Type.Is(`time.Time`) || m["t"].Type.Is(`*time.Time`))).
Where(m.GoVersion().GreaterEqThan("1.17") && isTime(m["t"])).
Suggest("$t.UnixMilli()").
Report(`use $t.UnixMilli() instead of $$`)

m.Match(`$t.UnixNano() * 1000`).
Where(m.GoVersion().GreaterEqThan("1.17") &&
(m["t"].Type.Is(`time.Time`) || m["t"].Type.Is(`*time.Time`))).
Where(m.GoVersion().GreaterEqThan("1.17") && isTime(m["t"])).
Suggest("$t.UnixMicro()").
Report(`use $t.UnixMicro() instead of $$`)
}
Expand All @@ -654,20 +656,24 @@ func timeExprSimplify(m dsl.Matcher) {
//doc:before type Foo struct{ ...; sync.Mutex; ... }
//doc:after type Foo struct{ ...; mu sync.Mutex; ... }
func exposedSyncMutex(m dsl.Matcher) {
isExported := func(v dsl.Var) bool {
return v.Text.Matches(`^\p{Lu}`)
}

m.Match(`type $x struct { $*_; sync.Mutex; $*_ }`).
Where(m["x"].Text.Matches(`^\p{Lu}`)).
Where(isExported(m["x"])).
Report("don't embed sync.Mutex")

m.Match(`type $x struct { $*_; *sync.Mutex; $*_ }`).
Where(m["x"].Text.Matches(`^\p{Lu}`)).
Where(isExported(m["x"])).
Report("don't embed *sync.Mutex")

m.Match(`type $x struct { $*_; sync.RWMutex; $*_ }`).
Where(m["x"].Text.Matches(`^\p{Lu}`)).
Where(isExported(m["x"])).
Report("don't embed sync.RWMutex")

m.Match(`type $x struct { $*_; *sync.RWMutex; $*_ }`).
Where(m["x"].Text.Matches(`^\p{Lu}`)).
Where(isExported(m["x"])).
Report("don't embed *sync.RWMutex")
}

Expand Down

0 comments on commit 785d1f6

Please sign in to comment.