Skip to content

Commit 67780da

Browse files
authored
use-errors-new: disable for Go 1.26 (#1697)
1 parent 4a96bcf commit 67780da

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
lines changed

RULES_DESCRIPTIONS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,9 @@ _Configuration_: N/A
17081708

17091709
_Description_: This rule identifies calls to `fmt.Errorf` that can be safely replaced by, the more efficient, `errors.New`.
17101710

1711+
_Note_: This rule is irrelevant for Go 1.26+.
1712+
For unformatted strings, `fmt.Errorf("x")` generally [matches](https://go.dev/doc/go1.26#fmtpkgfmt) the allocations for `errors.New("x")`.
1713+
17111714
_Configuration_: N/A
17121715

17131716
## use-fmt-print

lint/package.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ var (
4444
Go124 = goversion.Must(goversion.NewVersion("1.24"))
4545
// Go125 is a constant representing the Go version 1.25.
4646
Go125 = goversion.Must(goversion.NewVersion("1.25"))
47+
// Go126 is a constant representing the Go version 1.26.
48+
Go126 = goversion.Must(goversion.NewVersion("1.26"))
4749
)
4850

4951
// Files return package's files.

rule/use_errors_new.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ type UseErrorsNewRule struct{}
1212

1313
// Apply applies the rule to given file.
1414
func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
15+
if file.Pkg.IsAtLeastGoVersion(lint.Go126) {
16+
// For unformatted strings in Go 1.26, fmt.Errorf matches the behavior of errors.New, so we can skip the analysis.
17+
return nil
18+
}
19+
1520
var failures []lint.Failure
1621

1722
walker := lintFmtErrorf{

test/use_errors_new_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import (
88

99
func TestUseErrorsNew(t *testing.T) {
1010
testRule(t, "use_errors_new", &rule.UseErrorsNewRule{})
11+
testRule(t, "go1.26/use_errors_new", &rule.UseErrorsNewRule{})
1112
}

testdata/go1.26/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/mgechev/revive/testdata
2+
3+
go 1.26

testdata/go1.26/use_errors_new.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pkg
2+
3+
import "fmt"
4+
5+
func errorsNew() (int, error) {
6+
fmt.Errorf("repo cannot be nil")
7+
errs := append(errs, fmt.Errorf("commit cannot be nil"))
8+
fmt.Errorf("unable to load base repo: %w", err)
9+
fmt.Errorf("Failed to get full commit id for origin/%s: %w", pr.BaseBranch, err)
10+
11+
return 0, fmt.Errorf(msg + "something")
12+
}

0 commit comments

Comments
 (0)