8000 staticcheck: check for mistakenly returning a nil error · Issue #640 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

staticcheck: check for mistakenly returning a nil error #640

Open

Description

@FiloSottile

I caught this code in crypto/tls

	if sigType == 0 || err != nil {
		c.sendAlert(alertInternalError)
		return err
	}

You can see that if sigType == 0 && err == nil, we return nil, when in fact an error occurred.

This feels common enough and likely enough to be a mistake that staticcheck might raise it.

Activity

  1. ainar-g commented on Oct 29, 2019

    @ainar-g

    It looks like this will near-impossible to detect without generating lots of false-positives. A programmer could have easily decided to shorten this:

    if err != nil {
    	return err
    }
    
    if num == 0 {
    	return nil
    }

    Into this:

    if num == 0 || err != nil {
    	return err
    }

    Without it being an error.

  2. dominikh commented on Nov 5, 2019

    @dominikh
    Owner

    I think I agree with Ainar. It is impossible for us to determine which conditionals constitute an error condition and which constitute a short-circuit that carries a possibly non-nil error forward.

    This might be a good fit for a tool that reports "possibly wrong, look into this" diagnostics, but staticcheck tries to only flag things that are definitely wrong.

  3. added and removed
    needs-triageNewly filed issue that needs triage
    on Nov 5, 2019
  4. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    and removed on Oct 17, 2021
  5. dominikh commented on Oct 17, 2021

    @dominikh
    Owner

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

  6. 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 positives but is helpful during code reviewnew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0