8000 simple: flag unnecessary assignment to the blank identifier · Issue #253 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

simple: flag unnecessary assignment to the blank identifier #253

Open

Description

@dominikh

While debugging code, one (at least I do) often temporarily stops using a variable and assigns it to the blank identifier to shut up the compiler. For example:

x1, x2 := fn()
_ = x1
// use(x1)
use(x2)

As debugging continues and code is uncommented, this may result in

x1, x2 := fn()
_ = x1
use(x1)
use(x2)

with a leftover, unnecessary assignment to _. It might be desirable to flag this.

Activity

  1. dmitshur commented on Jan 17, 2018

    @dmitshur
    SponsorContributor

    One thing that immediately comes to mind is that you’ll want to be careful about expressions that may panic. Removing them would change program behavior (potentially significantly, if there’d be no panic otherwise).

    There are some false positives to avoid, such as “early bound checks”, e.g.:

    https://gotools.org/encoding/binary#binary.go-L68

    Otherwise, this seems like a nice simple check. (That said, personally, I don’t often find myself leaving such unnecessary assignments to blank identifier after finishing debugging or development.)

  2. dominikh commented on Jan 18, 2018

    @dominikh
    OwnerAuthor

    We'll limit it to identifiers that are local variables; no function calls or indexing or the like. That should get rid of any false positives.

  3. mvdan commented on Jan 18, 2018

    @mvdan
    Contributor

    I have forgotten about these a couple of times myself too. If the decision is about "is this useful", it has my thumbs up.

    Perhaps print and println should be in the same boat somehow, as in general they should never be used in production code and tend to be u 55F2 sed for debugging. But those tend to be harder to miss, as you are likely to see the debug output.

  4. removed
    needs-decisionWe have to decide if this check is feasible and desirable
    on Jan 21, 2018
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0