Browse Source

*: clean up bool comparison

Gyu-Ho Lee 9 years ago
parent
commit
c09f23c46d

+ 1 - 1
contrib/raftexample/raft.go

@@ -133,7 +133,7 @@ func (rc *raftNode) publishEntries(ents []raftpb.Entry) bool {
 
 
 // openWAL returns a WAL ready for reading.
 // openWAL returns a WAL ready for reading.
 func (rc *raftNode) openWAL() *wal.WAL {
 func (rc *raftNode) openWAL() *wal.WAL {
-	if wal.Exist(rc.waldir) == false {
+	if !wal.Exist(rc.waldir) {
 		if err := os.Mkdir(rc.waldir, 0750); err != nil {
 		if err := os.Mkdir(rc.waldir, 0750); err != nil {
 			log.Fatalf("raftexample: cannot create dir for wal (%v)", err)
 			log.Fatalf("raftexample: cannot create dir for wal (%v)", err)
 		}
 		}

+ 1 - 1
contrib/recipes/client.go

@@ -36,7 +36,7 @@ func deleteRevKey(kv v3.KV, key string, rev int64) (bool, error) {
 	txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit()
 	txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit()
 	if err != nil {
 	if err != nil {
 		return false, err
 		return false, err
-	} else if txnresp.Succeeded == false {
+	} else if !txnresp.Succeeded {
 		return false, nil
 		return false, nil
 	}
 	}
 	return true, nil
 	return true, nil

+ 2 - 2
contrib/recipes/key.go

@@ -84,7 +84,7 @@ func putNewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (int64, error) {
 	if err != nil {
 	if err != nil {
 		return 0, err
 		return 0, err
 	}
 	}
-	if txnresp.Succeeded == false {
+	if !txnresp.Succeeded {
 		return 0, ErrKeyExists
 		return 0, ErrKeyExists
 	}
 	}
 	return txnresp.Header.Revision, nil
 	return txnresp.Header.Revision, nil
@@ -132,7 +132,7 @@ func newSequentialKV(kv v3.KV, prefix, val string, leaseID v3.LeaseID) (*RemoteK
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	if txnresp.Succeeded == false {
+	if !txnresp.Succeeded {
 		return newSequentialKV(kv, prefix, val, leaseID)
 		return newSequentialKV(kv, prefix, val, leaseID)
 	}
 	}
 	return &RemoteKV{kv, newKey, txnresp.Header.Revision, val}, nil
 	return &RemoteKV{kv, newKey, txnresp.Header.Revision, val}, nil

+ 2 - 2
discovery/discovery_test.go

@@ -410,14 +410,14 @@ func TestSortableNodes(t *testing.T) {
 	for _, n := range sns.Nodes {
 	for _, n := range sns.Nodes {
 		cis = append(cis, int(n.CreatedIndex))
 		cis = append(cis, int(n.CreatedIndex))
 	}
 	}
-	if sort.IntsAreSorted(cis) != true {
+	if !sort.IntsAreSorted(cis) {
 		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
 		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
 	}
 	}
 	cis = make([]int, 0)
 	cis = make([]int, 0)
 	for _, n := range ns {
 	for _, n := range ns {
 		cis = append(cis, int(n.CreatedIndex))
 		cis = append(cis, int(n.CreatedIndex))
 	}
 	}
-	if sort.IntsAreSorted(cis) != true {
+	if !sort.IntsAreSorted(cis) {
 		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
 		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
 	}
 	}
 }
 }

+ 5 - 3
pkg/adt/interval_tree.go

@@ -27,10 +27,12 @@ type Comparable interface {
 	Compare(c Comparable) int
 	Compare(c Comparable) int
 }
 }
 
 
-type rbcolor bool
+type rbcolor int
 
 
-const black = true
-const red = false
+const (
+	black rbcolor = iota
+	red
+)
 
 
 // Interval implements a Comparable interval [begin, end)
 // Interval implements a Comparable interval [begin, end)
 // TODO: support different sorts of intervals: (a,b), [a,b], (a, b]
 // TODO: support different sorts of intervals: (a,b), [a,b], (a, b]

+ 1 - 4
raft/log.go

@@ -152,10 +152,7 @@ func (l *raftLog) nextEnts() (ents []pb.Entry) {
 // is a fast check without heavy raftLog.slice() in raftLog.nextEnts().
 // is a fast check without heavy raftLog.slice() in raftLog.nextEnts().
 func (l *raftLog) hasNextEnts() bool {
 func (l *raftLog) hasNextEnts() bool {
 	off := max(l.applied+1, l.firstIndex())
 	off := max(l.applied+1, l.firstIndex())
-	if l.committed+1 > off {
-		return true
-	}
-	return false
+	return l.committed+1 > off
 }
 }
 
 
 func (l *raftLog) snapshot() (pb.Snapshot, error) {
 func (l *raftLog) snapshot() (pb.Snapshot, error) {

+ 3 - 3
raft/log_test.go

@@ -243,7 +243,7 @@ func TestLogMaybeAppend(t *testing.T) {
 		func() {
 		func() {
 			defer func() {
 			defer func() {
 				if r := recover(); r != nil {
 				if r := recover(); r != nil {
-					if tt.wpanic != true {
+					if !tt.wpanic {
 						t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
 						t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
 					}
 					}
 				}
 				}
@@ -455,7 +455,7 @@ func TestCommitTo(t *testing.T) {
 		func() {
 		func() {
 			defer func() {
 			defer func() {
 				if r := recover(); r != nil {
 				if r := recover(); r != nil {
-					if tt.wpanic != true {
+					if !tt.wpanic {
 						t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
 						t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
 					}
 					}
 				}
 				}
@@ -548,7 +548,7 @@ func TestCompaction(t *testing.T) {
 		func() {
 		func() {
 			defer func() {
 			defer func() {
 				if r := recover(); r != nil {
 				if r := recover(); r != nil {
-					if tt.wallow == true {
+					if tt.wallow {
 						t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
 						t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
 					}
 					}
 				}
 				}

+ 3 - 3
raft/raft_paper_test.go

@@ -88,7 +88,7 @@ func TestRejectStaleTermMessage(t *testing.T) {
 
 
 	r.Step(pb.Message{Type: pb.MsgApp, Term: r.Term - 1})
 	r.Step(pb.Message{Type: pb.MsgApp, Term: r.Term - 1})
 
 
-	if called == true {
+	if called {
 		t.Errorf("stepFunc called = %v, want %v", called, false)
 		t.Errorf("stepFunc called = %v, want %v", called, false)
 	}
 	}
 }
 }
@@ -169,7 +169,7 @@ func testNonleaderStartElection(t *testing.T, state StateType) {
 	if r.state != StateCandidate {
 	if r.state != StateCandidate {
 		t.Errorf("state = %s, want %s", r.state, StateCandidate)
 		t.Errorf("state = %s, want %s", r.state, StateCandidate)
 	}
 	}
-	if r.votes[r.id] != true {
+	if !r.votes[r.id] {
 		t.Errorf("vote for self = false, want true")
 		t.Errorf("vote for self = false, want true")
 	}
 	}
 	msgs := r.readMessages()
 	msgs := r.readMessages()
@@ -326,7 +326,7 @@ func testNonleaderElectionTimeoutRandomized(t *testing.T, state StateType) {
 	}
 	}
 
 
 	for d := et + 1; d < 2*et; d++ {
 	for d := et + 1; d < 2*et; d++ {
-		if timeouts[d] != true {
+		if !timeouts[d] {
 			t.Errorf("timeout in %d ticks should happen", d)
 			t.Errorf("timeout in %d ticks should happen", d)
 		}
 		}
 	}
 	}

+ 2 - 2
raft/raft_snap_test.go

@@ -83,7 +83,7 @@ func TestSnapshotFailure(t *testing.T) {
 	if sm.prs[2].Next != 1 {
 	if sm.prs[2].Next != 1 {
 		t.Fatalf("Next = %d, want 1", sm.prs[2].Next)
 		t.Fatalf("Next = %d, want 1", sm.prs[2].Next)
 	}
 	}
-	if sm.prs[2].Paused != true {
+	if !sm.prs[2].Paused {
 		t.Errorf("Paused = %v, want true", sm.prs[2].Paused)
 		t.Errorf("Paused = %v, want true", sm.prs[2].Paused)
 	}
 	}
 }
 }
@@ -106,7 +106,7 @@ func TestSnapshotSucceed(t *testing.T) {
 	if sm.prs[2].Next != 12 {
 	if sm.prs[2].Next != 12 {
 		t.Fatalf("Next = %d, want 12", sm.prs[2].Next)
 		t.Fatalf("Next = %d, want 12", sm.prs[2].Next)
 	}
 	}
-	if sm.prs[2].Paused != true {
+	if !sm.prs[2].Paused {
 		t.Errorf("Paused = %v, want true", sm.prs[2].Paused)
 		t.Errorf("Paused = %v, want true", sm.prs[2].Paused)
 	}
 	}
 }
 }

+ 9 - 9
raft/raft_test.go

@@ -250,12 +250,12 @@ func TestProgressResume(t *testing.T) {
 		Paused: true,
 		Paused: true,
 	}
 	}
 	p.maybeDecrTo(1, 1)
 	p.maybeDecrTo(1, 1)
-	if p.Paused != false {
+	if p.Paused {
 		t.Errorf("paused= %v, want false", p.Paused)
 		t.Errorf("paused= %v, want false", p.Paused)
 	}
 	}
 	p.Paused = true
 	p.Paused = true
 	p.maybeUpdate(2)
 	p.maybeUpdate(2)
-	if p.Paused != false {
+	if p.Paused {
 		t.Errorf("paused= %v, want false", p.Paused)
 		t.Errorf("paused= %v, want false", p.Paused)
 	}
 	}
 }
 }
@@ -268,7 +268,7 @@ func TestProgressResumeByHeartbeat(t *testing.T) {
 	r.prs[2].Paused = true
 	r.prs[2].Paused = true
 
 
 	r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgBeat})
 	r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgBeat})
-	if r.prs[2].Paused != false {
+	if r.prs[2].Paused {
 		t.Errorf("paused = %v, want false", r.prs[2].Paused)
 		t.Errorf("paused = %v, want false", r.prs[2].Paused)
 	}
 	}
 }
 }
@@ -792,7 +792,7 @@ func TestStepIgnoreOldTermMsg(t *testing.T) {
 	sm.step = fakeStep
 	sm.step = fakeStep
 	sm.Term = 2
 	sm.Term = 2
 	sm.Step(pb.Message{Type: pb.MsgApp, Term: sm.Term - 1})
 	sm.Step(pb.Message{Type: pb.MsgApp, Term: sm.Term - 1})
-	if called == true {
+	if called {
 		t.Errorf("stepFunc called = %v , want %v", called, false)
 		t.Errorf("stepFunc called = %v , want %v", called, false)
 	}
 	}
 }
 }
@@ -1092,7 +1092,7 @@ func TestStateTransition(t *testing.T) {
 		func() {
 		func() {
 			defer func() {
 			defer func() {
 				if r := recover(); r != nil {
 				if r := recover(); r != nil {
-					if tt.wallow == true {
+					if tt.wallow {
 						t.Errorf("%d: allow = %v, want %v", i, false, true)
 						t.Errorf("%d: allow = %v, want %v", i, false, true)
 					}
 					}
 				}
 				}
@@ -1416,7 +1416,7 @@ func TestSendAppendForProgressProbe(t *testing.T) {
 			t.Errorf("index = %d, want %d", msg[0].Index, 0)
 			t.Errorf("index = %d, want %d", msg[0].Index, 0)
 		}
 		}
 
 
-		if r.prs[2].Paused != true {
+		if !r.prs[2].Paused {
 			t.Errorf("paused = %v, want true", r.prs[2].Paused)
 			t.Errorf("paused = %v, want true", r.prs[2].Paused)
 		}
 		}
 		for j := 0; j < 10; j++ {
 		for j := 0; j < 10; j++ {
@@ -1686,7 +1686,7 @@ func TestStepConfig(t *testing.T) {
 	if g := r.raftLog.lastIndex(); g != index+1 {
 	if g := r.raftLog.lastIndex(); g != index+1 {
 		t.Errorf("index = %d, want %d", g, index+1)
 		t.Errorf("index = %d, want %d", g, index+1)
 	}
 	}
-	if r.pendingConf != true {
+	if !r.pendingConf {
 		t.Errorf("pendingConf = %v, want true", r.pendingConf)
 		t.Errorf("pendingConf = %v, want true", r.pendingConf)
 	}
 	}
 }
 }
@@ -1759,7 +1759,7 @@ func TestAddNode(t *testing.T) {
 	r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage())
 	r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage())
 	r.pendingConf = true
 	r.pendingConf = true
 	r.addNode(2)
 	r.addNode(2)
-	if r.pendingConf != false {
+	if r.pendingConf {
 		t.Errorf("pendingConf = %v, want false", r.pendingConf)
 		t.Errorf("pendingConf = %v, want false", r.pendingConf)
 	}
 	}
 	nodes := r.nodes()
 	nodes := r.nodes()
@@ -1775,7 +1775,7 @@ func TestRemoveNode(t *testing.T) {
 	r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage())
 	r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage())
 	r.pendingConf = true
 	r.pendingConf = true
 	r.removeNode(2)
 	r.removeNode(2)
-	if r.pendingConf != false {
+	if r.pendingConf {
 		t.Errorf("pendingConf = %v, want false", r.pendingConf)
 		t.Errorf("pendingConf = %v, want false", r.pendingConf)
 	}
 	}
 	w := []uint64{1}
 	w := []uint64{1}

+ 10 - 10
rafthttp/stream_test.go

@@ -38,7 +38,7 @@ import (
 func TestStreamWriterAttachOutgoingConn(t *testing.T) {
 func TestStreamWriterAttachOutgoingConn(t *testing.T) {
 	sw := startStreamWriter(types.ID(1), newPeerStatus(types.ID(1)), &stats.FollowerStats{}, &fakeRaft{})
 	sw := startStreamWriter(types.ID(1), newPeerStatus(types.ID(1)), &stats.FollowerStats{}, &fakeRaft{})
 	// the expected initial state of streamWriter is not working
 	// the expected initial state of streamWriter is not working
-	if _, ok := sw.writec(); ok != false {
+	if _, ok := sw.writec(); ok {
 		t.Errorf("initial working status = %v, want false", ok)
 		t.Errorf("initial working status = %v, want false", ok)
 	}
 	}
 
 
@@ -54,28 +54,28 @@ func TestStreamWriterAttachOutgoingConn(t *testing.T) {
 		for j := 0; j < 3; j++ {
 		for j := 0; j < 3; j++ {
 			testutil.WaitSchedule()
 			testutil.WaitSchedule()
 			// previous attached connection should be closed
 			// previous attached connection should be closed
-			if prevwfc != nil && prevwfc.Closed() != true {
+			if prevwfc != nil && !prevwfc.Closed() {
 				continue
 				continue
 			}
 			}
 			// write chan is available
 			// write chan is available
-			if _, ok := sw.writec(); ok != true {
+			if _, ok := sw.writec(); !ok {
 				continue
 				continue
 			}
 			}
 		}
 		}
 
 
 		// previous attached connection should be closed
 		// previous attached connection should be closed
-		if prevwfc != nil && prevwfc.Closed() != true {
+		if prevwfc != nil && !prevwfc.Closed() {
 			t.Errorf("#%d: close of previous connection = %v, want true", i, prevwfc.Closed())
 			t.Errorf("#%d: close of previous connection = %v, want true", i, prevwfc.Closed())
 		}
 		}
 		// write chan is available
 		// write chan is available
-		if _, ok := sw.writec(); ok != true {
+		if _, ok := sw.writec(); !ok {
 			t.Errorf("#%d: working status = %v, want true", i, ok)
 			t.Errorf("#%d: working status = %v, want true", i, ok)
 		}
 		}
 
 
 		sw.msgc <- raftpb.Message{}
 		sw.msgc <- raftpb.Message{}
 		testutil.WaitSchedule()
 		testutil.WaitSchedule()
 		// write chan is available
 		// write chan is available
-		if _, ok := sw.writec(); ok != true {
+		if _, ok := sw.writec(); !ok {
 			t.Errorf("#%d: working status = %v, want true", i, ok)
 			t.Errorf("#%d: working status = %v, want true", i, ok)
 		}
 		}
 		if wfc.Written() == 0 {
 		if wfc.Written() == 0 {
@@ -85,10 +85,10 @@ func TestStreamWriterAttachOutgoingConn(t *testing.T) {
 
 
 	sw.stop()
 	sw.stop()
 	// write chan is unavailable since the writer is stopped.
 	// write chan is unavailable since the writer is stopped.
-	if _, ok := sw.writec(); ok != false {
+	if _, ok := sw.writec(); ok {
 		t.Errorf("working status after stop = %v, want false", ok)
 		t.Errorf("working status after stop = %v, want false", ok)
 	}
 	}
-	if wfc.Closed() != true {
+	if !wfc.Closed() {
 		t.Errorf("failed to close the underlying connection")
 		t.Errorf("failed to close the underlying connection")
 	}
 	}
 }
 }
@@ -104,10 +104,10 @@ func TestStreamWriterAttachBadOutgoingConn(t *testing.T) {
 	sw.msgc <- raftpb.Message{}
 	sw.msgc <- raftpb.Message{}
 	testutil.WaitSchedule()
 	testutil.WaitSchedule()
 	// no longer working
 	// no longer working
-	if _, ok := sw.writec(); ok != false {
+	if _, ok := sw.writec(); ok {
 		t.Errorf("working = %v, want false", ok)
 		t.Errorf("working = %v, want false", ok)
 	}
 	}
-	if wfc.Closed() != true {
+	if !wfc.Closed() {
 		t.Errorf("failed to close the underlying connection")
 		t.Errorf("failed to close the underlying connection")
 	}
 	}
 }
 }