/httpreturnchecker

Primary LanguageGoMIT LicenseMIT

httpreturnchecker

httpreturnchecker is a static analysis tool that checks if you forget to correctly use a return after writing to the w variable - http.ResponseWriter

Why this?

Read this article http://lanre.wtf/blog/2024/08/25/http-handler-errors-golang

Install

go install github.com/adelowo/httpreturnchecker

How to use

go vet -vettool=$(which httpreturnchecker) path/to/file.go

Notices

It only handles scenarios like this:

  • fmt.Fprint family of functions
  • io.Copy
  • w.Write()
  • render.Render using github.com/go-chi/render

Example

// OK - return here
func handler1(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("hello"))
 return
}

// OK - last statement
func handler2(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("hello"))
}

// Bad - write with no return and not last statement
func handler3(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("hello"))
 someOtherOperation()
}