Skip to content

Commit

Permalink
checkers: fix reflect.Value suggestion in redundantSprint (#1260)
Browse files Browse the repository at this point in the history
Also enable embedded rules testing: we need to call
InitEmbeddedRules() somewhere inside the testing code
in order for them to be registered.

Fixes #1255
  • Loading branch information
quasilyte committed Aug 28, 2022
1 parent 5c89720 commit cb9153b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
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 {
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)
}
}

0 comments on commit cb9153b

Please sign in to comment.