-
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: report non-constant format, no args
Calls such as fmt.Printf(s), where s is non-constant and there are no arguments to format, are invariably a mistake. This CL causes the printf checker to report them, and to offer a suggested fix of fmt.Printf("%s", s). Also: - tests - docs - fixes to existing violations in x/tools (3 bugs, 2 merely bad form). - an ignore-tagged main file for the printf checker. Fixes golang/go#60529 Change-Id: Ic7cc4f3e7487bc1549948fa03b5b0b27959948e5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/585795 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Loading branch information
Showing
8 changed files
with
125 additions
and
64 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,20 @@ | ||
// 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. | ||
|
||
//go:build ignore | ||
|
||
// The printf command applies the printf checker to the specified | ||
// packages of Go source code. | ||
// | ||
// Run with: | ||
// | ||
// $ go run ./go/analysis/passes/printf/main.go -- packages... | ||
package main | ||
|
||
import ( | ||
"golang.org/x/tools/go/analysis/passes/printf" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
func main() { singlechecker.Main(printf.Analyzer) } |
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,20 @@ | ||
// 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 suggested fixes. | ||
|
||
package fix | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
func nonConstantFormat(s string) { // #60529 | ||
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` | ||
} |
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,20 @@ | ||
// 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 suggested fixes. | ||
|
||
package fix | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
func nonConstantFormat(s string) { // #60529 | ||
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