Skip to content

Commit ab34263

Browse files
committed
dns/dnsmessage: remove use of fmt that crept in
This package cannot use fmt, because standard package net imports it. (Most of the package is already clean, including Type.String.) For golang/go#40070. Change-Id: I9be92e98d9f5dcfb26852d38004e05f07df5e17a Reviewed-on: https://go-review.googlesource.com/c/net/+/241085 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
1 parent 4c52546 commit ab34263

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

dns/dnsmessage/message.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package dnsmessage
1414

1515
import (
1616
"errors"
17-
"fmt"
1817
)
1918

2019
// Message formats
@@ -2141,7 +2140,7 @@ func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody,
21412140
return nil, off, &nestedError{name + " record", err}
21422141
}
21432142
if r == nil {
2144-
return nil, off, fmt.Errorf("invalid resource type: %d", hdr.Type)
2143+
return nil, off, errors.New("invalid resource type: " + hdr.Type.String())
21452144
}
21462145
return r, off + int(hdr.Length), nil
21472146
}

dns/dnsmessage/message_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package dnsmessage
77
import (
88
"bytes"
99
"fmt"
10+
"io/ioutil"
11+
"path/filepath"
1012
"reflect"
1113
"strings"
1214
"testing"
@@ -1449,3 +1451,28 @@ func largeTestMsg() Message {
14491451
},
14501452
}
14511453
}
1454+
1455+
// This package is imported by the standard library net package
1456+
// and therefore must not use fmt. We'll catch a mistake when vendored
1457+
// into the standard library, but this test catches the mistake earlier.
1458+
func TestNoFmt(t *testing.T) {
1459+
files, err := filepath.Glob("*.go")
1460+
if err != nil {
1461+
t.Fatal(err)
1462+
}
1463+
for _, file := range files {
1464+
if strings.HasSuffix(file, "_test.go") {
1465+
continue
1466+
}
1467+
// Could use something complex like go/build or x/tools/go/packages,
1468+
// but there's no reason for "fmt" to appear (in quotes) in the source
1469+
// otherwise, so just use a simple substring search.
1470+
data, err := ioutil.ReadFile(file)
1471+
if err != nil {
1472+
t.Fatal(err)
1473+
}
1474+
if bytes.Contains(data, []byte(`"fmt"`)) {
1475+
t.Errorf(`%s: cannot import "fmt"`, file)
1476+
}
1477+
}
1478+
}

0 commit comments

Comments
 (0)