-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/analysis/passes/printf: suppress errors for non-const format strings
The new check added in golang/go#60529 reports errors for non-constant format strings with no arguments. These are almost always bugs, but are often mild or inconsequential, and can be numerous in existing code bases. To reduce friction from this change, gate the new check on the implied language version. For golang/go#71485 Change-Id: I4926da2809dd14ba70ae530cd1657119f5377ad5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645595 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
- Loading branch information
Showing
7 changed files
with
195 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
This test checks for the correct suppression (or activation) of the | ||
non-constant format string check (golang/go#60529), in a go1.23 module. | ||
|
||
See golang/go#71485 for details. | ||
|
||
-- go.mod -- | ||
module example.com/nonconst | ||
|
||
go 1.23 | ||
|
||
-- nonconst.go -- | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
func _(s string) { | ||
fmt.Printf(s) | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, s) | ||
log.Printf(s) | ||
} | ||
|
||
-- nonconst_go124.go -- | ||
//go:build go1.24 | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
// With Go 1.24, the analyzer should be activated, as this is a go1.24 file. | ||
func _(s string) { | ||
fmt.Printf(s) // want `non-constant format string in call to fmt.Printf` | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, s) // want `non-constant format string in call to fmt.Fprintf` | ||
log.Printf(s) // want `non-constant format string in call to log.Printf` | ||
} | ||
|
||
-- nonconst_go124.go.golden -- | ||
//go:build go1.24 | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
// With Go 1.24, the analyzer should be activated, as this is a go1.24 file. | ||
func _(s string) { | ||
fmt.Printf("%s", s) // want `non-constant format string in call to fmt.Printf` | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, "%s", s) // want `non-constant format string in call to fmt.Fprintf` | ||
log.Printf("%s", s) // want `non-constant format string in call to log.Printf` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
This test checks for the correct suppression (or activation) of the | ||
non-constant format string check (golang/go#60529), in a go1.24 module. | ||
|
||
See golang/go#71485 for details. | ||
|
||
-- go.mod -- | ||
module example.com/nonconst | ||
|
||
go 1.24 | ||
|
||
-- nonconst.go -- | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
func _(s string) { | ||
fmt.Printf(s) // want `non-constant format string in call to fmt.Printf` | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, s) // want `non-constant format string in call to fmt.Fprintf` | ||
log.Printf(s) // want `non-constant format string in call to log.Printf` | ||
} | ||
|
||
-- nonconst.go.golden -- | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
func _(s string) { | ||
fmt.Printf("%s", s) // want `non-constant format string in call to fmt.Printf` | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, "%s", s) // want `non-constant format string in call to fmt.Fprintf` | ||
log.Printf("%s", s) // want `non-constant format string in call to log.Printf` | ||
} | ||
|
||
-- nonconst_go123.go -- | ||
//go:build go1.23 | ||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
// The analyzer should be silent, as this is a go1.23 file. | ||
func _(s string) { | ||
fmt.Printf(s) | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, s) | ||
log.Printf(s) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
go/analysis/passes/printf/testdata/src/nonconst/nonconst.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// This file contains tests of the printf checker's handling of non-constant | ||
// format strings (golang/go#60529). | ||
|
||
package nonconst | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
// As the language version is empty here, and the new check is gated on go1.24, | ||
// diagnostics are suppressed here. | ||
func nonConstantFormat(s string) { | ||
fmt.Printf(s) | ||
fmt.Printf(s, "arg") | ||
fmt.Fprintf(os.Stderr, s) | ||
log.Printf(s) | ||
} |