From c1934b75d054975b79a8179cb6f0a9b8b3fa33cd Mon Sep 17 00:00:00 2001 From: smasher164 Date: Sat, 30 May 2020 01:52:20 -0400 Subject: [PATCH] go/analysis: improve error message for string(int) warning To an experienced Go user, the previous error message doesn't describe the error--it merely states a fact. The new error message asks if the user meant fmt.Sprint(x). If they used the conversion correctly, the suggested fix to replace string(int) with string(rune(int)) is understandable and valid. On the other hand, if they used it to print a digit representation, asking that in the error message further emphasizes the mistake. Updates golang/go#39151. Change-Id: Iab9cdcaf53e9ca134285246fad0546d6f24c3983 Reviewed-on: https://go-review.googlesource.com/c/tools/+/235797 Run-TryBot: Akhil Indurti Reviewed-by: Bryan C. Mills Reviewed-by: Jay Conrod TryBot-Result: Gobot Gobot --- go/analysis/passes/stringintconv/string.go | 2 +- go/analysis/passes/stringintconv/testdata/src/a/a.go | 12 ++++++------ .../passes/stringintconv/testdata/src/a/a.go.golden | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/go/analysis/passes/stringintconv/string.go b/go/analysis/passes/stringintconv/string.go index ac2cd84ad3..7a005901e8 100644 --- a/go/analysis/passes/stringintconv/string.go +++ b/go/analysis/passes/stringintconv/string.go @@ -101,7 +101,7 @@ func run(pass *analysis.Pass) (interface{}, error) { } diag := analysis.Diagnostic{ Pos: n.Pos(), - Message: fmt.Sprintf("conversion from %s to %s yields a string of one rune", source, target), + Message: fmt.Sprintf("conversion from %s to %s yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)", source, target), SuggestedFixes: []analysis.SuggestedFix{ { Message: "Did you mean to convert a rune to a string?", diff --git a/go/analysis/passes/stringintconv/testdata/src/a/a.go b/go/analysis/passes/stringintconv/testdata/src/a/a.go index 72ceb970cb..837469c194 100644 --- a/go/analysis/passes/stringintconv/testdata/src/a/a.go +++ b/go/analysis/passes/stringintconv/testdata/src/a/a.go @@ -25,12 +25,12 @@ func StringTest() { o struct{ x int } ) const p = 0 - _ = string(i) // want `^conversion from int to string yields a string of one rune$` + _ = string(i) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` _ = string(j) _ = string(k) - _ = string(p) // want `^conversion from untyped int to string yields a string of one rune$` - _ = A(l) // want `^conversion from C \(int\) to A \(string\) yields a string of one rune$` - _ = B(m) // want `^conversion from uintptr to B \(string\) yields a string of one rune$` - _ = string(n[1]) // want `^conversion from int to string yields a string of one rune$` - _ = string(o.x) // want `^conversion from int to string yields a string of one rune$` + _ = string(p) // want `^conversion from untyped int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = A(l) // want `^conversion from C \(int\) to A \(string\) yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = B(m) // want `^conversion from uintptr to B \(string\) yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = string(n[1]) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = string(o.x) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` } diff --git a/go/analysis/passes/stringintconv/testdata/src/a/a.go.golden b/go/analysis/passes/stringintconv/testdata/src/a/a.go.golden index 9538a0132f..593962d7a9 100644 --- a/go/analysis/passes/stringintconv/testdata/src/a/a.go.golden +++ b/go/analysis/passes/stringintconv/testdata/src/a/a.go.golden @@ -25,12 +25,12 @@ func StringTest() { o struct{ x int } ) const p = 0 - _ = string(rune(i)) // want `^conversion from int to string yields a string of one rune$` + _ = string(rune(i)) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` _ = string(j) _ = string(k) - _ = string(rune(p)) // want `^conversion from untyped int to string yields a string of one rune$` - _ = A(rune(l)) // want `^conversion from C \(int\) to A \(string\) yields a string of one rune$` - _ = B(rune(m)) // want `^conversion from uintptr to B \(string\) yields a string of one rune$` - _ = string(rune(n[1])) // want `^conversion from int to string yields a string of one rune$` - _ = string(rune(o.x)) // want `^conversion from int to string yields a string of one rune$` + _ = string(rune(p)) // want `^conversion from untyped int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = A(rune(l)) // want `^conversion from C \(int\) to A \(string\) yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = B(rune(m)) // want `^conversion from uintptr to B \(string\) yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = string(rune(n[1])) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` + _ = string(rune(o.x)) // want `^conversion from int to string yields a string of one rune, not a string of digits \(did you mean fmt\.Sprint\(x\)\?\)$` }