Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive for unnecessaryDefer #926

Closed
bombsimon opened this issue May 18, 2020 · 1 comment
Closed

False positive for unnecessaryDefer #926

bombsimon opened this issue May 18, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@bombsimon
Copy link
Contributor

bombsimon commented May 18, 2020

It seems like the check for unnecessaryDefer only checks if the defer statement is the last statement in a block, no matter what block it is. Because of this it's not possible to set conditional defer statements.

Noticed this when a build in golangci-lint failed. Here's a smaller example to reproduce.

package main

import "fmt"

func main() {
    if true {
        defer func() {
            fmt.Println("something was true")
        }()
    }

    fmt.Println("more code before return")
}
$ gocritic check -enable='unnecessaryDefer' ./...
./main.go:7:3: unnecessaryDefer: defer func(){...}(...) is placed just before return
@quasilyte quasilyte added the bug Something isn't working label May 18, 2020
bombsimon added a commit to bombsimon/go-critic that referenced this issue May 18, 2020
By keeping track of the block type we can differentiate between
functions and other blocks. This way we can enure that only blocks
ending with explicit return or end of functions yelds warnings.
quasilyte pushed a commit that referenced this issue May 18, 2020
By keeping track of the block type we can differentiate between
functions and other blocks. This way we can enure that only blocks
ending with explicit return or end of functions yelds warnings.
@quasilyte
Copy link
Member

Fixed in #927

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants