Skip to content

Commit

Permalink
checkers: add new checker stringsCompare (#1195)
Browse files Browse the repository at this point in the history
Updates #1167
  • Loading branch information
peakle committed Feb 5, 2022
1 parent 0ad1a33 commit fc5746a
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
17 changes: 17 additions & 0 deletions checkers/rules/rules.go
Expand Up @@ -736,3 +736,20 @@ func dynamicFmtString(m dsl.Matcher) {
Suggest("errors.New($f($*args))").
Report(`use errors.New($f($*args)) or fmt.Errorf("%s", $f($*args)) instead`)
}

//doc:summary Detects strings.Compare usage
//doc:tags style experimental
//doc:before strings.Compare(x, y)
//doc:after x < y
func stringsCompare(m dsl.Matcher) {
m.Match(`strings.Compare($s1, $s2) == 0`).
Suggest(`$s1 == $s2`)

m.Match(`strings.Compare($s1, $s2) == -1`,
`strings.Compare($s1, $s2) < 0`).
Suggest(`$s1 < $s2`)

m.Match(`strings.Compare($s1, $s2) == 1`,
`strings.Compare($s1, $s2) > 0`).
Suggest(`$s1 > $s2`)
}
40 changes: 40 additions & 0 deletions checkers/rulesdata/rulesdata.go

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

78 changes: 78 additions & 0 deletions checkers/testdata/stringsCompare/negative_tests.go
@@ -0,0 +1,78 @@
package checker_test

import (
"bytes"
"strings"
)

type s []string

func (s) Compare(x, y string) int {
if x < y {
return 1
}
return 0
}

func negative() {
bytes.Compare([]byte{}, []byte{})

strings := s{}
_ = strings.Compare("1", "3") == 0
}

func negative2() {
var a, b = "aaa", "bbb"
if a > b {
print(1)
}

if a == b {
print(0)
}

if a < b {
print(-1)
}

switch a > b {
case true:
print(1)
case false:
print(0, -1)
}
}

func negative3() {
if "aaa" > "bbb" {
print(1)
}

if "aaa" == "bbb" {
print(0)
}

if "aaa" < "bbb" {
print(-1)
}

switch "aaa" > "bbb" {
case true:
print(1)
case false:
print(0, -1)
}
}

func negative4() {
f, b := "aaa", "bbb"

_ = strings.Compare(f, b) > -100
_ = strings.Compare(f, b) < 100
_ = strings.Compare(f, b) >= -1
_ = strings.Compare(f, b) <= -1
_ = strings.Compare(f, b) >= 1
_ = strings.Compare(f, b) <= 1
_ = strings.Compare(f, b) >= 0
_ = strings.Compare(f, b) <= 0
}
36 changes: 36 additions & 0 deletions checkers/testdata/stringsCompare/positive_tests.go
@@ -0,0 +1,36 @@
package checker_test

import (
"strings"
)

func warning() {
f, b := "aaa", "bbb"

/*! suggestion: f == b */
if strings.Compare(f, b) == 0 {
}

/*! suggestion: f > b */
switch foo := strings.Compare(f, b) > 0; foo {
case true:
print(0)
case false:
print(1)
}

/*! suggestion: "s" < "ww" */
_ = strings.Compare("s", "ww") < 0
/*! suggestion: "s" == "ww" */
_ = strings.Compare("s", "ww") == 0
/*! suggestion: "s" > "ww" */
_ = strings.Compare("s", "ww") > 0

/*! suggestion: "s" > "ww" */
_ = strings.Compare("s", "ww") > 0

/*! suggestion: "s" > "ww" */
_ = strings.Compare("s", "ww") == 1
/*! suggestion: "s" < "ww" */
_ = strings.Compare("s", "ww") == -1
}

0 comments on commit fc5746a

Please sign in to comment.