]> Cypherpunks repositories - gostls13.git/commitdiff
all: simplify multiple for loops
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 5 Mar 2019 20:44:29 +0000 (20:44 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 8 Mar 2019 14:29:19 +0000 (14:29 +0000)
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: Ian Lance Taylor <iant@golang.org>
13 files changed:
src/cmd/compile/internal/gc/pgen.go
src/cmd/compile/internal/gc/reflect.go
src/cmd/compile/internal/gc/walk.go
src/cmd/compile/internal/ssa/schedule.go
src/cmd/internal/obj/pass.go
src/cmd/link/internal/ld/elf.go
src/encoding/json/encode.go
src/encoding/json/stream_test.go
src/internal/reflectlite/type.go
src/reflect/type.go
src/runtime/traceback.go
src/runtime/type.go
src/text/template/funcs.go

index 6914e3c5f8844d618a0849c707b578192019f7be..5b9b6ce45e4826ae4ebcff388ba51e8a684c188d 100644 (file)
@@ -553,11 +553,9 @@ func createDwarfVars(fnsym *obj.LSym, fn *Func, automDecls []*Node) ([]*Node, []
                decls, vars, selected = createSimpleVars(automDecls)
        }
 
-       var dcl []*Node
+       dcl := automDecls
        if fnsym.WasInlined() {
                dcl = preInliningDcls(fnsym)
-       } else {
-               dcl = automDecls
        }
 
        // If optimization is enabled, the list above will typically be
index 8b058330ddee6c8ca30232af3d7e414f99d20335..03fbbb123d8838ede1ae88d6c98a78e718db4c88 100644 (file)
@@ -477,12 +477,10 @@ func dimportpath(p *types.Pkg) {
                return
        }
 
-       var str string
+       str := p.Path
        if p == localpkg {
                // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
                str = myimportpath
-       } else {
-               str = p.Path
        }
 
        s := Ctxt.Lookup("type..importpath." + p.Prefix + ".")
index 41a9d8e9dcc4706ee40fb97c4b34b74334c78a64..77f578197cef53581b973c836d2ea6be5ab90a8d 100644 (file)
@@ -3116,11 +3116,9 @@ func walkcompare(n *Node, init *Nodes) *Node {
        if l != nil {
                // Handle both == and !=.
                eq := n.Op
-               var andor Op
+               andor := OOROR
                if eq == OEQ {
                        andor = OANDAND
-               } else {
-                       andor = OOROR
                }
                // Check for types equal.
                // For empty interface, this is:
index c5b4c538434f97a1eb6666bd1aab12637c86ef1c..ca0e82953e7cb36ebb530f7ea7b48f5fa117bae6 100644 (file)
@@ -233,14 +233,10 @@ func schedule(f *Func) {
                // Schedule highest priority value, update use counts, repeat.
                order = order[:0]
                tuples := make(map[ID][]*Value)
-               for {
+               for priq.Len() > 0 {
                        // Find highest priority schedulable value.
                        // Note that schedule is assembled backwards.
 
-                       if priq.Len() == 0 {
-                               break
-                       }
-
                        v := heap.Pop(priq).(*Value)
 
                        // Add it to the schedule.
index 87de6a5fd145b4cfc32d97823090dbfd6ad46133..0c401710f61445b0be6b44e548033dc58fe42305 100644 (file)
@@ -141,10 +141,7 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
                        continue
                }
                q := sym.Func.Text
-               for q != nil {
-                       if p.To.Offset == q.Pc {
-                               break
-                       }
+               for q != nil && p.To.Offset != q.Pc {
                        if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
                                q = q.Forwd
                        } else {
index 19bcbbb87a6a25949fbe6279f3d474469855078b..3995a9423d3cce811994b565dc28718e33b2e7eb 100644 (file)
@@ -1290,11 +1290,9 @@ func elfshreloc(arch *sys.Arch, sect *sym.Section) *ElfShdr {
                return nil
        }
 
-       var typ int
+       typ := SHT_REL
        if elfRelType == ".rela" {
                typ = SHT_RELA
-       } else {
-               typ = SHT_REL
        }
 
        sh := elfshname(elfRelType + sect.Name)
index de6d2632f42a4fc21b5e1d38dc069f575494cb45..e3c5ffc9cb0103b09308c85f17f122eb5a6cbdb6 100644 (file)
@@ -1069,8 +1069,7 @@ func typeFields(t reflect.Type) []field {
        next := []field{{typ: t}}
 
        // Count of queued names for current level and the next.
-       count := map[reflect.Type]int{}
-       nextCount := map[reflect.Type]int{}
+       var count, nextCount map[reflect.Type]int
 
        // Types already visited at an earlier level.
        visited := map[reflect.Type]bool{}
index aaf32e0a24ce516dd71bb83b548f47c07e6aa70e..8dc74e54669f2cf1e97e973fd3657d1a6d0e1511 100644 (file)
@@ -296,7 +296,7 @@ type decodeThis struct {
        v interface{}
 }
 
-var tokenStreamCases []tokenStreamCase = []tokenStreamCase{
+var tokenStreamCases = []tokenStreamCase{
        // streaming token cases
        {json: `10`, expTokens: []interface{}{float64(10)}},
        {json: ` [10] `, expTokens: []interface{}{
index 35bc0db2c7f45c3374748174a3d41b6ff6f21fba..70c3723de79d01a07f5da02dd0371203ca765f5c 100644 (file)
@@ -510,10 +510,7 @@ func (t *rtype) Name() string {
        }
        s := t.String()
        i := len(s) - 1
-       for i >= 0 {
-               if s[i] == '.' {
-                       break
-               }
+       for i >= 0 && s[i] != '.' {
                i--
        }
        return s[i+1:]
index 5c7ed243d56144589a138665d5ba7b182966211f..b1df4f22fc07625d84251ed5c7e483206022817b 100644 (file)
@@ -875,10 +875,7 @@ func (t *rtype) Name() string {
        }
        s := t.String()
        i := len(s) - 1
-       for i >= 0 {
-               if s[i] == '.' {
-                       break
-               }
+       for i >= 0 && s[i] != '.' {
                i--
        }
        return s[i+1:]
index a536fb2a7166ccf41e6cd19ddb322bee29cd7c6d..0bb7fc2831c8da9d177c3f26a361c493c4965a5a 100644 (file)
@@ -1289,10 +1289,7 @@ func printCgoTraceback(callers *cgoCallers) {
 func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int {
        c := 0
        arg.pc = pc
-       for {
-               if c > max {
-                       break
-               }
+       for c <= max {
                callCgoSymbolizer(arg)
                if arg.funcName != nil {
                        // Note that we don't print any argument
index f7f99924eaf759bf63cacd087eb16b0e508f317f..dc7f62eff75d345a23c4b266cd83463f2ca81081 100644 (file)
@@ -118,10 +118,7 @@ func (t *_type) name() string {
        }
        s := t.string()
        i := len(s) - 1
-       for i >= 0 {
-               if s[i] == '.' {
-                       break
-               }
+       for i >= 0 && s[i] != '.' {
                i--
        }
        return s[i+1:]
index 72d3f666918d6569aa8b3cb9d207d1d42e22169d..a626247c2cc12bc2a11bd2188d422716909306a5 100644 (file)
@@ -263,11 +263,9 @@ func call(fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
        for i, arg := range args {
                value := indirectInterface(arg)
                // Compute the expected type. Clumsy because of variadics.
-               var argType reflect.Type
+               argType := dddType
                if !typ.IsVariadic() || i < numIn-1 {
                        argType = typ.In(i)
-               } else {
-                       argType = dddType
                }
 
                var err error