Pull request: all: rm var shadowing vol. 3

Updates #2803.

Squashed commit of the following:

commit ce711ae2e08c3714a4e0c6d177c02f7801c05fce
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 20:58:40 2021 +0300

    all: imp code, docs

commit e91094e461893c27eda879b33b5ed2c7085510df
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 20:42:14 2021 +0300

    all: rm var shadowing vol. 3
This commit is contained in:
Ainar Garipov 2021-03-11 21:30:52 +03:00
parent 4cf44dd1d4
commit 968831c5b9
13 changed files with 47 additions and 39 deletions

View File

@ -132,7 +132,7 @@ on GitHub and most other Markdown renderers. -->
}
```
* Name the deferred errors (e.g. when closing something) `cerr`.
* Name the deferred errors (e.g. when closing something) `derr`.
* No shadowing, since it can often lead to subtle bugs, especially with
errors.

View File

@ -13,7 +13,7 @@ import (
)
const (
maxRetryDur = 500 * time.Millisecond
maxRetryDur = 1000 * time.Millisecond
retryDur = 5 * time.Millisecond
)
@ -56,7 +56,7 @@ func prepareTestDir(t *testing.T) (dir string) {
time.Sleep(retryDur)
}
assert.Nil(t, err)
assert.Nil(t, err, "after %s", time.Since(start))
})
return dir

View File

@ -20,7 +20,7 @@ import (
// CheckIfOtherDHCPServersPresentV4 sends a DHCP request to the specified network interface,
// and waits for a response for a period defined by defaultDiscoverTime
func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error) {
func CheckIfOtherDHCPServersPresentV4(ifaceName string) (ok bool, err error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return false, fmt.Errorf("couldn't find interface by name %s: %w", ifaceName, err)
@ -86,7 +86,8 @@ func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error) {
}
for {
ok, next, err := tryConn4(req, c, iface)
var next bool
ok, next, err = tryConn4(req, c, iface)
if next {
if err != nil {
log.Debug("dhcpv4: trying a connection: %s", err)
@ -94,6 +95,7 @@ func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error) {
continue
}
if err != nil {
return false, err
}
@ -155,7 +157,7 @@ func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, n
// CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified network interface,
// and waits for a response for a period defined by defaultDiscoverTime
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (ok bool, err error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return false, fmt.Errorf("dhcpv6: net.InterfaceByName: %s: %w", ifaceName, err)
@ -207,7 +209,8 @@ func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
}
for {
ok, next, err := tryConn6(req, c)
var next bool
ok, next, err = tryConn6(req, c)
if next {
if err != nil {
log.Debug("dhcpv6: trying a connection: %s", err)
@ -215,6 +218,7 @@ func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
continue
}
if err != nil {
return false, err
}

View File

@ -282,7 +282,9 @@ func (s *Server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
// this interface doesn't support broadcast, skip it
continue
}
addrs, err := iface.Addrs()
var addrs []net.Addr
addrs, err = iface.Addrs()
if err != nil {
httpError(r, w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err)
return

View File

@ -476,7 +476,7 @@ func (c *Client) send(dest *net.UDPAddr, msg *dhcpv4.DHCPv4) (resp <-chan *dhcpv
c.pendingMu.Unlock()
}
if _, err := c.conn.WriteTo(msg.ToBytes(), dest); err != nil {
if _, err = c.conn.WriteTo(msg.ToBytes(), dest); err != nil {
cancel()
return nil, nil, fmt.Errorf("error writing packet to connection: %w", err)
}

View File

@ -206,7 +206,7 @@ func TestSendAndRead(t *testing.T) {
t.Error(err)
}
if err := ComparePacket(rcvd, tt.want); err != nil {
if err = ComparePacket(rcvd, tt.want); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
})
@ -290,7 +290,7 @@ func TestSimpleSendAndReadDiscardGarbage(t *testing.T) {
t.Errorf("SendAndRead(%v) = %v, want nil", pkt, err)
}
if err := ComparePacket(rcvd, responses); err != nil {
if err = ComparePacket(rcvd, responses); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
}
@ -337,7 +337,7 @@ func TestMultipleSendAndRead(t *testing.T) {
if wantErr := tt.wantErr[i]; err != wantErr {
t.Errorf("SendAndReadOne(%v): got %v, want %v", send, err, wantErr)
}
if err := pktsExpected([]*dhcpv4.DHCPv4{rcvd}, tt.server[i]); err != nil {
if err = pktsExpected([]*dhcpv4.DHCPv4{rcvd}, tt.server[i]); err != nil {
t.Errorf("got unexpected packets: %v", err)
}
}

View File

@ -188,20 +188,20 @@ func (ra *raCtx) Init() error {
}
defer func() {
if !success {
cerr := ra.Close()
if cerr != nil {
log.Error("closing context: %s", cerr)
derr := ra.Close()
if derr != nil {
log.Error("closing context: %s", derr)
}
}
}()
con6 := ra.conn.IPv6PacketConn()
if err := con6.SetHopLimit(255); err != nil {
if err = con6.SetHopLimit(255); err != nil {
return fmt.Errorf("dhcpv6 ra: SetHopLimit: %w", err)
}
if err := con6.SetMulticastHopLimit(255); err != nil {
if err = con6.SetMulticastHopLimit(255); err != nil {
return fmt.Errorf("dhcpv6 ra: SetMulticastHopLimit: %w", err)
}

View File

@ -128,13 +128,13 @@ func TestV4StaticLease_Get(t *testing.T) {
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
t.Run("discover", func(t *testing.T) {
var err error
var terr error
req, err = dhcpv4.NewDiscovery(mac)
require.Nil(t, err)
req, terr = dhcpv4.NewDiscovery(mac)
require.Nil(t, terr)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
resp, terr = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, terr)
assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)
@ -227,13 +227,13 @@ func TestV4DynamicLease_Get(t *testing.T) {
})
t.Run("request", func(t *testing.T) {
var err error
var terr error
req, err = dhcpv4.NewRequestFromOffer(resp)
require.Nil(t, err)
req, terr = dhcpv4.NewRequestFromOffer(resp)
require.Nil(t, terr)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
resp, terr = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, terr)
assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)

View File

@ -290,7 +290,8 @@ func (s *Server) prepareUpstreamSettings() error {
if len(upstreamConfig.Upstreams) == 0 {
log.Info("warning: no default upstream servers specified, using %v", defaultDNS)
uc, err := proxy.ParseUpstreamsConfig(
var uc proxy.UpstreamConfig
uc, err = proxy.ParseUpstreamsConfig(
defaultDNS,
upstream.Options{
Bootstrap: s.conf.BootstrapDNS,
@ -344,7 +345,8 @@ func (s *Server) prepareTLS(proxyConfig *proxy.Config) error {
}
if s.conf.StrictSNICheck {
x, err := x509.ParseCertificate(s.conf.cert.Certificate[0])
var x *x509.Certificate
x, err = x509.ParseCertificate(s.conf.cert.Certificate[0])
if err != nil {
return fmt.Errorf("x509.ParseCertificate(): %w", err)
}

View File

@ -46,8 +46,8 @@ func startDeferStop(t *testing.T, s *Server) {
require.Nilf(t, err, "failed to start server: %s", err)
t.Cleanup(func() {
err := s.Stop()
require.Nilf(t, err, "dns server failed to stop: %s", err)
serr := s.Stop()
require.Nilf(t, serr, "dns server failed to stop: %s", serr)
})
}

View File

@ -328,18 +328,17 @@ func ValidateUpstreams(upstreams []string) error {
var defaultUpstreamFound bool
for _, u := range upstreams {
d, err := validateUpstream(u)
var ok bool
ok, err = validateUpstream(u)
if err != nil {
return err
}
// Check this flag until default upstream will not be found
if !defaultUpstreamFound {
defaultUpstreamFound = d
defaultUpstreamFound = ok
}
}
// Return error if there are no default upstreams
if !defaultUpstreamFound {
return fmt.Errorf("no default upstreams specified")
}
@ -477,7 +476,7 @@ func checkDNS(input string, bootstrap []string) error {
return nil
}
if _, err := validateUpstream(input); err != nil {
if _, err = validateUpstream(input); err != nil {
return fmt.Errorf("wrong upstream format: %w", err)
}

View File

@ -199,7 +199,8 @@ func TestDNSForwardHTTTP_handleSetConfig(t *testing.T) {
})
rBody := ioutil.NopCloser(strings.NewReader(tc.req))
r, err := http.NewRequest(http.MethodPost, "http://example.com", rBody)
var r *http.Request
r, err = http.NewRequest(http.MethodPost, "http://example.com", rBody)
require.Nil(t, err)
s.handleSetConfig(w, r)

View File

@ -74,7 +74,8 @@ func (c *ipsetCtx) ipsetProps(name string) (set ipsetProps, err error) {
// query only the IPv4 one.
//
// TODO(a.garipov): Find out if this is a bug or a feature.
res, err := c.ipv4Conn.Header(name)
var res *ipset.HeaderPolicy
res, err = c.ipv4Conn.Header(name)
if err != nil {
return set, err
}
@ -104,7 +105,6 @@ func (c *ipsetCtx) ipsets(names []string) (sets []ipsetProps, err error) {
continue
}
var err error
set, err = c.ipsetProps(name)
if err != nil {
return nil, fmt.Errorf("querying ipset %q: %w", name, err)