Skip to content

Commit

Permalink
Merge pull request #56 from Antonboom/useless-assert
Browse files Browse the repository at this point in the history
new checker 'useless-assert'
  • Loading branch information
Antonboom authored Jan 24, 2024
2 parents 9918edc + 2bd66b8 commit 586e7ba
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 34 deletions.
18 changes: 11 additions & 7 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
version: 2

updates:
- package-ecosystem: github-actions
directory: /
update-types: semver-major
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
interval: "monthly"
ignore:
- dependency-name: "*"
update-types:
- "version-update:semver-minor"
- "version-update:semver-patch"

- package-ecosystem: gomod
directory: /
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: monthly
interval: "monthly"
48 changes: 22 additions & 26 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ Describe a new checker in [checkers section](./README.md#checkers).
- [float-compare](#float-compare)
- [http-const](#http-const)
- [http-sugar](#http-sugar)
- [inefficient-assert](#inefficient-assert)
- [negative-positive](#negative-positive)
- [no-fmt-mess](#no-fmt-mess)
- [require-len](#require-len)
- [suite-run](#suite-run)
- [suite-test-name](#suite-test-name)
- [useless-assert](#useless-assert)
- [zero](#zero)

---
Expand Down Expand Up @@ -299,31 +299,6 @@ And similar idea for `assert.InEpsilonSlice` / `assert.InDeltaSlice`.

---

### inefficient-assert

Simple:
```go
body, err := io.ReadAll(rr.Body)
require.NoError(t, err)
require.NoError(t, err) ❌

expectedJSON, err := json.Marshal(expected)
require.NoError(t, err)
require.JSONEq(t, string(expectedJSON), string(body))
```

Complex:
```go
require.NoError(t, err)
assert.ErrorContains(t, err, "user") ❌
```

**Autofix**: false. <br>
**Enabled by default**: Probably false, depends on implementation performance. <br>
**Reason**: Code simplification, elimination of possible bugs.

---

### negative-positive

```go
Expand Down Expand Up @@ -459,6 +434,27 @@ func (s *HandlersSuite) Test_UsecaseSuccess()

---

### useless-assert

Support more complex cases, e.g.

```go
body, err := io.ReadAll(rr.Body)
require.NoError(t, err)
require.NoError(t, err) ❌

expectedJSON, err := json.Marshal(expected)
require.NoError(t, err)
require.JSONEq(t, string(expectedJSON), string(body))
```

```go
require.NoError(t, err)
assert.ErrorContains(t, err, "user") ❌
```

---

### zero

```go
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ https://golangci-lint.run/usage/linters/#testifylint
| [suite-dont-use-pkg](#suite-dont-use-pkg) |||
| [suite-extra-assert-call](#suite-extra-assert-call) |||
| [suite-thelper](#suite-thelper) |||
| [useless-assert](#useless-assert) |||

> ⚠️ Also look at open for contribution [checkers](CONTRIBUTING.md#open-for-contribution)
Expand Down Expand Up @@ -253,6 +254,7 @@ logic, but without autofix.
assert.InDeltaMapValues(t, result, map[string]float64{"score": 0.99}, 1.0)
assert.InDeltaSlice(t, result, []float64{0.98, 0.99}, 1.0)
assert.InEpsilon(t, result, 42.42, 0.0001)
assert.InEpsilonSlice(t, result, []float64{0.9801, 0.9902}, 0.0001)
assert.IsType(t, result, (*User)(nil))
assert.NotEqual(t, result, "expected")
assert.NotEqualValues(t, result, "expected")
Expand Down Expand Up @@ -511,6 +513,24 @@ The checker rather acts as an example of a [checkers.AdvancedChecker](https://gi

---

### useless-assert

Currently the checker guards against assertion of the same variable:

```go
❌ assert.Equal(t, tt.value, tt.value)
assert.ElementsMatch(t, users, users)
// And so on...
```

More complex cases are [open for contribution](CONTRIBUTING.md#useless-assert).

**Autofix**: false. <br>
**Enabled by default**: true. <br>
**Reason**: Protection from bugs and possible dead code.

---

## Chain of warnings

Linter does not automatically handle the "evolution" of changes.
Expand Down
2 changes: 2 additions & 0 deletions analyzer/checkers_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewExpectedActual(),
checkers.NewSuiteExtraAssertCall(),
checkers.NewSuiteDontUsePkg(),
checkers.NewUselessAssert(),
}
allRegularCheckers := []checkers.RegularChecker{
checkers.NewFloatCompare(),
Expand All @@ -38,6 +39,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewExpectedActual(),
checkers.NewSuiteExtraAssertCall(),
checkers.NewSuiteDontUsePkg(),
checkers.NewUselessAssert(),
}

enabledByDefaultAdvancedCheckers := []checkers.AdvancedChecker{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func TestExpectedActualChecker_Other(t *testing.T) {
assert.InEpsilonf(t, expected, result, 0.0001, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilon(t, 42.42, result, 0.0001) // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilonf(t, 42.42, result, 0.0001, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilonSlice(t, expected, result, 0.0001) // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilonSlicef(t, expected, result, 0.0001, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilonSlice(t, []float64{0.9801, 0.9902}, result, 0.0001) // want "expected-actual: need to reverse actual and expected values"
assert.InEpsilonSlicef(t, []float64{0.9801, 0.9902}, result, 0.0001, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.IsType(t, expected, result) // want "expected-actual: need to reverse actual and expected values"
assert.IsTypef(t, expected, result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.IsType(t, user{}, result) // want "expected-actual: need to reverse actual and expected values"
Expand Down
Loading

0 comments on commit 586e7ba

Please sign in to comment.