Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: printf check misses strings that are + together #30436

Closed
imirkin opened this issue Feb 27, 2019 · 6 comments
Closed

cmd/vet: printf check misses strings that are + together #30436

imirkin opened this issue Feb 27, 2019 · 6 comments

Comments

@imirkin
Copy link

@imirkin imirkin commented Feb 27, 2019

With this example put into https://play.golang.org/ (and go1.11), there is no warning. Joining the two strings into 1 results in a warning.

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground %d" + "", 5)
}

Some people split long strings, which is where I encountered it.

@bcmills bcmills changed the title vet: printf check misses strings that are + together cmd/vet: printf check misses strings that are + together Feb 27, 2019
@bcmills
Copy link
Member

@bcmills bcmills commented Feb 27, 2019

Loading

@alandonovan
Copy link
Contributor

@alandonovan alandonovan commented Feb 27, 2019

Thanks, this is indeed a bug. The checkPrint function in golang.org/x/tools/go/analysis/passes/printf/printf.go looks for ast.BasicLit, which is a relic of the older implementation which could not assume well-typed syntax trees. Now it's better to look for expressions that are string constants, independent of their syntax, as we do when checking Printf (not Println as in your example).

Loading

@ALTree ALTree added this to the Unplanned milestone Sep 27, 2019
AlexanderYastrebov added a commit to AlexanderYastrebov/tools that referenced this issue Oct 19, 2021
AlexanderYastrebov added a commit to AlexanderYastrebov/tools that referenced this issue Oct 19, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 19, 2021

Change https://golang.org/cl/356830 mentions this issue: x/tools: print check misses concatenated strings

Loading

@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Nov 3, 2021

This change is finding 3 instances in the Go tree itself:

# cmd/cover
cmd/cover/cover.go:43:2: fmt.Fprintln arg list ends with redundant newline
# cmd/vendor/github.com/google/pprof/internal/report
cmd/vendor/github.com/google/pprof/internal/report/source.go:877:2: fmt.Fprintln arg list ends with redundant newline
# cmd/trace
cmd/trace/main.go:69:3: fmt.Fprintln arg list ends with redundant newline

Filed #49322 for resolving them.

Loading

zpavlinovic added a commit to zpavlinovic/pprof that referenced this issue Nov 3, 2021
This would otherwise be caught with the improved vet printf
checker (golang/go#30436).
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 3, 2021

Change https://golang.org/cl/361263 mentions this issue: src/cmd/cover: use fmt.Print for newline-ending fixed string

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 3, 2021

Change https://golang.org/cl/361265 mentions this issue: src/cmd/trace: use fmt.Print for newline-ending fixed string

Loading

gopherbot pushed a commit that referenced this issue Nov 4, 2021
This redundancy is now caught by the improved printf vet checker
(#30436).

Updates #49322

Change-Id: Id450247adc6fa28a9244c019be3c1b52c2d17f49
Reviewed-on: https://go-review.googlesource.com/c/go/+/361263
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
zpavlinovic added a commit to zpavlinovic/pprof that referenced this issue Nov 4, 2021
This would otherwise be caught with the improved vet printf
checker (golang/go#30436).
aalexand pushed a commit to google/pprof that referenced this issue Nov 4, 2021
* Replace fmt.Println with fmt.Printf for newline-ending string.

This would otherwise be caught with the improved vet printf
checker (golang/go#30436).

* Replace fmt.Println with fmt.Printf for newline-ending string.

This would otherwise be caught with the improved vet printf
checker (golang/go#30436).

* Avoid adding a redundant newline when printing weblistPageClosing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants