Skip to content

Commit

Permalink
Refactor and modify error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Mar 19, 2023
1 parent 9f139d3 commit 73a6877
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
30 changes: 15 additions & 15 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import (
)

func bad() {
log.Error() // want "missing Msg or Send call for zerolog log method"
log.Info() // want "missing Msg or Send call for zerolog log method"
log.Fatal() // want "missing Msg or Send call for zerolog log method"
log.Debug() // want "missing Msg or Send call for zerolog log method"
log.Warn() // want "missing Msg or Send call for zerolog log method"
log.Error() // want "missing to dispatch with Msg or Send function. nothing will be logged"
log.Info() // want "missing to dispatch with Msg or Send function. nothing will be logged"
log.Fatal() // want "missing to dispatch with Msg or Send function. nothing will be logged"
log.Debug() // want "missing to dispatch with Msg or Send function. nothing will be logged"
log.Warn() // want "missing to dispatch with Msg or Send function. nothing will be logged"

var err error
log.Error().Err(err) // want "missing Msg or Send call for zerolog log method"
log.Error().Err(err).Str("foo", "bar").Int("foo", 1) // want "missing Msg or Send call for zerolog log method"
log.Error().Err(err) // want "missing to dispatch with Msg or Send function. nothing will be logged"
log.Error().Err(err).Str("foo", "bar").Int("foo", 1) // want "missing to dispatch with Msg or Send function. nothing will be logged"

logger := log.Error() // want "missing Msg or Send call for zerolog log method"
logger := log.Error() // want "missing to dispatch with Msg or Send function. nothing will be logged"
logger.Err(err).Str("foo", "bar").Int("foo", 1)

// include zerolog.Dict()
log.Info(). // want "missing Msg or Send call for zerolog log method"
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
log.Info(). // want "missing to dispatch with Msg or Send function. nothing will be logged"
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
)

// conditional
logger2 := log.Info() // want "missing Msg or Send call for zerolog log method"
logger2 := log.Info() // want "missing to dispatch with Msg or Send function. nothing will be logged"
if err != nil {
logger2 = log.Error() // want "missing Msg or Send call for zerolog log method"
logger2 = log.Error() // want "missing to dispatch with Msg or Send function. nothing will be logged"
}
logger2.Str("foo", "bar")
}
Expand Down
18 changes: 4 additions & 14 deletions zerologlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,18 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
}
// At the end, if the set is clear -> ok.
// if the set is not clear -> there must be a left zerolog.Event var that weren't dispached.
// -> Report
// Otherwise, there must be a left zerolog.Event var that weren't dispached. So report it.
for k := range set {
pass.Reportf(k.Pos(), "missing Msg or Send call for zerolog log method")
pass.Reportf(k.Pos(), "missing to dispatch with Msg or Send function. nothing will be logged")
}
return nil, nil
}

func isInLogPkg(c *ssa.Call) bool {
switch v := c.Call.Value.(type) {
case ssa.Member:
p := removeVendor(v.Package().Pkg.Path())
return p == "github.com/rs/zerolog/log"
p := v.Package().Pkg.Path()
return strings.HasSuffix(p, "github.com/rs/zerolog/log")
default:
return false
}
Expand All @@ -96,15 +95,6 @@ func isZerologEvent(v ssa.Value) bool {
return strings.HasSuffix(ts, "github.com/rs/zerolog.Event")
}

// RemoVendor removes vendoring information from import path.
func removeVendor(path string) string {
i := strings.Index(path, "vendor/")
if i >= 0 {
return path[i+len("vendor/"):]
}
return path
}

func isDispatchMethod(c *ssa.Call) bool {
m := c.Common().StaticCallee().Name()
if m == "Send" || m == "Msg" {
Expand Down

0 comments on commit 73a6877

Please sign in to comment.