Skip to content

jakobmoellerdev/deferrlint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deferrlint

A static analysis tool for Go that checks for incorrect usage of defer statements, specifically assignments to error-typed variables that are not named return values in deferred functions.

Features

  • Detects deferred functions that assign to error variables not declared as named return values.
  • Helps avoid the common pitfall of using defer incorrectly with errors, which can lead to unexpected behavior in error handling.

Installation

You can build deferrlint from source and install it locally:

Make sure you have Go and Task installed on your machine.

git clone https://github.com/yourusername/deferrlint.git
cd deferrlint
task install

Usage

Run deferrlint on your Go codebase:

go run main.go ./...

Or, if you built the binary with task install, you can run it directly:

deferrlint ./...

You can verify deferrlint is installed correctly by running:

deferrlint ./testdata/src/fail/...

Example

package main

import (
	"errors"
)

func foo() (err error) {
	defer func() {
		err = errors.New("foo") // OK: err is a named return value
	}()
	
	return err
}

func bar() error {
	var err error
	defer func() {
		err = errors.New("bar") // Not OK: err is not a named return value
	}()
	return err
}

deferrlint will report the assignment in bar as a potential issue.

Contributing

Pull requests and issues are welcome!

License

See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages