Skip to content

Commit

Permalink
go/parser: clean up unnecessary arguments and replace an if statement
Browse files Browse the repository at this point in the history
Eliminate an unnecessary argument from parseGenericType, and replace an
if statement with a switch.

Change-Id: Iaa8afeface929332579f183c8e523961cca9aca4
Reviewed-on: https://go-review.googlesource.com/c/go/+/354869
Trust: Robert Findley <[email protected]>
Run-TryBot: Robert Findley <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
findleyr committed Oct 11, 2021
1 parent 59b2f51 commit 34f7b1f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/go/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2506,9 +2506,9 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, _ token.Pos, keyword toke
return spec
}

func (p *parser) parseGenericType(spec *ast.TypeSpec, openPos token.Pos, name0 *ast.Ident, closeTok token.Token) {
list := p.parseParameterList(name0, closeTok, p.parseParamDecl, true)
closePos := p.expect(closeTok)
func (p *parser) parseGenericType(spec *ast.TypeSpec, openPos token.Pos, name0 *ast.Ident) {
list := p.parseParameterList(name0, token.RBRACK, p.parseParamDecl, true)
closePos := p.expect(token.RBRACK)
spec.TypeParams = &ast.FieldList{Opening: openPos, List: list, Closing: closePos}
// Type alias cannot have type parameters. Accept them for robustness but complain.
if p.tok == token.ASSIGN {
Expand Down Expand Up @@ -2537,7 +2537,7 @@ func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Pos, _ token.Token
p.exprLev--
if name0, _ := x.(*ast.Ident); p.parseTypeParams() && name0 != nil && p.tok != token.RBRACK {
// generic type [T any];
p.parseGenericType(spec, lbrack, name0, token.RBRACK)
p.parseGenericType(spec, lbrack, name0)
} else {
// array type
// TODO(rfindley) should resolve all identifiers in x.
Expand Down Expand Up @@ -2619,18 +2619,19 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
results := p.parseResult()

var body *ast.BlockStmt
if p.tok == token.LBRACE {
switch p.tok {
case token.LBRACE:
body = p.parseBody()
p.expectSemi()
} else if p.tok == token.SEMICOLON {
case token.SEMICOLON:
p.next()
if p.tok == token.LBRACE {
// opening { of function declaration on next line
p.error(p.pos, "unexpected semicolon or newline before {")
body = p.parseBody()
p.expectSemi()
}
} else {
default:
p.expectSemi()
}

Expand Down

0 comments on commit 34f7b1f

Please sign in to comment.