Commit 49662bc6 authored by Daniel Martí's avatar Daniel Martí

all: simplify multiple for loops

If a for loop has a simple condition and begins with a simple
"if x { break; }"; we can simply add "!x" to the loop's condition.

While at it, simplify a few assignments to use the common pattern
"x := staticDefault; if cond { x = otherValue(); }".

Finally, simplify a couple of var declarations.

Change-Id: I413982c6abd32905adc85a9a666cb3819139c19f
Reviewed-on: https://go-review.googlesource.com/c/go/+/165342
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent ce7534ff
...@@ -553,11 +553,9 @@ func createDwarfVars(fnsym *obj.LSym, fn *Func, automDecls []*Node) ([]*Node, [] ...@@ -553,11 +553,9 @@ func createDwarfVars(fnsym *obj.LSym, fn *Func, automDecls []*Node) ([]*Node, []
decls, vars, selected = createSimpleVars(automDecls) decls, vars, selected = createSimpleVars(automDecls)
} }
var dcl []*Node dcl := automDecls
if fnsym.WasInlined() { if fnsym.WasInlined() {
dcl = preInliningDcls(fnsym) dcl = preInliningDcls(fnsym)
} else {
dcl = automDecls
} }
// If optimization is enabled, the list above will typically be // If optimization is enabled, the list above will typically be
......
...@@ -477,12 +477,10 @@ func dimportpath(p *types.Pkg) { ...@@ -477,12 +477,10 @@ func dimportpath(p *types.Pkg) {
return return
} }
var str string str := p.Path
if p == localpkg { if p == localpkg {
// Note: myimportpath != "", or else dgopkgpath won't call dimportpath. // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
str = myimportpath str = myimportpath
} else {
str = p.Path
} }
s := Ctxt.Lookup("type..importpath." + p.Prefix + ".") s := Ctxt.Lookup("type..importpath." + p.Prefix + ".")
......
...@@ -3116,11 +3116,9 @@ func walkcompare(n *Node, init *Nodes) *Node { ...@@ -3116,11 +3116,9 @@ func walkcompare(n *Node, init *Nodes) *Node {
if l != nil { if l != nil {
// Handle both == and !=. // Handle both == and !=.
eq := n.Op eq := n.Op
var andor Op andor := OOROR
if eq == OEQ { if eq == OEQ {
andor = OANDAND andor = OANDAND
} else {
andor = OOROR
} }
// Check for types equal. // Check for types equal.
// For empty interface, this is: // For empty interface, this is:
......
...@@ -233,14 +233,10 @@ func schedule(f *Func) { ...@@ -233,14 +233,10 @@ func schedule(f *Func) {
// Schedule highest priority value, update use counts, repeat. // Schedule highest priority value, update use counts, repeat.
order = order[:0] order = order[:0]
tuples := make(map[ID][]*Value) tuples := make(map[ID][]*Value)
for { for priq.Len() > 0 {
// Find highest priority schedulable value. // Find highest priority schedulable value.
// Note that schedule is assembled backwards. // Note that schedule is assembled backwards.
if priq.Len() == 0 {
break
}
v := heap.Pop(priq).(*Value) v := heap.Pop(priq).(*Value)
// Add it to the schedule. // Add it to the schedule.
......
...@@ -141,10 +141,7 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) { ...@@ -141,10 +141,7 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
continue continue
} }
q := sym.Func.Text q := sym.Func.Text
for q != nil { for q != nil && p.To.Offset != q.Pc {
if p.To.Offset == q.Pc {
break
}
if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc { if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
q = q.Forwd q = q.Forwd
} else { } else {
......
...@@ -1290,11 +1290,9 @@ func elfshreloc(arch *sys.Arch, sect *sym.Section) *ElfShdr { ...@@ -1290,11 +1290,9 @@ func elfshreloc(arch *sys.Arch, sect *sym.Section) *ElfShdr {
return nil return nil
} }
var typ int typ := SHT_REL
if elfRelType == ".rela" { if elfRelType == ".rela" {
typ = SHT_RELA typ = SHT_RELA
} else {
typ = SHT_REL
} }
sh := elfshname(elfRelType + sect.Name) sh := elfshname(elfRelType + sect.Name)
......
...@@ -1069,8 +1069,7 @@ func typeFields(t reflect.Type) []field { ...@@ -1069,8 +1069,7 @@ func typeFields(t reflect.Type) []field {
next := []field{{typ: t}} next := []field{{typ: t}}
// Count of queued names for current level and the next. // Count of queued names for current level and the next.
count := map[reflect.Type]int{} var count, nextCount map[reflect.Type]int
nextCount := map[reflect.Type]int{}
// Types already visited at an earlier level. // Types already visited at an earlier level.
visited := map[reflect.Type]bool{} visited := map[reflect.Type]bool{}
......
...@@ -296,7 +296,7 @@ type decodeThis struct { ...@@ -296,7 +296,7 @@ type decodeThis struct {
v interface{} v interface{}
} }
var tokenStreamCases []tokenStreamCase = []tokenStreamCase{ var tokenStreamCases = []tokenStreamCase{
// streaming token cases // streaming token cases
{json: `10`, expTokens: []interface{}{float64(10)}}, {json: `10`, expTokens: []interface{}{float64(10)}},
{json: ` [10] `, expTokens: []interface{}{ {json: ` [10] `, expTokens: []interface{}{
......
...@@ -510,10 +510,7 @@ func (t *rtype) Name() string { ...@@ -510,10 +510,7 @@ func (t *rtype) Name() string {
} }
s := t.String() s := t.String()
i := len(s) - 1 i := len(s) - 1
for i >= 0 { for i >= 0 && s[i] != '.' {
if s[i] == '.' {
break
}
i-- i--
} }
return s[i+1:] return s[i+1:]
......
...@@ -875,10 +875,7 @@ func (t *rtype) Name() string { ...@@ -875,10 +875,7 @@ func (t *rtype) Name() string {
} }
s := t.String() s := t.String()
i := len(s) - 1 i := len(s) - 1
for i >= 0 { for i >= 0 && s[i] != '.' {
if s[i] == '.' {
break
}
i-- i--
} }
return s[i+1:] return s[i+1:]
......
...@@ -1289,10 +1289,7 @@ func printCgoTraceback(callers *cgoCallers) { ...@@ -1289,10 +1289,7 @@ func printCgoTraceback(callers *cgoCallers) {
func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int { func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int {
c := 0 c := 0
arg.pc = pc arg.pc = pc
for { for c <= max {
if c > max {
break
}
callCgoSymbolizer(arg) callCgoSymbolizer(arg)
if arg.funcName != nil { if arg.funcName != nil {
// Note that we don't print any argument // Note that we don't print any argument
......
...@@ -118,10 +118,7 @@ func (t *_type) name() string { ...@@ -118,10 +118,7 @@ func (t *_type) name() string {
} }
s := t.string() s := t.string()
i := len(s) - 1 i := len(s) - 1
for i >= 0 { for i >= 0 && s[i] != '.' {
if s[i] == '.' {
break
}
i-- i--
} }
return s[i+1:] return s[i+1:]
......
...@@ -263,11 +263,9 @@ func call(fn reflect.Value, args ...reflect.Value) (reflect.Value, error) { ...@@ -263,11 +263,9 @@ func call(fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
for i, arg := range args { for i, arg := range args {
value := indirectInterface(arg) value := indirectInterface(arg)
// Compute the expected type. Clumsy because of variadics. // Compute the expected type. Clumsy because of variadics.
var argType reflect.Type argType := dddType
if !typ.IsVariadic() || i < numIn-1 { if !typ.IsVariadic() || i < numIn-1 {
argType = typ.In(i) argType = typ.In(i)
} else {
argType = dddType
} }
var err error var err error
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment