From a4a005d19a6cc388305c8640276d4379673b5337 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Sun, 10 May 2020 14:04:53 +0200 Subject: [PATCH] stylecheck: avoid flagging error strings that begin with a function call Closes gh-452 (cherry picked from commit d9d28f5dea96fe3488f19326d0089b3b2ef40ac1) --- stylecheck/lint.go | 4 ++++ .../testdata/src/CheckErrorStrings/CheckErrorStrings.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/stylecheck/lint.go b/stylecheck/lint.go index 75a0112b2..3cdabe155 100644 --- a/stylecheck/lint.go +++ b/stylecheck/lint.go @@ -410,6 +410,10 @@ func CheckErrorStrings(pass *analysis.Pass) (interface{}, error) { } } + if strings.ContainsRune(word, '(') { + // Might be a function call + continue instrLoop + } word = strings.TrimRightFunc(word, func(r rune) bool { return unicode.IsPunct(r) }) if objNames[fn.Package()][word] { // Word is probably the name of a function or type in this package diff --git a/stylecheck/testdata/src/CheckErrorStrings/CheckErrorStrings.go b/stylecheck/testdata/src/CheckErrorStrings/CheckErrorStrings.go index 2659fa126..c734149bc 100644 --- a/stylecheck/testdata/src/CheckErrorStrings/CheckErrorStrings.go +++ b/stylecheck/testdata/src/CheckErrorStrings/CheckErrorStrings.go @@ -11,6 +11,9 @@ func fn() { errors.New("SomeFunc is okay") errors.New("URL is okay, but the period is not.") // want `error strings should not end with punctuation or a newline` errors.New("T must not be nil") + errors.New("Foo() failed") + errors.New("Foo(bar) failed") + errors.New("Foo(bar, baz) failed") } func Write() {