Skip to content

Commit

Permalink
Merge branch 'master' into liamg-add-todo-linter
Browse files Browse the repository at this point in the history
  • Loading branch information
liamg committed Dec 28, 2021
2 parents fd8ed00 + b808007 commit 26ed3c4
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 86 deletions.
1 change: 1 addition & 0 deletions checkers/commentFormatting_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
`^//line /.*:\d+`, // e.g.: line /path/to/file:123
`^//export \w+$`, // e.g.: export Foo
`^//[/+#-]+.*$`, // e.g.: vertical breaker /////////////
`^//noinspection `, // e.g.: noinspection ALL, some GoLand and friends versions
}
pat := "(?m)" + strings.Join(parts, "|")
pragmaRE := regexp.MustCompile(pat)
Expand Down
37 changes: 29 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 Expand Up @@ -711,3 +717,18 @@ func emptyDecl(m dsl.Matcher) {
m.Match(`const()`).Report(`empty const() block`)
m.Match(`type()`).Report(`empty type() block`)
}

//doc:summary Detects suspicious formatting strings usage
//doc:tags diagnostic experimental
//doc:before fmt.Errorf(msg)
//doc:after fmt.Errorf("%s", msg)
func dynamicFmtString(m dsl.Matcher) {
m.Match(`fmt.Errorf($f)`).
Where(!m["f"].Const).
Suggest("errors.New($f)").
Report(`use errors.New($f) or fmt.Errorf("%s", $f) instead`)

m.Match(`fmt.Errorf($f($*args))`).
Suggest("errors.New($f($*args))").
Report(`use errors.New($f($*args)) or fmt.Errorf("%s", $f($*args)) instead`)
}

0 comments on commit 26ed3c4

Please sign in to comment.