From 950a274afc8b54fc081b77e4d8336760a974bcf4 Mon Sep 17 00:00:00 2001 From: wangggong <793160615@qq.com> Date: Tue, 14 Dec 2021 21:10:35 +0800 Subject: [PATCH] golangci-lint: support durationcheck (#30027) --- .golangci.yml | 1 + server/util.go | 8 ++++---- session/session.go | 3 ++- types/time.go | 22 +++++++++++----------- util/execdetails/execdetails.go | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 816e2404a9e36..d262ed0e0457b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters: - rowserrcheck - unconvert - makezero + - durationcheck linters-settings: staticcheck: diff --git a/server/util.go b/server/util.go index 2a45dce017dc2..f5518f0396a9a 100644 --- a/server/util.go +++ b/server/util.go @@ -179,16 +179,16 @@ func dumpBinaryTime(dur time.Duration) (data []byte) { dur = -dur } days := dur / (24 * time.Hour) - dur -= days * 24 * time.Hour + dur -= days * 24 * time.Hour //nolint:durationcheck data[2] = byte(days) hours := dur / time.Hour - dur -= hours * time.Hour + dur -= hours * time.Hour //nolint:durationcheck data[6] = byte(hours) minutes := dur / time.Minute - dur -= minutes * time.Minute + dur -= minutes * time.Minute //nolint:durationcheck data[7] = byte(minutes) seconds := dur / time.Second - dur -= seconds * time.Second + dur -= seconds * time.Second //nolint:durationcheck data[8] = byte(seconds) if dur == 0 { data[0] = 8 diff --git a/session/session.go b/session/session.go index 8a2b61e50e6db..bfc5288a7ff4f 100644 --- a/session/session.go +++ b/session/session.go @@ -1549,7 +1549,8 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex failpoint.Inject("mockStmtSlow", func(val failpoint.Value) { if strings.Contains(stmtNode.Text(), "/* sleep */") { - time.Sleep(time.Duration(val.(int)) * time.Millisecond) + v, _ := val.(int) + time.Sleep(time.Duration(v) * time.Millisecond) } }) diff --git a/types/time.go b/types/time.go index 8d2a11fc58d90..0ea01c8a85f96 100644 --- a/types/time.go +++ b/types/time.go @@ -49,9 +49,9 @@ const ( // MaxDuration is the maximum for duration. MaxDuration int64 = 838*10000 + 59*100 + 59 // MinTime is the minimum for mysql time type. - MinTime = -gotime.Duration(838*3600+59*60+59) * gotime.Second + MinTime = -(838*gotime.Hour + 59*gotime.Minute + 59*gotime.Second) // MaxTime is the maximum for mysql time type. - MaxTime = gotime.Duration(838*3600+59*60+59) * gotime.Second + MaxTime = 838*gotime.Hour + 59*gotime.Minute + 59*gotime.Second // ZeroDatetimeStr is the string representation of a zero datetime. ZeroDatetimeStr = "0000-00-00 00:00:00" // ZeroDateStr is the string representation of a zero date. @@ -466,7 +466,7 @@ func (t Time) ConvertToDuration() (Duration, error) { hour, minute, second := t.Clock() frac := t.Microsecond() * 1000 - d := gotime.Duration(hour*3600+minute*60+second)*gotime.Second + gotime.Duration(frac) + d := gotime.Duration(hour*3600+minute*60+second)*gotime.Second + gotime.Duration(frac) //nolint:durationcheck // TODO: check convert validation return Duration{Duration: d, Fsp: t.Fsp()}, nil } @@ -579,7 +579,7 @@ func RoundFrac(t gotime.Time, fsp int8) (gotime.Time, error) { if err != nil { return t, errors.Trace(err) } - return t.Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil + return t.Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil //nolint:durationcheck } // TruncateFrac truncates fractional seconds precision with new fsp and returns a new one. @@ -589,7 +589,7 @@ func TruncateFrac(t gotime.Time, fsp int8) (gotime.Time, error) { if _, err := CheckFsp(int(fsp)); err != nil { return t, err } - return t.Truncate(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil + return t.Truncate(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil //nolint:durationcheck } // ToPackedUint encodes Time to a packed uint64 value. @@ -1270,7 +1270,7 @@ func AdjustYear(y int64, adjustZero bool) (int64, error) { // NewDuration construct duration with time. func NewDuration(hour, minute, second, microsecond int, fsp int8) Duration { return Duration{ - Duration: gotime.Duration(hour)*gotime.Hour + gotime.Duration(minute)*gotime.Minute + gotime.Duration(second)*gotime.Second + gotime.Duration(microsecond)*gotime.Microsecond, + Duration: gotime.Duration(hour)*gotime.Hour + gotime.Duration(minute)*gotime.Minute + gotime.Duration(second)*gotime.Second + gotime.Duration(microsecond)*gotime.Microsecond, //nolint:durationcheck Fsp: fsp, } } @@ -1490,7 +1490,7 @@ func (d Duration) RoundFrac(fsp int8, loc *gotime.Location) (Duration, error) { } n := gotime.Date(0, 0, 0, 0, 0, 0, 0, tz) - nd := n.Add(d.Duration).Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond).Sub(n) + nd := n.Add(d.Duration).Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond).Sub(n) //nolint:durationcheck return Duration{Duration: nd, Fsp: fsp}, nil } @@ -1711,7 +1711,7 @@ func matchDuration(str string, fsp int8) (Duration, error) { return Duration{t, fsp}, ErrTruncatedWrongVal.GenWithStackByArgs("time", str) } - d := gotime.Duration(hhmmss[0]*3600+hhmmss[1]*60+hhmmss[2])*gotime.Second + gotime.Duration(frac)*gotime.Microsecond + d := gotime.Duration(hhmmss[0]*3600+hhmmss[1]*60+hhmmss[2])*gotime.Second + gotime.Duration(frac)*gotime.Microsecond //nolint:durationcheck if negative { d = -d } @@ -1800,11 +1800,11 @@ func splitDuration(t gotime.Duration) (int, int, int, int, int) { } hours := t / gotime.Hour - t -= hours * gotime.Hour + t -= hours * gotime.Hour //nolint:durationcheck minutes := t / gotime.Minute - t -= minutes * gotime.Minute + t -= minutes * gotime.Minute //nolint:durationcheck seconds := t / gotime.Second - t -= seconds * gotime.Second + t -= seconds * gotime.Second //nolint:durationcheck fraction := t / gotime.Microsecond return sign, int(hours), int(minutes), int(seconds), int(fraction) diff --git a/util/execdetails/execdetails.go b/util/execdetails/execdetails.go index 4265145c2d66a..ea1e4cda1e746 100644 --- a/util/execdetails/execdetails.go +++ b/util/execdetails/execdetails.go @@ -968,7 +968,7 @@ func FormatDuration(d time.Duration) string { if unit == time.Nanosecond { return d.String() } - integer := (d / unit) * unit + integer := (d / unit) * unit //nolint:durationcheck decimal := float64(d%unit) / float64(unit) if d < 10*unit { decimal = math.Round(decimal*100) / 100