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

cmd/gofmt: rewrite might eat comments when given statement #6099

Closed
DanielMorsing opened this issue Aug 10, 2013 · 9 comments
Closed

cmd/gofmt: rewrite might eat comments when given statement #6099

DanielMorsing opened this issue Aug 10, 2013 · 9 comments

Comments

@DanielMorsing
Copy link
Contributor

run "gofmt -r 'x := s.foo[y] -> x := s.foo.Get(y)'" on the following file.

package main

type s struct {
    // my precious comment
    foo bool
}

func (*s) weird() bool {
    return false
}

The comment will be deleted and the return declaration will be put in parens. Like so:

package main

type s struct {
    foo bool
}

func (*s) weird() (bool) {
    return false
}

This happened on a much larger program and I had to spend some time restoring all the
comments that were eaten.
@dominikh
Copy link
Member

Comment 2:

2-3 Two things at play here.
1) https://golang.org/cl/6294076/ added removal of comments to gofmt -r. It
only keeps comments for which a node exists in the new AST
2) 'x := s.foo[y] -> x := s.foo.Get(y)' seems to get interpreted as 'x -> x', replacing
every identifier/expression with itself
3) Due to the way equality is checked (map lookup with ast.Node's as keys), the
replacements aren't equal to the old nodes, causing all attached comments to be lost.

@dominikh
Copy link
Member

Comment 3:

Considering 2), it would probably already go a long way to refuse patterns that don't
get fully consumed during parsing, since that means they don't express what the user
thought they did.

@rsc
Copy link
Contributor

rsc commented Sep 10, 2013

Comment 4:

Labels changed: removed go1.2maybe.

@griesemer
Copy link
Contributor

Comment 5:

Owner changed to @griesemer.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 6:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 7:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 8:

Labels changed: added repo-main.

@griesemer
Copy link
Contributor

Comment 9:

This is partly addressed with https://golang.org/cl/68920044 :
gofmt -r 'x := s.foo[y] -> x := s.foo.Get(y)'
now complains that the expression pattern is incorrect. Thanks to dominik.honnef for
analyzing the case.

@griesemer
Copy link
Contributor

Comment 10:

The before mentioned CL fixes the confusion (per dominik.honnef's suggestion.
Closing this in favor of the more specific issue #7417.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants