Skip to content

cmd/vet: warn on 3-clause loops where an iterator variable contains a lock type when GoVersion>=1.22 #66387

Closed

Description

@timothy-king

Proposal Details

Extend the copylock analyzer to emit a warning on the copy of the loop variable between iterations that occurs immediately before the loop's Post statement in Go >= 1.22. The requirements for the warning are when:

  • a 3-clause for loop declares a loop scoped variable containing a lock type,
  • the file's GoVersion is >= 1.22,
  • the name of the variable is not _, and
  • the Init statement of the for loop is not otherwise reported.

Example:

for _, mu := 0, (sync.Mutex{}); x < 10; x++ { // want "for loop iteration copies lock value to mu: sync.Mutex"
	_ = mu.TryLock()
}

Relevant part of the spec https://go.dev/ref/spec#For_clause :

The variable used by each subsequent iteration is declared implicitly before executing the post statement and initialized to the value of the previous iteration's variable at that moment.

Activity

  1. timothy-king commented on Mar 18, 2024

    @timothy-king
    ContributorAuthor

    Proposed implementation http://go.dev/cl/569955.

    Related #66156. This is 'Case 1'.

  2. changed the title [-]cmd/vet: warn on 3-clause loops where an iterator variable contains a lock type when GoVersion>1.22[/-] [+]cmd/vet: warn on 3-clause loops where an iterator variable contains a lock type when GoVersion>=1.22[/+] on Mar 18, 2024
  3. changed the title [-]cmd/vet: warn on 3-clause loops where an iterator variable contains a lock type when GoVersion>=1.22[/-] [+]proposal: cmd/vet: warn on 3-clause loops where an iterator variable contains a lock type when GoVersion>=1.22[/+] on Mar 27, 2024
  4. moved this to Incoming in Proposalson Mar 27, 2024
  5. added this to the Proposal milestone on Mar 27, 2024
  6. gopherbot commented on May 8, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/569955 mentions this issue: go/analysis/passes/copylock: add support for ForStmt

  7. rsc commented on Jul 25, 2024

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  8. moved this from Incoming to Active in Proposalson Jul 25, 2024
  9. rsc commented on Jul 25, 2024

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  10. moved this from Active to Likely Accept in Proposalson Aug 7, 2024
  11. rsc commented on Aug 7, 2024

    @rsc
    Contributor

    Based on the discussion above, this proposal seems like a likely accept.
    — rsc for the proposal review group

    The proposal details are in #66387 (comment).

  12. moved this from Likely Accept to Accepted in Proposalson Aug 14, 2024
  13. 8 remaining items

  14. timothy-king commented on Aug 16, 2024

    @timothy-king
    ContributorAuthor

    This is expected to go into tip during the next x/tools. Probably not more than a month. The first release it is expected to go into is 1.24.

  15. modified the milestones: Backlog, Go1.24 on Nov 26, 2024
  16. dmitshur commented on Nov 26, 2024

    @dmitshur
    Member

    This vet change seems like something that needs to be mentioned in Go 1.24 release notes, and it's not mentioned there yet. Reopening as a release blocker to track that.

  17. gopherbot commented on Nov 27, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/632315 mentions this issue: doc/next: document copylock changes

  18. added a commit that references this issue on Nov 27, 2024
    b7e450b
  19. added a commit that references this issue on Feb 13, 2025
    dfcdf50
  20. locked and limited conversation to collaborators on Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions