Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable perfsprint in golangci-lint #1011

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linters:
- testifylint
- godot
- copyloopvar
- perfsprint

issues:
max-same-issues: 0
Expand Down
13 changes: 7 additions & 6 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -80,7 +81,7 @@ func actionCI(c *cli.Context) error {
}
currentBranch, err := git.CurrentBranch(git.RunGit)
if err != nil {
return fmt.Errorf("failed to get the name of current branch")
return errors.New("failed to get the name of current branch")
}
slog.Debug("Got branch information", slog.String("base", baseBranch), slog.String("current", currentBranch))
if currentBranch == strings.Split(baseBranch, "/")[len(strings.Split(baseBranch, "/"))-1] {
Expand Down Expand Up @@ -134,7 +135,7 @@ func actionCI(c *cli.Context) error {
if meta.cfg.Repository != nil && meta.cfg.Repository.BitBucket != nil {
token, ok := os.LookupEnv("BITBUCKET_AUTH_TOKEN")
if !ok {
return fmt.Errorf("BITBUCKET_AUTH_TOKEN env variable is required when reporting to BitBucket")
return errors.New("BITBUCKET_AUTH_TOKEN env variable is required when reporting to BitBucket")
}

timeout, _ := time.ParseDuration(meta.cfg.Repository.BitBucket.Timeout)
Expand All @@ -154,7 +155,7 @@ func actionCI(c *cli.Context) error {
if meta.cfg.Repository != nil && meta.cfg.Repository.GitLab != nil {
token, ok := os.LookupEnv("GITLAB_AUTH_TOKEN")
if !ok {
return fmt.Errorf("GITLAB_AUTH_TOKEN env variable is required when reporting to GitLab")
return errors.New("GITLAB_AUTH_TOKEN env variable is required when reporting to GitLab")
}

timeout, _ := time.ParseDuration(meta.cfg.Repository.GitLab.Timeout)
Expand All @@ -177,12 +178,12 @@ func actionCI(c *cli.Context) error {
if meta.cfg.Repository != nil && meta.cfg.Repository.GitHub != nil {
token, ok := os.LookupEnv("GITHUB_AUTH_TOKEN")
if !ok {
return fmt.Errorf("GITHUB_AUTH_TOKEN env variable is required when reporting to GitHub")
return errors.New("GITHUB_AUTH_TOKEN env variable is required when reporting to GitHub")
}

prVal, ok := os.LookupEnv("GITHUB_PULL_REQUEST_NUMBER")
if !ok {
return fmt.Errorf("GITHUB_PULL_REQUEST_NUMBER env variable is required when reporting to GitHub")
return errors.New("GITHUB_PULL_REQUEST_NUMBER env variable is required when reporting to GitHub")
}

var prNum int
Expand Down Expand Up @@ -234,7 +235,7 @@ func actionCI(c *cli.Context) error {
}

if problemsFound {
return fmt.Errorf("problems found")
return errors.New("problems found")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -58,7 +59,7 @@ func actionLint(c *cli.Context) error {

paths := c.Args().Slice()
if len(paths) == 0 {
return fmt.Errorf("at least one file or directory required")
return errors.New("at least one file or directory required")
}

slog.Info("Finding all rules to check", slog.Any("paths", paths))
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -123,7 +124,7 @@ func actionParse(c *cli.Context) (err error) {

parts := c.Args().Slice()
if len(parts) == 0 {
return fmt.Errorf("a query string is required")
return errors.New("a query string is required")
}
query := strings.Join(parts, " ")
return parseQuery(query)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func scanWorker(ctx context.Context, jobs <-chan scanJob, results chan<- reporte
Last: commentErr.Line,
},
Reporter: pintCommentReporter,
Text: fmt.Sprintf("This comment is not a valid pint control comment: %s", commentErr.Error()),
Text: "This comment is not a valid pint control comment: " + commentErr.Error(),
Severity: checks.Warning,
},
Owner: job.entry.Owner,
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var watchCmd = &cli.Command{

paths := c.Args().Slice()
if len(paths) == 0 {
return fmt.Errorf("at least one file or directory required")
return errors.New("at least one file or directory required")
}

slog.Debug("Starting glob watch", slog.Any("paths", paths))
Expand All @@ -72,7 +72,7 @@ var watchCmd = &cli.Command{

args := c.Args().Slice()
if len(args) != 1 {
return fmt.Errorf("exactly one argument required with the URI of Prometheus server to query")
return errors.New("exactly one argument required with the URI of Prometheus server to query")
}

gen := config.NewPrometheusGenerator(meta.cfg, prometheus.NewRegistry())
Expand Down
2 changes: 1 addition & 1 deletion internal/checks/alerts_annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c AnnotationCheck) checkValue(rule parser.Rule, value string, lines parser

func maybeComment(c string) string {
if c != "" {
return fmt.Sprintf("Rule comment: %s", c)
return "Rule comment: " + c
}
return ""
}
2 changes: 1 addition & 1 deletion internal/checks/promql_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func (c SeriesCheck) getMinAge(rule parser.Rule, selector promParser.VectorSelec
minAge = time.Hour * 2
bareSelector := stripLabels(selector)
prefixes := []string{
fmt.Sprintf("%s min-age ", c.Reporter()),
c.Reporter() + " min-age ",
fmt.Sprintf("%s(%s) min-age ", c.Reporter(), bareSelector.String()),
fmt.Sprintf("%s(%s) min-age ", c.Reporter(), selector.String()),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/checks/rule_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestRuleLinkCheck(t *testing.T) {
checker: func(_ *promapi.FailoverGroup) checks.RuleChecker {
return checks.NewRuleLinkCheck(
checks.MustTemplatedRegexp("http://(.*)"),
fmt.Sprintf(srv.URL+"/rewrite"),
srv.URL+"/rewrite",
time.Second,
map[string]string{"X-Host": "rewrite.example.com"},
"",
Expand Down
34 changes: 17 additions & 17 deletions internal/comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "this file"`),
Err: errors.New(`unexpected comment suffix: "this file"`),
}},
},
},
Expand All @@ -73,7 +73,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "this line"`),
Err: errors.New(`unexpected comment suffix: "this line"`),
}},
},
},
Expand All @@ -91,7 +91,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "here"`),
Err: errors.New(`unexpected comment suffix: "here"`),
}},
},
},
Expand All @@ -109,7 +109,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "here"`),
Err: errors.New(`unexpected comment suffix: "here"`),
}},
},
},
Expand All @@ -127,7 +127,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "here"`),
Err: errors.New(`unexpected comment suffix: "here"`),
}},
},
},
Expand All @@ -145,7 +145,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing file/owner value"),
Err: errors.New("missing file/owner value"),
}},
},
},
Expand All @@ -166,7 +166,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing rule/owner value"),
Err: errors.New("missing rule/owner value"),
}},
},
},
Expand All @@ -187,7 +187,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing file/disable value"),
Err: errors.New("missing file/disable value"),
}},
},
},
Expand All @@ -208,7 +208,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing disable value"),
Err: errors.New("missing disable value"),
}},
},
},
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing file/snooze value"),
Err: errors.New("missing file/snooze value"),
}},
},
},
Expand All @@ -250,7 +250,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
}},
},
},
Expand All @@ -262,7 +262,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
}},
},
},
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing snooze value"),
Err: errors.New("missing snooze value"),
}},
},
},
Expand All @@ -310,7 +310,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`),
}},
},
},
Expand All @@ -322,7 +322,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`),
}},
},
},
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf("missing rule/set value"),
Err: errors.New("missing rule/set value"),
}},
},
},
Expand Down Expand Up @@ -452,7 +452,7 @@ func TestParse(t *testing.T) {
Type: comments.InvalidComment,
Value: comments.Invalid{Err: comments.CommentError{
Line: 1,
Err: fmt.Errorf(`unexpected comment suffix: "# pint ignore/file"`),
Err: errors.New(`unexpected comment suffix: "# pint ignore/file"`),
}},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func getContext() *hcl.EvalContext {
vars := map[string]cty.Value{}
for _, e := range os.Environ() {
if k, v, ok := strings.Cut(e, "="); ok {
vars[fmt.Sprintf("ENV_%s", k)] = cty.StringVal(v)
vars["ENV_"+k] = cty.StringVal(v)
}
}
return &hcl.EvalContext{Variables: vars}
Expand Down
8 changes: 4 additions & 4 deletions internal/config/cost.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"fmt"
"errors"

"github.com/cloudflare/pint/internal/checks"
)
Expand All @@ -22,13 +22,13 @@ func (cs CostSettings) validate() error {
}
}
if cs.MaxSeries < 0 {
return fmt.Errorf("maxSeries value must be >= 0")
return errors.New("maxSeries value must be >= 0")
}
if cs.MaxTotalSamples < 0 {
return fmt.Errorf("maxTotalSamples value must be >= 0")
return errors.New("maxTotalSamples value must be >= 0")
}
if cs.MaxPeakSamples < 0 {
return fmt.Errorf("maxPeakSamples value must be >= 0")
return errors.New("maxPeakSamples value must be >= 0")
}
if cs.MaxEvaluationDuration != "" {
if _, err := parseDuration(cs.MaxEvaluationDuration); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/config/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (m Match) validate(allowEmpty bool) error {
}

if !allowEmpty && m.Path == "" && m.Name == "" && m.Kind == "" && m.Label == nil && m.Annotation == nil && m.Command == nil && m.For == "" {
return fmt.Errorf("ignore block must have at least one condition")
return errors.New("ignore block must have at least one condition")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type TLSConfig struct {

func (t TLSConfig) validate() error {
if (t.ClientCert != "") != (t.ClientKey != "") {
return fmt.Errorf("clientCert and clientKey must be set together")
return errors.New("clientCert and clientKey must be set together")
}
return nil
}
Expand Down
Loading
Loading