1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/revivelib/core_test.go
Robert Liebowitz 2b4286ede2
Drop if-return from default ruleset (#843)
The `if-return` rule was originally a golint rule which was removed
from their ruleset for being out of scope. Similarly, it was dropped
from revive intentionally as a result of #537. More recently, it was
reintroduced into the default ruleset as a result of #799 due to a
discrepancy in documentation without a discussion of whether this rule
in particular belonged as a part of that default rule set.

While it is no longer a goal of this project to align 100% with the
golint defaults, I believe that this rule gives bad advice often enough
that it should not be enabled by default.

For example, consider the following code:

```go
if err := func1(); err != nil {
	return err
}

if err := func2(); err != nil {
	return err
}

if err := func3(); err != nil {
	return err
}

return nil
```

The `if-return` rule considers this a violation of style, and instead
suggests the following:

```go
if err := func1(); err != nil {
	return err
}

if err := func2(); err != nil {
	return err
}

return func3()
```

While this is more terse, it has a few shortcomings compared to the
original. In particular, it means extending the size of the diff if
changing the order of checks, adding logic after the call that currently
happens to be last, or choosing to wrap the error. And in that last
case, it can make it less obvious that there is an unwrapped error being
propagated up the call stack.

This in practice has a very similar effect to disabling trailing commas;
while it is not necessarily wrong as a style choice, I don't believe it
warrants a position as part of the default ruleset here.

See-also: https://github.com/golang/lint/issues/363
2023-06-26 09:43:19 -07:00

104 lines
2.4 KiB
Go

package revivelib_test
import (
"strings"
"testing"
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/revivelib"
"github.com/mgechev/revive/rule"
)
func TestReviveLint(t *testing.T) {
// ARRANGE
revive := getMockRevive(t)
// ACT
failures, err := revive.Lint(revivelib.Include("../testdata/if-return.go"))
if err != nil {
t.Fatal(err)
}
// ASSERT
failureList := []lint.Failure{}
for failure := range failures {
failureList = append(failureList, failure)
}
const expected = 5
got := len(failureList)
if got != expected {
t.Fatalf("Expected failures to have %d failures, but it has %d.", expected, got)
}
}
func TestReviveFormat(t *testing.T) {
// ARRANGE
revive := getMockRevive(t)
failuresChan, err := revive.Lint(revivelib.Include("../testdata/if-return.go"))
if err != nil {
t.Fatal(err)
}
// ACT
failures, exitCode, err := revive.Format("stylish", failuresChan)
// ASSERT
if err != nil {
t.Fatal(err)
}
errorMsgs := []string{
"(91, 3) https://revive.run/r#unreachable-code unreachable code after this statement",
"(98, 3) https://revive.run/r#unreachable-code unreachable code after this statement",
"(15, 2) https://revive.run/r#if-return redundant if ...; err != nil check, just return error instead.",
"(88, 3) https://revive.run/r#if-return redundant if ...; err != nil check, just return error instead.",
"(95, 3) https://revive.run/r#if-return redundant if ...; err != nil check, just return error instead.",
}
for _, errorMsg := range errorMsgs {
if !strings.Contains(failures, errorMsg) {
t.Fatalf("Expected formatted failures\n'%s'\nto contain\n'%s', but it didn't.", failures, errorMsg)
}
}
const expected = 1
if exitCode != expected {
t.Fatalf("Expected exit code to be %d, but it was %d.", expected, exitCode)
}
}
type mockRule struct{}
func (r *mockRule) Name() string {
return "mock-rule"
}
func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
return nil
}
func getMockRevive(t *testing.T) *revivelib.Revive {
t.Helper()
conf, err := config.GetConfig("../defaults.toml")
if err != nil {
t.Fatal(err)
}
revive, err := revivelib.New(
conf,
true,
2048,
revivelib.NewExtraRule(&rule.IfReturnRule{}, lint.RuleConfig{}),
revivelib.NewExtraRule(&mockRule{}, lint.RuleConfig{}),
)
if err != nil {
t.Fatal(err.Error())
}
return revive
}