Skip to content

Conversation

bombsimon
Copy link
Owner

@bombsimon bombsimon commented Jul 1, 2025

Only perform error checking when there's a single comparison with a binary operator comparing an error with nil. Both != and == are candidates for cuddling.

These should be cuddled if err exist on the line above

if err != nil {}

if err == nil {}

Nothing else should trigger this lint, all of these should be left

// More than error checking
if err != nil || true {}

// Technically an error checking, but we only check for binary
// operators, not expressions.
if errors.Is(err, SomeErr) {}

// More than one error checking
if err != nil || err2 != nil {}

This also matches gofumpt which has this rule:

No empty lines before a simple error check

foo, err := processFoo()

if err != nil {
	return err
}
foo, err := processFoo()
if err != nil {
	return err
}

Solves #183

Only perform error checking when there's a single comparison with a
binary operator comparing an error with `nil`. Both `!=` and `==` are
candidates for cuddling.

These should be cuddled if `err` exist on the line above

```go
if err != nil {}

if err == nil {}
```

Nothing else should trigger this lint, all of these should be left

```go
// More than error checking
if err != nil || true {}

// Technically an error checking, but we only check for binary
// operators, not expressions.
if errors.Is(err, SomeErr) {}

// More than one error checking
if err != nil || err2 != nil {}
```

This also matches [`gofumpt`](https://github.com/mvdan/gofumpt) which
has this rule:

> No empty lines before a simple error check
>
> ```go
> foo, err := processFoo()
>
> if err != nil {
> 	return err
> }
> ```
>
> ```go
> foo, err := processFoo()
> if err != nil {
> 	return err
> }
> ```
@coveralls
Copy link

Coverage Status

coverage: 93.837% (-0.6%) from 94.44%
when pulling 0535abb on lax-error-checking
into 628ebb7 on main.

@bombsimon bombsimon merged commit ebb08fc into main Jul 2, 2025
4 checks passed
@bombsimon bombsimon deleted the lax-error-checking branch July 2, 2025 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants