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>
diff --git a/dns/dnsmessage/message.go b/dns/dnsmessage/message.go
index da8bafd..654f191 100644
--- a/dns/dnsmessage/message.go
+++ b/dns/dnsmessage/message.go
@@ -14,7 +14,6 @@
 
 import (
 	"errors"
-	"fmt"
 )
 
 // Message formats
@@ -2141,7 +2140,7 @@
 		return nil, off, &nestedError{name + " record", err}
 	}
 	if r == nil {
-		return nil, off, fmt.Errorf("invalid resource type: %d", hdr.Type)
+		return nil, off, errors.New("invalid resource type: " + hdr.Type.String())
 	}
 	return r, off + int(hdr.Length), nil
 }
diff --git a/dns/dnsmessage/message_test.go b/dns/dnsmessage/message_test.go
index 25ba8f0..90ceb29 100644
--- a/dns/dnsmessage/message_test.go
+++ b/dns/dnsmessage/message_test.go
@@ -7,6 +7,8 @@
 import (
 	"bytes"
 	"fmt"
+	"io/ioutil"
+	"path/filepath"
 	"reflect"
 	"strings"
 	"testing"
@@ -1449,3 +1451,28 @@
 		},
 	}
 }
+
+// This package is imported by the standard library net package
+// and therefore must not use fmt. We'll catch a mistake when vendored
+// into the standard library, but this test catches the mistake earlier.
+func TestNoFmt(t *testing.T) {
+	files, err := filepath.Glob("*.go")
+	if err != nil {
+		t.Fatal(err)
+	}
+	for _, file := range files {
+		if strings.HasSuffix(file, "_test.go") {
+			continue
+		}
+		// Could use something complex like go/build or x/tools/go/packages,
+		// but there's no reason for "fmt" to appear (in quotes) in the source
+		// otherwise, so just use a simple substring search.
+		data, err := ioutil.ReadFile(file)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if bytes.Contains(data, []byte(`"fmt"`)) {
+			t.Errorf(`%s: cannot import "fmt"`, file)
+		}
+	}
+}