append result not assigned to the same slice
1164
1165// Clone returns a cloned PD server config.
1166func (c *PDServerConfig) Clone() *PDServerConfig {
1167 runtimeServices := append(c.RuntimeServices[:0:0], c.RuntimeServices...)1168 cfg := *c
1169 cfg.RuntimeServices = runtimeServices
1170 return &cfg
append result not assigned to the same slice
1013
1014// Clone makes a deep copy of the config.
1015func (c *ReplicationConfig) Clone() *ReplicationConfig {
1016 locationLabels := append(c.LocationLabels[:0:0], c.LocationLabels...)1017 cfg := *c
1018 cfg.LocationLabels = locationLabels
1019 return &cfg
append result not assigned to the same slice
679
680// Clone returns a cloned scheduling configuration.
681func (c *ScheduleConfig) Clone() *ScheduleConfig {
682 schedulers := append(c.Schedulers[:0:0], c.Schedulers...) 683 var storeLimit map[uint64]StoreLimitConfig
684 if c.StoreLimit != nil {
685 storeLimit = make(map[uint64]StoreLimitConfig, len(c.StoreLimit))
append result not assigned to the same slice
306 // Make a copy to avoid changing DefaultSchedulers unexpectedly.
307 // When reloading from storage, the config is passed to json.Unmarshal.
308 // Without clone, the DefaultSchedulers could be overwritten.
309 *v = append(defValue[:0:0], defValue...) 310 }
311}
312
Description
The builtin function append
returns the updated slice after the append operation.
This resulting slice is usually assigned back to the same variable that contained
the original slice to which new values were appended.
Bad practice
x = []int{1, 3, 5}
append(x, 7) // returns the value []int{1, 3, 5, 7}. This is usually assigned back to x
x := []int{}
y := []int{}
x = append(y, 1) // possibly wrong
Recommended
x = []int{1, 3, 5}
x = append(x, 7)
x := []int{}
y := []int{}
y = append(y, 1) // append 1 to y