Commit 3fdf4be5 by Arve Knudsen Committed by GitHub

Chore: Enable more go-ruleguard rules (#29781)

* Chore: Enable more go-ruleguard rules

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent e33c7cfb
...@@ -112,17 +112,15 @@ func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error { ...@@ -112,17 +112,15 @@ func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
func (b *InProcBus) Dispatch(msg Msg) error { func (b *InProcBus) Dispatch(msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name() var msgName = reflect.TypeOf(msg).Elem().Name()
var handler = b.handlersWithCtx[msgName]
withCtx := true withCtx := true
handler := b.handlersWithCtx[msgName]
if handler == nil { if handler == nil {
withCtx = false withCtx = false
handler = b.handlers[msgName] handler = b.handlers[msgName]
}
if handler == nil { if handler == nil {
return ErrHandlerNotFound return ErrHandlerNotFound
} }
}
var params = []reflect.Value{} var params = []reflect.Value{}
if withCtx { if withCtx {
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"math" "math"
"reflect" "reflect"
"runtime" "runtime"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp/parse" "github.com/grafana/grafana/pkg/expr/mathexp/parse"
...@@ -130,7 +129,7 @@ func (e *State) walkUnary(node *parse.UnaryNode) (Results, error) { ...@@ -130,7 +129,7 @@ func (e *State) walkUnary(node *parse.UnaryNode) (Results, error) {
} }
func (e *State) unarySeries(s Series, op string) (Series, error) { func (e *State) unarySeries(s Series, op string) (Series, error) {
newSeries := NewSeries(e.RefID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len()) newSeries := NewSeries(e.RefID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, s.Len())
for i := 0; i < s.Len(); i++ { for i := 0; i < s.Len(); i++ {
t, f := s.GetPoint(i) t, f := s.GetPoint(i)
if f == nil { if f == nil {
...@@ -432,7 +431,7 @@ func (e *State) biScalarNumber(labels data.Labels, op string, number Number, sca ...@@ -432,7 +431,7 @@ func (e *State) biScalarNumber(labels data.Labels, op string, number Number, sca
} }
func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVal *float64, seriesFirst bool) (Series, error) { func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVal *float64, seriesFirst bool) (Series, error) {
newSeries := NewSeries(e.RefID, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len()) newSeries := NewSeries(e.RefID, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, s.Len())
var err error var err error
for i := 0; i < s.Len(); i++ { for i := 0; i < s.Len(); i++ {
nF := math.NaN() nF := math.NaN()
...@@ -462,18 +461,21 @@ func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVa ...@@ -462,18 +461,21 @@ func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVa
// biSeriesSeries performs a the binary operation for each value in the two series where the times // biSeriesSeries performs a the binary operation for each value in the two series where the times
// are equal. If there are datapoints in A or B that do not share a time, they will be dropped. // are equal. If there are datapoints in A or B that do not share a time, they will be dropped.
func (e *State) biSeriesSeries(labels data.Labels, op string, aSeries, bSeries Series) (Series, error) { func (e *State) biSeriesSeries(labels data.Labels, op string, aSeries, bSeries Series) (Series, error) {
bPoints := make(map[time.Time]*float64) bPoints := make(map[string]*float64)
for i := 0; i < bSeries.Len(); i++ { for i := 0; i < bSeries.Len(); i++ {
t, f := bSeries.GetPoint(i) t, f := bSeries.GetPoint(i)
if t != nil { if t != nil {
bPoints[*t] = f bPoints[t.UTC().String()] = f
} }
} }
newSeries := NewSeries(e.RefID, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx, aSeries.ValueIsNullabe || bSeries.ValueIsNullabe, 0) newSeries := NewSeries(
e.RefID, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx,
aSeries.ValueIsNullable || bSeries.ValueIsNullable, 0,
)
for aIdx := 0; aIdx < aSeries.Len(); aIdx++ { for aIdx := 0; aIdx < aSeries.Len(); aIdx++ {
aTime, aF := aSeries.GetPoint(aIdx) aTime, aF := aSeries.GetPoint(aIdx)
bF, ok := bPoints[*aTime] bF, ok := bPoints[aTime.UTC().String()]
if !ok { if !ok {
continue continue
} }
......
...@@ -95,7 +95,10 @@ func perFloat(e *State, val Value, floatF func(x float64) float64) (Value, error ...@@ -95,7 +95,10 @@ func perFloat(e *State, val Value, floatF func(x float64) float64) (Value, error
newVal = NewScalar(e.RefID, &nF) newVal = NewScalar(e.RefID, &nF)
case parse.TypeSeriesSet: case parse.TypeSeriesSet:
resSeries := val.(Series) resSeries := val.(Series)
newSeries := NewSeries(e.RefID, resSeries.GetLabels(), resSeries.TimeIdx, resSeries.TimeIsNullable, resSeries.ValueIdx, resSeries.ValueIsNullabe, resSeries.Len()) newSeries := NewSeries(
e.RefID, resSeries.GetLabels(), resSeries.TimeIdx, resSeries.TimeIsNullable, resSeries.ValueIdx,
resSeries.ValueIsNullable, resSeries.Len(),
)
for i := 0; i < resSeries.Len(); i++ { for i := 0; i < resSeries.Len(); i++ {
t, f := resSeries.GetPoint(i) t, f := resSeries.GetPoint(i)
nF := math.NaN() nF := math.NaN()
......
...@@ -14,7 +14,7 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin ...@@ -14,7 +14,7 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
if newSeriesLength <= 0 { if newSeriesLength <= 0 {
return s, fmt.Errorf("the series cannot be sampled further; the time range is shorter than the interval") return s, fmt.Errorf("the series cannot be sampled further; the time range is shorter than the interval")
} }
resampled := NewSeries(refID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, newSeriesLength+1) resampled := NewSeries(refID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullable, newSeriesLength+1)
bookmark := 0 bookmark := 0
var lastSeen *float64 var lastSeen *float64
idx := 0 idx := 0
......
...@@ -14,7 +14,7 @@ type Series struct { ...@@ -14,7 +14,7 @@ type Series struct {
Frame *data.Frame Frame *data.Frame
TimeIsNullable bool TimeIsNullable bool
TimeIdx int TimeIdx int
ValueIsNullabe bool ValueIsNullable bool
ValueIdx int ValueIdx int
// TODO: // TODO:
// - Multiple Value Fields // - Multiple Value Fields
...@@ -43,7 +43,7 @@ func SeriesFromFrame(frame *data.Frame) (s Series, err error) { ...@@ -43,7 +43,7 @@ func SeriesFromFrame(frame *data.Frame) (s Series, err error) {
foundValue = true foundValue = true
s.ValueIdx = i s.ValueIdx = i
case data.FieldTypeNullableFloat64: case data.FieldTypeNullableFloat64:
s.ValueIsNullabe = true s.ValueIsNullable = true
foundValue = true foundValue = true
s.ValueIdx = i s.ValueIdx = i
default: default:
...@@ -80,7 +80,7 @@ func NewSeries(refID string, labels data.Labels, timeIdx int, nullableTime bool, ...@@ -80,7 +80,7 @@ func NewSeries(refID string, labels data.Labels, timeIdx int, nullableTime bool,
Frame: data.NewFrame("", fields...), Frame: data.NewFrame("", fields...),
TimeIsNullable: nullableTime, TimeIsNullable: nullableTime,
TimeIdx: timeIdx, TimeIdx: timeIdx,
ValueIsNullabe: nullableValue, ValueIsNullable: nullableValue,
ValueIdx: valueIdx, ValueIdx: valueIdx,
} }
} }
...@@ -115,7 +115,7 @@ func (s Series) SetPoint(pointIdx int, t *time.Time, f *float64) (err error) { ...@@ -115,7 +115,7 @@ func (s Series) SetPoint(pointIdx int, t *time.Time, f *float64) (err error) {
} }
s.Frame.Fields[s.TimeIdx].Set(pointIdx, *t) s.Frame.Fields[s.TimeIdx].Set(pointIdx, *t)
} }
if s.ValueIsNullabe { if s.ValueIsNullable {
s.Frame.Fields[s.ValueIdx].Set(pointIdx, f) s.Frame.Fields[s.ValueIdx].Set(pointIdx, f)
} else { } else {
if f == nil { if f == nil {
...@@ -136,7 +136,7 @@ func (s Series) AppendPoint(pointIdx int, t *time.Time, f *float64) (err error) ...@@ -136,7 +136,7 @@ func (s Series) AppendPoint(pointIdx int, t *time.Time, f *float64) (err error)
} }
s.Frame.Fields[s.TimeIdx].Append(*t) s.Frame.Fields[s.TimeIdx].Append(*t)
} }
if s.ValueIsNullabe { if s.ValueIsNullable {
s.Frame.Fields[s.ValueIdx].Append(f) s.Frame.Fields[s.ValueIdx].Append(f)
} else { } else {
if f == nil { if f == nil {
...@@ -163,7 +163,7 @@ func (s Series) GetTime(pointIdx int) *time.Time { ...@@ -163,7 +163,7 @@ func (s Series) GetTime(pointIdx int) *time.Time {
// GetValue returns the float value at the specified index. // GetValue returns the float value at the specified index.
func (s Series) GetValue(pointIdx int) *float64 { func (s Series) GetValue(pointIdx int) *float64 {
if s.ValueIsNullabe { if s.ValueIsNullable {
return s.Frame.Fields[s.ValueIdx].At(pointIdx).(*float64) return s.Frame.Fields[s.ValueIdx].At(pointIdx).(*float64)
} }
f := s.Frame.Fields[s.ValueIdx].At(pointIdx).(float64) f := s.Frame.Fields[s.ValueIdx].At(pointIdx).(float64)
......
...@@ -95,7 +95,7 @@ func TestSeriesFromFrame(t *testing.T) { ...@@ -95,7 +95,7 @@ func TestSeriesFromFrame(t *testing.T) {
TimeIdx: 0, TimeIdx: 0,
TimeIsNullable: false, TimeIsNullable: false,
ValueIdx: 1, ValueIdx: 1,
ValueIsNullabe: false, ValueIsNullable: false,
}, },
}, },
{ {
...@@ -120,7 +120,7 @@ func TestSeriesFromFrame(t *testing.T) { ...@@ -120,7 +120,7 @@ func TestSeriesFromFrame(t *testing.T) {
TimeIdx: 1, TimeIdx: 1,
TimeIsNullable: true, TimeIsNullable: true,
ValueIdx: 0, ValueIdx: 0,
ValueIsNullabe: true, ValueIsNullable: true,
}, },
}, },
{ {
......
...@@ -35,8 +35,8 @@ func unconvert(m fluent.Matcher) { ...@@ -35,8 +35,8 @@ func unconvert(m fluent.Matcher) {
m.Match("float32($x)").Where(m["x"].Type.Is("float32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x") m.Match("float32($x)").Where(m["x"].Type.Is("float32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("float64($x)").Where(m["x"].Type.Is("float64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x") m.Match("float64($x)").Where(m["x"].Type.Is("float64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
// m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x") m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x")
// m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x") m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x")
m.Match("bool($x)").Where(m["x"].Type.Is("bool") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x") m.Match("bool($x)").Where(m["x"].Type.Is("bool") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int8($x)").Where(m["x"].Type.Is("int8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x") m.Match("int8($x)").Where(m["x"].Type.Is("int8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
...@@ -52,7 +52,6 @@ func unconvert(m fluent.Matcher) { ...@@ -52,7 +52,6 @@ func unconvert(m fluent.Matcher) {
m.Match("time.Duration($x)").Where(m["x"].Type.Is("time.Duration") && !m["x"].Text.Matches("^[0-9]*$")).Report("unnecessary conversion").Suggest("$x") m.Match("time.Duration($x)").Where(m["x"].Type.Is("time.Duration") && !m["x"].Text.Matches("^[0-9]*$")).Report("unnecessary conversion").Suggest("$x")
} }
/*
// Don't use == or != with time.Time // Don't use == or != with time.Time
// https://github.com/dominikh/go-tools/issues/47 : Wontfix // https://github.com/dominikh/go-tools/issues/47 : Wontfix
func timeeq(m fluent.Matcher) { func timeeq(m fluent.Matcher) {
...@@ -60,7 +59,6 @@ func timeeq(m fluent.Matcher) { ...@@ -60,7 +59,6 @@ func timeeq(m fluent.Matcher) {
m.Match("$t0 != $t1").Where(m["t0"].Type.Is("time.Time")).Report("using != with time.Time") m.Match("$t0 != $t1").Where(m["t0"].Type.Is("time.Time")).Report("using != with time.Time")
m.Match(`map[$k]$v`).Where(m["k"].Type.Is("time.Time")).Report("map with time.Time keys are easy to misuse") m.Match(`map[$k]$v`).Where(m["k"].Type.Is("time.Time")).Report("map with time.Time keys are easy to misuse")
} }
*/
// Wrong err in error check // Wrong err in error check
func wrongerr(m fluent.Matcher) { func wrongerr(m fluent.Matcher) {
...@@ -225,7 +223,6 @@ func ifreturn(m fluent.Matcher) { ...@@ -225,7 +223,6 @@ func ifreturn(m fluent.Matcher) {
} }
func oddifsequence(m fluent.Matcher) { func oddifsequence(m fluent.Matcher) {
/*
m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test") m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests") m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests")
...@@ -236,17 +233,14 @@ func oddifsequence(m fluent.Matcher) { ...@@ -236,17 +233,14 @@ func oddifsequence(m fluent.Matcher) {
m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests") m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests") m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests")
*/
} }
// odd sequence of nested if tests // odd sequence of nested if tests
func nestedifsequence(m fluent.Matcher) { func nestedifsequence(m fluent.Matcher) {
/*
m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests") m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests") m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests") m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests") m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
*/
} }
// odd sequence of assignments // odd sequence of assignments
...@@ -315,7 +309,6 @@ func floateq(m fluent.Matcher) { ...@@ -315,7 +309,6 @@ func floateq(m fluent.Matcher) {
m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }"). m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
Where(m["x"].Type.Is("float64")). Where(m["x"].Type.Is("float64")).
Report("floating point as switch expression") Report("floating point as switch expression")
} }
*/ */
...@@ -432,7 +425,6 @@ func nilerr(m fluent.Matcher) { ...@@ -432,7 +425,6 @@ func nilerr(m fluent.Matcher) {
} }
/*
func mailaddress(m fluent.Matcher) { func mailaddress(m fluent.Matcher) {
m.Match( m.Match(
"fmt.Sprintf(`\"%s\" <%s>`, $NAME, $EMAIL)", "fmt.Sprintf(`\"%s\" <%s>`, $NAME, $EMAIL)",
...@@ -447,7 +439,6 @@ func mailaddress(m fluent.Matcher) { ...@@ -447,7 +439,6 @@ func mailaddress(m fluent.Matcher) {
Report("use net/mail Address.String() instead of fmt.Sprintf()"). Report("use net/mail Address.String() instead of fmt.Sprintf()").
Suggest("(&mail.Address{Name:$NAME, Address:$EMAIL}).String()") Suggest("(&mail.Address{Name:$NAME, Address:$EMAIL}).String()")
} }
*/
func errnetclosed(m fluent.Matcher) { func errnetclosed(m fluent.Matcher) {
m.Match( m.Match(
...@@ -459,7 +450,6 @@ func errnetclosed(m fluent.Matcher) { ...@@ -459,7 +450,6 @@ func errnetclosed(m fluent.Matcher) {
} }
/*
func httpheaderadd(m fluent.Matcher) { func httpheaderadd(m fluent.Matcher) {
m.Match( m.Match(
`$H.Add($KEY, $VALUE)`, `$H.Add($KEY, $VALUE)`,
...@@ -468,7 +458,6 @@ func httpheaderadd(m fluent.Matcher) { ...@@ -468,7 +458,6 @@ func httpheaderadd(m fluent.Matcher) {
Report("use http.Header.Set method instead of Add to overwrite all existing header values"). Report("use http.Header.Set method instead of Add to overwrite all existing header values").
Suggest(`$H.Set($KEY, $VALUE)`) Suggest(`$H.Set($KEY, $VALUE)`)
} }
*/
func hmacnew(m fluent.Matcher) { func hmacnew(m fluent.Matcher) {
m.Match("hmac.New(func() hash.Hash { return $x }, $_)", m.Match("hmac.New(func() hash.Hash { return $x }, $_)",
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"html/template" "html/template"
"io" "io"
"net" "net"
"net/mail"
"strconv" "strconv"
"strings" "strings"
...@@ -175,10 +176,11 @@ func (ns *NotificationService) buildEmailMessage(cmd *models.SendEmailCommand) ( ...@@ -175,10 +176,11 @@ func (ns *NotificationService) buildEmailMessage(cmd *models.SendEmailCommand) (
subject = subjectBuffer.String() subject = subjectBuffer.String()
} }
addr := mail.Address{Name: ns.Cfg.Smtp.FromName, Address: ns.Cfg.Smtp.FromAddress}
return &Message{ return &Message{
To: cmd.To, To: cmd.To,
SingleEmail: cmd.SingleEmail, SingleEmail: cmd.SingleEmail,
From: fmt.Sprintf("%s <%s>", ns.Cfg.Smtp.FromName, ns.Cfg.Smtp.FromAddress), From: addr.String(),
Subject: subject, Subject: subject,
Body: buffer.String(), Body: buffer.String(),
EmbeddedFiles: cmd.EmbeddedFiles, EmbeddedFiles: cmd.EmbeddedFiles,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment