Skip to content

Commit

Permalink
dns/dnsmessage: remove use of fmt that crept in
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rsc committed Jul 7, 2020
1 parent 4c52546 commit ab34263
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 1 addition & 2 deletions dns/dnsmessage/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package dnsmessage

import (
"errors"
"fmt"
)

// Message formats
Expand Down Expand Up @@ -2141,7 +2140,7 @@ func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody,
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
}
Expand Down
27 changes: 27 additions & 0 deletions dns/dnsmessage/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package dnsmessage
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1449,3 +1451,28 @@ func largeTestMsg() Message {
},
}
}

// 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)
}
}
}

0 comments on commit ab34263

Please sign in to comment.