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

checkers: fix reflect.Value suggestion in redundantSprint #1260

Merged
merged 1 commit into from Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions checkers/checkers_test.go
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/google/go-cmp/cmp"
)

func init() {
if err := InitEmbeddedRules(); err != nil {
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
panic(err) // Should never happen
}
}

func TestCheckers(t *testing.T) {
allParams := map[string]map[string]interface{}{
"captLocal": {"paramsOnly": false},
Expand Down
2 changes: 1 addition & 1 deletion checkers/rules/rules.go
Expand Up @@ -10,7 +10,7 @@ import (
//doc:after x.String()
func redundantSprint(m dsl.Matcher) {
m.Match(`fmt.Sprint($x)`, `fmt.Sprintf("%s", $x)`, `fmt.Sprintf("%v", $x)`).
Where(m["x"].Type.Implements(`fmt.Stringer`)).
Where(!m["x"].Type.Is(`reflect.Value`) && m["x"].Type.Implements(`fmt.Stringer`)).
Suggest(`$x.String()`).
Report(`use $x.String() instead`)

Expand Down
29 changes: 24 additions & 5 deletions checkers/rulesdata/rulesdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions checkers/testdata/redundantSprint/negative_tests.go
@@ -1,5 +1,10 @@
package checker_test

import (
"fmt"
"reflect"
)

func _() {
{
var foo withStringer
Expand All @@ -17,4 +22,11 @@ func _() {

_ = "x"
}

{
var rv reflect.Value
_ = fmt.Sprint(rv)
_ = fmt.Sprintf("%s", rv)
_ = fmt.Sprintf("%v", rv)
}
}