[tools] go/analysis/passes/printf: update directive diagnostic message

115 views
Skip to first unread message

Tim King (Gerrit)

unread,
May 1, 2023, 5:00:45 PM5/1/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Tim King has uploaded this change for review.

View Change

go/analysis/passes/printf: update directive diagnostic message

Updates the Diagnostic message for a Printf directive being
found in a non-Printf function. New message suggests using Printf.

Example buggy code:
```
fmt.Println("%s", "hi")
```

Old diagnostic message:
```
fmt.Println call has possible formatting directive %s
```

New diagnostic message:
```
fmt.Println call contains %s, possibly intended as Printf formatting directive?

```

Fixes golang/go#47872

Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
---
M go/analysis/passes/printf/printf.go
M go/analysis/passes/printf/testdata/src/a/a.go
2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go
index 919edac..c9aa7d0 100644
--- a/go/analysis/passes/printf/printf.go
+++ b/go/analysis/passes/printf/printf.go
@@ -1056,7 +1056,7 @@
if strings.Contains(s, "%") {
m := printFormatRE.FindStringSubmatch(s)
if m != nil {
- pass.ReportRangef(call, "%s call has possible formatting directive %s", fn.FullName(), m[0])
+ pass.ReportRangef(call, "%s call contains %s, possibly intended as Printf formatting directive?", fn.FullName(), m[0])
}
}
}
diff --git a/go/analysis/passes/printf/testdata/src/a/a.go b/go/analysis/passes/printf/testdata/src/a/a.go
index 8aa4af9..301cde4 100644
--- a/go/analysis/passes/printf/testdata/src/a/a.go
+++ b/go/analysis/passes/printf/testdata/src/a/a.go
@@ -150,10 +150,10 @@
fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want "fmt.Printf format %6g has arg 'x' of wrong type rune"
fmt.Println() // not an error
- fmt.Println("%s", "hi") // want "fmt.Println call has possible formatting directive %s"
- fmt.Println("%v", "hi") // want "fmt.Println call has possible formatting directive %v"
- fmt.Println("%T", "hi") // want "fmt.Println call has possible formatting directive %T"
- fmt.Println("%s"+" there", "hi") // want "fmt.Println call has possible formatting directive %s"
+ fmt.Println("%s", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive?"
+ fmt.Println("%v", "hi") // want "fmt.Println call contains %v, possibly intended as Printf formatting directive?"
+ fmt.Println("%T", "hi") // want "fmt.Println call contains %T, possibly intended as Printf formatting directive?"
+ fmt.Println("%s"+" there", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive?"
fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args"
_ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args"
@@ -177,19 +177,19 @@
Printf(format, "hi") // want "a.Printf format %s reads arg #2, but call has 1 arg$"
Printf("%s %d %.3v %q", "str", 4) // want "a.Printf format %.3v reads arg #3, but call has 2 args"
f := new(ptrStringer)
- f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call has possible formatting directive %s`
+ f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call contains %s, possibly intended as Printf formatting directive?`
f.Warnf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf call needs 1 arg but has 2 args`
f.Warnf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf format %r has unknown verb r`
f.Warnf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf format %#s has unrecognized flag #`
- f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call has possible formatting directive %s`
+ f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call contains %s, possibly intended as Printf formatting directive?`
f.Warnf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf2 call needs 1 arg but has 2 args`
f.Warnf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %r has unknown verb r`
f.Warnf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %#s has unrecognized flag #`
- f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call has possible formatting directive %s`
+ f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call contains %s, possibly intended as Printf formatting directive?`
f.Wrapf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf call needs 1 arg but has 2 args`
f.Wrapf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf format %r has unknown verb r`
f.Wrapf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf format %#s has unrecognized flag #`
- f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call has possible formatting directive %s`
+ f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call contains %s, possibly intended as Printf formatting directive?`
f.Wrapf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf2 call needs 1 arg but has 2 args`
f.Wrapf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %r has unknown verb r`
f.Wrapf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %#s has unrecognized flag #`
@@ -226,7 +226,7 @@
var et1 *testing.T
et1.Error() // ok
et1.Error("hi") // ok
- et1.Error("%d", 3) // want `\(\*testing.common\).Error call has possible formatting directive %d`
+ et1.Error("%d", 3) // want `\(\*testing.common\).Error call contains %d, possibly intended as Printf formatting directive?`
et1.Errorf("%s", 1) // want `\(\*testing.common\).Errorf format %s has arg 1 of wrong type int`
var et3 errorTest3
et3.Error() // ok, not an error method.
@@ -253,7 +253,7 @@
// Special handling for Log.
math.Log(3) // OK
var t *testing.T
- t.Log("%d", 3) // want `\(\*testing.common\).Log call has possible formatting directive %d`
+ t.Log("%d", 3) // want `\(\*testing.common\).Log call contains %d, possibly intended as Printf formatting directive?`
t.Logf("%d", 3)
t.Logf("%d", "hi") // want `\(\*testing.common\).Logf format %d has arg "hi" of wrong type string`

@@ -307,27 +307,27 @@
Printf(someString(), "hello") // OK

// Printf wrappers in package log should be detected automatically
- logpkg.Fatal("%d", 1) // want "log.Fatal call has possible formatting directive %d"
+ logpkg.Fatal("%d", 1) // want "log.Fatal call contains %d, possibly intended as Printf formatting directive?"
logpkg.Fatalf("%d", "x") // want `log.Fatalf format %d has arg "x" of wrong type string`
- logpkg.Fatalln("%d", 1) // want "log.Fatalln call has possible formatting directive %d"
- logpkg.Panic("%d", 1) // want "log.Panic call has possible formatting directive %d"
+ logpkg.Fatalln("%d", 1) // want "log.Fatalln call contains %d, possibly intended as Printf formatting directive?"
+ logpkg.Panic("%d", 1) // want "log.Panic call contains %d, possibly intended as Printf formatting directive?"
logpkg.Panicf("%d", "x") // want `log.Panicf format %d has arg "x" of wrong type string`
- logpkg.Panicln("%d", 1) // want "log.Panicln call has possible formatting directive %d"
- logpkg.Print("%d", 1) // want "log.Print call has possible formatting directive %d"
+ logpkg.Panicln("%d", 1) // want "log.Panicln call contains %d, possibly intended as Printf formatting directive?"
+ logpkg.Print("%d", 1) // want "log.Print call contains %d, possibly intended as Printf formatting directive?"
logpkg.Printf("%d", "x") // want `log.Printf format %d has arg "x" of wrong type string`
- logpkg.Println("%d", 1) // want "log.Println call has possible formatting directive %d"
+ logpkg.Println("%d", 1) // want "log.Println call contains %d, possibly intended as Printf formatting directive?"

// Methods too.
var l *logpkg.Logger
- l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call has possible formatting directive %d`
+ l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call contains %d, possibly intended as Printf formatting directive?`
l.Fatalf("%d", "x") // want `\(\*log.Logger\).Fatalf format %d has arg "x" of wrong type string`
- l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call has possible formatting directive %d`
- l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call has possible formatting directive %d`
+ l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call contains %d, possibly intended as Printf formatting directive?`
+ l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call contains %d, possibly intended as Printf formatting directive?`
l.Panicf("%d", "x") // want `\(\*log.Logger\).Panicf format %d has arg "x" of wrong type string`
- l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call has possible formatting directive %d`
- l.Print("%d", 1) // want `\(\*log.Logger\).Print call has possible formatting directive %d`
+ l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call contains %d, possibly intended as Printf formatting directive?`
+ l.Print("%d", 1) // want `\(\*log.Logger\).Print call contains %d, possibly intended as Printf formatting directive?`
l.Printf("%d", "x") // want `\(\*log.Logger\).Printf format %d has arg "x" of wrong type string`
- l.Println("%d", 1) // want `\(\*log.Logger\).Println call has possible formatting directive %d`
+ l.Println("%d", 1) // want `\(\*log.Logger\).Println call contains %d, possibly intended as Printf formatting directive?`

// Issue 26486
dbg("", 1) // no error "call has arguments but no formatting directive"
@@ -361,7 +361,7 @@
eis.Errorf(0, "%w", err) // OK
ess.Errorf("ERROR", "%w", err) // OK
fmt.Appendf(nil, "%d", "123") // want `wrong type`
- fmt.Append(nil, "%d", 123) // want `possible formatting directive`
+ fmt.Append(nil, "%d", 123) // want `possibly intended as Printf formatting directive`

}

@@ -839,7 +839,7 @@
// Printf wrappers from external package
func externalPackage() {
b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int"
- b.Wrap("%s", 1) // want "Wrap call has possible formatting directive %s"
+ b.Wrap("%s", 1) // want "Wrap call contains %s, possibly intended as Printf formatting directive?"
b.NoWrap("%s", 1)
b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
}

To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
Gerrit-Change-Number: 491075
Gerrit-PatchSet: 1
Gerrit-Owner: Tim King <tak...@google.com>

Tim King (Gerrit)

unread,
May 1, 2023, 5:00:54 PM5/1/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Patch set 1:Run-TryBot +1Commit-Queue +1

View Change

    To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
    Gerrit-Change-Number: 491075
    Gerrit-PatchSet: 1
    Gerrit-Owner: Tim King <tak...@google.com>
    Gerrit-Reviewer: Tim King <tak...@google.com>
    Gerrit-Comment-Date: Mon, 01 May 2023 21:00:51 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes

    kokoro (Gerrit)

    unread,
    May 1, 2023, 5:24:37 PM5/1/23
    to Tim King, goph...@pubsubhelper.golang.org, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

    Attention is currently required from: Tim King.

    Kokoro presubmit build finished with status: SUCCESS
    Logs at: https://source.cloud.google.com/results/invocations/624f39ab-cf21-47d6-9f8c-d3468c9e3fc9

    Patch set 1:gopls-CI +1

    View Change

      To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
      Gerrit-Change-Number: 491075
      Gerrit-PatchSet: 1
      Gerrit-Owner: Tim King <tak...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Tim King <tak...@google.com>
      Gerrit-Reviewer: kokoro <noreply...@google.com>
      Gerrit-Attention: Tim King <tak...@google.com>
      Gerrit-Comment-Date: Mon, 01 May 2023 21:24:27 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes

      Alan Donovan (Gerrit)

      unread,
      May 1, 2023, 6:57:07 PM5/1/23
      to Tim King, goph...@pubsubhelper.golang.org, kokoro, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

      Attention is currently required from: Tim King.

      View Change

      1 comment:

      • File go/analysis/passes/printf/printf.go:

        • Patch Set #1, Line 1059: ?

          At the risk of relitigating (and contradicting myself), let's lose the question mark since it's ungrammatical and redundant with "possibly".

      To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
      Gerrit-Change-Number: 491075
      Gerrit-PatchSet: 1
      Gerrit-Owner: Tim King <tak...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Tim King <tak...@google.com>
      Gerrit-Reviewer: kokoro <noreply...@google.com>
      Gerrit-CC: Alan Donovan <adon...@google.com>
      Gerrit-Attention: Tim King <tak...@google.com>
      Gerrit-Comment-Date: Mon, 01 May 2023 22:57:03 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No

      Tim King (Gerrit)

      unread,
      May 1, 2023, 11:25:51 PM5/1/23
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Tim King.

      Tim King uploaded patch set #2 to this change.

      View Change

      The following approvals got outdated and were removed: Run-TryBot+1 by Tim King, TryBot-Result+1 by Gopher Robot, gopls-CI+1 by kokoro

      go/analysis/passes/printf: update directive diagnostic message

      Updates the Diagnostic message for a Printf directive being
      found in a non-Printf function. New message suggests using Printf.

      Example buggy code:
      fmt.Println("%s", "hi")
      Old diagnostic message:
      fmt.Println call has possible formatting directive %s
      New diagnostic message:
      fmt.Println call contains %s, possibly intended as Printf formatting directive

      Fixes golang/go#47872

      Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
      ---
      M go/analysis/passes/printf/printf.go
      M go/analysis/passes/printf/testdata/src/a/a.go
      2 files changed, 25 insertions(+), 25 deletions(-)

      To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: newpatchset
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
      Gerrit-Change-Number: 491075
      Gerrit-PatchSet: 2

      Tim King (Gerrit)

      unread,
      May 1, 2023, 11:26:08 PM5/1/23
      to goph...@pubsubhelper.golang.org, Alan Donovan, kokoro, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

      Patch set 2:Run-TryBot +1

      View Change

        To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
        Gerrit-Change-Number: 491075
        Gerrit-PatchSet: 2
        Gerrit-Owner: Tim King <tak...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Tim King <tak...@google.com>
        Gerrit-Reviewer: kokoro <noreply...@google.com>
        Gerrit-CC: Alan Donovan <adon...@google.com>
        Gerrit-Comment-Date: Tue, 02 May 2023 03:26:04 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes

        Tim King (Gerrit)

        unread,
        May 1, 2023, 11:26:42 PM5/1/23
        to goph...@pubsubhelper.golang.org, Alan Donovan, Zvonimir Pavlinovic, kokoro, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

        Attention is currently required from: Alan Donovan, Zvonimir Pavlinovic.

        View Change

        1 comment:

        • File go/analysis/passes/printf/printf.go:

          • At the risk of relitigating (and contradicting myself), let's lose the question mark since it's ungr […]

            Makes sense to me.

        To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
        Gerrit-Change-Number: 491075
        Gerrit-PatchSet: 2
        Gerrit-Owner: Tim King <tak...@google.com>
        Gerrit-Reviewer: Alan Donovan <adon...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Tim King <tak...@google.com>
        Gerrit-Reviewer: Zvonimir Pavlinovic <zpavl...@google.com>
        Gerrit-Reviewer: kokoro <noreply...@google.com>
        Gerrit-Attention: Zvonimir Pavlinovic <zpavl...@google.com>
        Gerrit-Attention: Alan Donovan <adon...@google.com>
        Gerrit-Comment-Date: Tue, 02 May 2023 03:26:39 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Alan Donovan <adon...@google.com>

        kokoro (Gerrit)

        unread,
        May 1, 2023, 11:44:32 PM5/1/23
        to Tim King, goph...@pubsubhelper.golang.org, Gopher Robot, Alan Donovan, Zvonimir Pavlinovic, Go LUCI, golang-co...@googlegroups.com

        Attention is currently required from: Alan Donovan, Zvonimir Pavlinovic.

        Kokoro presubmit build finished with status: SUCCESS
        Logs at: https://source.cloud.google.com/results/invocations/d3730600-512f-444b-8cd0-983f2b11b939

        Patch set 2:gopls-CI +1

        View Change

          To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-MessageType: comment
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
          Gerrit-Change-Number: 491075
          Gerrit-PatchSet: 2
          Gerrit-Owner: Tim King <tak...@google.com>
          Gerrit-Reviewer: Alan Donovan <adon...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Tim King <tak...@google.com>
          Gerrit-Reviewer: Zvonimir Pavlinovic <zpavl...@google.com>
          Gerrit-Reviewer: kokoro <noreply...@google.com>
          Gerrit-Attention: Zvonimir Pavlinovic <zpavl...@google.com>
          Gerrit-Attention: Alan Donovan <adon...@google.com>
          Gerrit-Comment-Date: Tue, 02 May 2023 03:44:28 +0000
          Gerrit-HasComments: No
          Gerrit-Has-Labels: Yes

          Alan Donovan (Gerrit)

          unread,
          May 2, 2023, 8:59:42 AM5/2/23
          to Tim King, goph...@pubsubhelper.golang.org, kokoro, Gopher Robot, Zvonimir Pavlinovic, Go LUCI, golang-co...@googlegroups.com

          Attention is currently required from: Tim King, Zvonimir Pavlinovic.

          Patch set 2:Code-Review +2

          View Change

          1 comment:

          To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-MessageType: comment
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
          Gerrit-Change-Number: 491075
          Gerrit-PatchSet: 2
          Gerrit-Owner: Tim King <tak...@google.com>
          Gerrit-Reviewer: Alan Donovan <adon...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Tim King <tak...@google.com>
          Gerrit-Reviewer: Zvonimir Pavlinovic <zpavl...@google.com>
          Gerrit-Reviewer: kokoro <noreply...@google.com>
          Gerrit-Attention: Zvonimir Pavlinovic <zpavl...@google.com>
          Gerrit-Attention: Tim King <tak...@google.com>
          Gerrit-Comment-Date: Tue, 02 May 2023 12:59:38 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: Yes

          Tim King (Gerrit)

          unread,
          May 2, 2023, 5:50:59 PM5/2/23
          to goph...@pubsubhelper.golang.org, Alan Donovan, kokoro, Gopher Robot, Zvonimir Pavlinovic, Go LUCI, golang-co...@googlegroups.com

          Attention is currently required from: Zvonimir Pavlinovic.

          Patch set 2:Commit-Queue +1

          View Change

            To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-MessageType: comment
            Gerrit-Project: tools
            Gerrit-Branch: master
            Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
            Gerrit-Change-Number: 491075
            Gerrit-PatchSet: 2
            Gerrit-Owner: Tim King <tak...@google.com>
            Gerrit-Reviewer: Alan Donovan <adon...@google.com>
            Gerrit-Reviewer: Gopher Robot <go...@golang.org>
            Gerrit-Reviewer: Tim King <tak...@google.com>
            Gerrit-Reviewer: Zvonimir Pavlinovic <zpavl...@google.com>
            Gerrit-Reviewer: kokoro <noreply...@google.com>
            Gerrit-Attention: Zvonimir Pavlinovic <zpavl...@google.com>
            Gerrit-Comment-Date: Tue, 02 May 2023 21:50:56 +0000
            Gerrit-HasComments: No
            Gerrit-Has-Labels: Yes

            Zvonimir Pavlinovic (Gerrit)

            unread,
            May 2, 2023, 6:21:38 PM5/2/23
            to Tim King, goph...@pubsubhelper.golang.org, Alan Donovan, kokoro, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

            Attention is currently required from: Tim King.

            Patch set 2:Code-Review +2

            View Change

              To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-MessageType: comment
              Gerrit-Project: tools
              Gerrit-Branch: master
              Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
              Gerrit-Change-Number: 491075
              Gerrit-PatchSet: 2
              Gerrit-Owner: Tim King <tak...@google.com>
              Gerrit-Reviewer: Alan Donovan <adon...@google.com>
              Gerrit-Reviewer: Gopher Robot <go...@golang.org>
              Gerrit-Reviewer: Tim King <tak...@google.com>
              Gerrit-Reviewer: Zvonimir Pavlinovic <zpavl...@google.com>
              Gerrit-Reviewer: kokoro <noreply...@google.com>
              Gerrit-Attention: Tim King <tak...@google.com>
              Gerrit-Comment-Date: Tue, 02 May 2023 22:21:35 +0000
              Gerrit-HasComments: No
              Gerrit-Has-Labels: Yes

              Tim King (Gerrit)

              unread,
              May 2, 2023, 6:25:07 PM5/2/23
              to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Zvonimir Pavlinovic, Alan Donovan, kokoro, Gopher Robot, Go LUCI, golang-co...@googlegroups.com

              Tim King submitted this change.

              View Change

              Approvals: Tim King: Run TryBots kokoro: gopls CI succeeded Zvonimir Pavlinovic: Looks good to me, approved Alan Donovan: Looks good to me, approved Gopher Robot: TryBots succeeded
              go/analysis/passes/printf: update directive diagnostic message

              Updates the Diagnostic message for a Printf directive being
              found in a non-Printf function. New message suggests using Printf.

              Example buggy code:
              fmt.Println("%s", "hi")
              Old diagnostic message:
              fmt.Println call has possible formatting directive %s
              New diagnostic message:
              fmt.Println call contains %s, possibly intended as Printf formatting directive

              Fixes golang/go#47872

              Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
              Reviewed-on: https://go-review.googlesource.com/c/tools/+/491075
              Reviewed-by: Zvonimir Pavlinovic <zpavl...@google.com>
              gopls-CI: kokoro <noreply...@google.com>
              TryBot-Result: Gopher Robot <go...@golang.org>
              Run-TryBot: Tim King <tak...@google.com>
              Reviewed-by: Alan Donovan <adon...@google.com>

              ---
              M go/analysis/passes/printf/printf.go
              M go/analysis/passes/printf/testdata/src/a/a.go
              2 files changed, 25 insertions(+), 25 deletions(-)

              
              
              diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go
              index 919edac..314b152 100644

              --- a/go/analysis/passes/printf/printf.go
              +++ b/go/analysis/passes/printf/printf.go
              @@ -1056,7 +1056,7 @@
              if strings.Contains(s, "%") {
              m := printFormatRE.FindStringSubmatch(s)
              if m != nil {
              - pass.ReportRangef(call, "%s call has possible formatting directive %s", fn.FullName(), m[0])
              +				pass.ReportRangef(call, "%s call contains %s, possibly intended as Printf formatting directive", fn.FullName(), m[0])
              }
              }
              }
              diff --git a/go/analysis/passes/printf/testdata/src/a/a.go b/go/analysis/passes/printf/testdata/src/a/a.go
              index 8aa4af9..ce8809f 100644

              --- a/go/analysis/passes/printf/testdata/src/a/a.go
              +++ b/go/analysis/passes/printf/testdata/src/a/a.go
              @@ -150,10 +150,10 @@
              fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
              fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want "fmt.Printf format %6g has arg 'x' of wrong type rune"
              fmt.Println() // not an error
              - fmt.Println("%s", "hi") // want "fmt.Println call has possible formatting directive %s"
              - fmt.Println("%v", "hi") // want "fmt.Println call has possible formatting directive %v"
              - fmt.Println("%T", "hi") // want "fmt.Println call has possible formatting directive %T"
              -	fmt.Println("%s"+" there", "hi")            // want "fmt.Println call has possible formatting directive %s"
              + fmt.Println("%s", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive"
              + fmt.Println("%v", "hi") // want "fmt.Println call contains %v, possibly intended as Printf formatting directive"
              + fmt.Println("%T", "hi") // want "fmt.Println call contains %T, possibly intended as Printf formatting directive"
              + fmt.Println("%s"+" there", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive"

              fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
              fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args"
              _ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args"
              @@ -177,19 +177,19 @@
              Printf(format, "hi") // want "a.Printf format %s reads arg #2, but call has 1 arg$"
              Printf("%s %d %.3v %q", "str", 4) // want "a.Printf format %.3v reads arg #3, but call has 2 args"
              f := new(ptrStringer)
              - f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call has possible formatting directive %s`
              +	f.Warn(0, "%s", "hello", 3)           // want `\(\*a.ptrStringer\).Warn call contains %s, possibly intended as Printf formatting directive`

              f.Warnf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf call needs 1 arg but has 2 args`
              f.Warnf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf format %r has unknown verb r`
              f.Warnf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf format %#s has unrecognized flag #`
              - f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call has possible formatting directive %s`
              +	f.Warn2(0, "%s", "hello", 3)          // want `\(\*a.ptrStringer\).Warn2 call contains %s, possibly intended as Printf formatting directive`

              f.Warnf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf2 call needs 1 arg but has 2 args`
              f.Warnf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %r has unknown verb r`
              f.Warnf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %#s has unrecognized flag #`
              - f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call has possible formatting directive %s`
              +	f.Wrap(0, "%s", "hello", 3)           // want `\(\*a.ptrStringer\).Wrap call contains %s, possibly intended as Printf formatting directive`

              f.Wrapf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf call needs 1 arg but has 2 args`
              f.Wrapf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf format %r has unknown verb r`
              f.Wrapf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf format %#s has unrecognized flag #`
              - f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call has possible formatting directive %s`
              +	f.Wrap2(0, "%s", "hello", 3)          // want `\(\*a.ptrStringer\).Wrap2 call contains %s, possibly intended as Printf formatting directive`

              f.Wrapf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf2 call needs 1 arg but has 2 args`
              f.Wrapf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %r has unknown verb r`
              f.Wrapf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %#s has unrecognized flag #`
              @@ -226,7 +226,7 @@
              var et1 *testing.T
              et1.Error() // ok
              et1.Error("hi") // ok
              - et1.Error("%d", 3) // want `\(\*testing.common\).Error call has possible formatting directive %d`
              +	et1.Error("%d", 3)  // want `\(\*testing.common\).Error call contains %d, possibly intended as Printf formatting directive`

              et1.Errorf("%s", 1) // want `\(\*testing.common\).Errorf format %s has arg 1 of wrong type int`
              var et3 errorTest3
              et3.Error() // ok, not an error method.
              @@ -253,7 +253,7 @@
              // Special handling for Log.
              math.Log(3) // OK
              var t *testing.T
              - t.Log("%d", 3) // want `\(\*testing.common\).Log call has possible formatting directive %d`
              +	t.Log("%d", 3) // want `\(\*testing.common\).Log call contains %d, possibly intended as Printf formatting directive`

              t.Logf("%d", 3)
              t.Logf("%d", "hi") // want `\(\*testing.common\).Logf format %d has arg "hi" of wrong type string`

              @@ -307,27 +307,27 @@
              Printf(someString(), "hello") // OK

              // Printf wrappers in package log should be detected automatically
              - logpkg.Fatal("%d", 1) // want "log.Fatal call has possible formatting directive %d"
              +	logpkg.Fatal("%d", 1)    // want "log.Fatal call contains %d, possibly intended as Printf formatting directive"

              logpkg.Fatalf("%d", "x") // want `log.Fatalf format %d has arg "x" of wrong type string`
              - logpkg.Fatalln("%d", 1) // want "log.Fatalln call has possible formatting directive %d"
              - logpkg.Panic("%d", 1) // want "log.Panic call has possible formatting directive %d"
              +	logpkg.Fatalln("%d", 1)  // want "log.Fatalln call contains %d, possibly intended as Printf formatting directive"
              + logpkg.Panic("%d", 1) // want "log.Panic call contains %d, possibly intended as Printf formatting directive"

              logpkg.Panicf("%d", "x") // want `log.Panicf format %d has arg "x" of wrong type string`
              - logpkg.Panicln("%d", 1) // want "log.Panicln call has possible formatting directive %d"
              - logpkg.Print("%d", 1) // want "log.Print call has possible formatting directive %d"
              +	logpkg.Panicln("%d", 1)  // want "log.Panicln call contains %d, possibly intended as Printf formatting directive"
              + logpkg.Print("%d", 1) // want "log.Print call contains %d, possibly intended as Printf formatting directive"

              logpkg.Printf("%d", "x") // want `log.Printf format %d has arg "x" of wrong type string`
              - logpkg.Println("%d", 1) // want "log.Println call has possible formatting directive %d"
              +	logpkg.Println("%d", 1)  // want "log.Println call contains %d, possibly intended as Printf formatting directive"


              // Methods too.
              var l *logpkg.Logger
              - l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call has possible formatting directive %d`
              +	l.Fatal("%d", 1)    // want `\(\*log.Logger\).Fatal call contains %d, possibly intended as Printf formatting directive`

              l.Fatalf("%d", "x") // want `\(\*log.Logger\).Fatalf format %d has arg "x" of wrong type string`
              - l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call has possible formatting directive %d`
              - l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call has possible formatting directive %d`
              +	l.Fatalln("%d", 1)  // want `\(\*log.Logger\).Fatalln call contains %d, possibly intended as Printf formatting directive`
              + l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call contains %d, possibly intended as Printf formatting directive`

              l.Panicf("%d", "x") // want `\(\*log.Logger\).Panicf format %d has arg "x" of wrong type string`
              - l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call has possible formatting directive %d`
              - l.Print("%d", 1) // want `\(\*log.Logger\).Print call has possible formatting directive %d`
              +	l.Panicln("%d", 1)  // want `\(\*log.Logger\).Panicln call contains %d, possibly intended as Printf formatting directive`
              + l.Print("%d", 1) // want `\(\*log.Logger\).Print call contains %d, possibly intended as Printf formatting directive`

              l.Printf("%d", "x") // want `\(\*log.Logger\).Printf format %d has arg "x" of wrong type string`
              - l.Println("%d", 1) // want `\(\*log.Logger\).Println call has possible formatting directive %d`
              +	l.Println("%d", 1)  // want `\(\*log.Logger\).Println call contains %d, possibly intended as Printf formatting directive`


              // Issue 26486
              dbg("", 1) // no error "call has arguments but no formatting directive"
              @@ -361,7 +361,7 @@
              eis.Errorf(0, "%w", err) // OK
              ess.Errorf("ERROR", "%w", err) // OK
              fmt.Appendf(nil, "%d", "123") // want `wrong type`
              - fmt.Append(nil, "%d", 123) // want `possible formatting directive`
              + fmt.Append(nil, "%d", 123) // want `possibly intended as Printf formatting directive`

              }

              @@ -839,7 +839,7 @@
              // Printf wrappers from external package
              func externalPackage() {
              b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int"
              - b.Wrap("%s", 1) // want "Wrap call has possible formatting directive %s"
              +	b.Wrap("%s", 1)  // want "Wrap call contains %s, possibly intended as Printf formatting directive"

              b.NoWrap("%s", 1)
              b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
              }

              To view, visit change 491075. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-MessageType: merged
              Gerrit-Project: tools
              Gerrit-Branch: master
              Gerrit-Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016
              Gerrit-Change-Number: 491075
              Gerrit-PatchSet: 3
              Reply all
              Reply to author
              Forward
              0 new messages