8000 Detect returning nil after `err != nil` check · Issue #863 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

Detect returning nil after err != nil check #863

Open

Description

@ilinum

I would like staticcheck to detect the following:

func foo() error {
  ...
  err := bar()
  if err != nil {
    return nil // BUG! Most of the time it should be `return err`
  }
  ...
}

Sometimes, of course, you want to explicitly return an error there, so this check will have quite a few false positives.
Most of the time, however, it will detect bugs.

Activity

  1. ilinum commented on Oct 24, 2020

    @ilinum
    Author

    I'd be happy to implement this but I would like to hear some input first with regards to whether or not this would cause too many false positives for the value that the check would provide.

  2. dominikh commented on Oct 24, 2020

    @dominikh
    Owner

    so this check will have quite a few false positives

    That, unfortunately, makes it ineligible for Staticcheck. We err on the side of false negatives, and checks are expected to have no false positives in the general case. As such, we can't just flag if err != nil { return nil }.

    This might be better suited for https://github.com/go-critic/go-critic

  3. added and removed
    needs-triageNewly filed issue that needs triage
    on Oct 24, 2020
  4. dgryski commented on Oct 24, 2020

    @dgryski

    This pattern is very easy to match with ruleguard or semgrep.

  5. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    on Oct 17, 2021
  6. dominikh commented on Oct 17, 2021

    @dominikh
    Owner

    Reopening under the aggressive label, for a future set of checks specific to code review.

  7. dominikh commented on Oct 19, 2021

    @dominikh
    Owner

    Running this against k8s.io/kubernetes/pkg/... yields

    • 6 false positives
    • 2 instances I'm really not sure about
    • 1 instance that is probably a true positive

    Do people see this kind of bug during code review, and how often?

  8. ainar-g commented on Oct 19, 2021

    @ainar-g

    Does the check that you run include something like an exception for branches with comments? For example, does it match code like:

    err = foo()
    if err != nil {
            // If foo returned an error, just return immediately,
            // since foo errors aren't critical.
            return nil
    }

    To answer your question directly, I've definitely seen this kind of bug a couple of times in code reviews and at least a dozen of times in the code that I wrote but haven't committed yet.

  9. dominikh commented on Oct 20, 2021

    @dominikh
    Owner

    Does the check that you run include something like an exception for branches with comments?

    That implementation did not. However, none of the results found in k8s had comments, either.

    I've definitely seen this kind of bug a couple of times in code reviews and at least a dozen of times in the code that I wrote but haven't committed yet.

    Thanks.

  10. dgryski commented on Oct 20, 2021

    @dgryski

    Same. For me the pattern occurs not as a direct typo, but as the result mis-think during refactoring when you change the if condition and forget to change the body because your brain already sees a return there.

  11. ainar-g commented on Oct 20, 2021

    @ainar-g

    That implementation did not. However, none of the results found in k8s had comments, either.

    I mean, if we're talking code review, I'm pretty sure that even if such code is correct, it should at least have a comment there saying so, so that the readers won't have to guess if it's actually correct or not. But opinions may differ here.

  12. added this to the Staticcheck 2022.2 milestone on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positive 4669 s but is helpful during code reviewnew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0