diff --git a/checkers_bytes.go b/checkers_bytes.go index 8676385..9ad9cbc 100644 --- a/checkers_bytes.go +++ b/checkers_bytes.go @@ -92,7 +92,7 @@ var ( Function: "ContainsRune", }, Generate: &checker.Generate{ - Pattern: `ContainsRune($0, rune('ф'))`, + Pattern: `ContainsRune($0, 'ф')`, Returns: 1, }, }, diff --git a/checkers_utf8.go b/checkers_utf8.go index 703c430..f0cce64 100644 --- a/checkers_utf8.go +++ b/checkers_utf8.go @@ -38,4 +38,118 @@ var UTF8Functions = map[string]checker.Violation{ Returns: 1, }, }, + "FullRune": { + Type: checker.Function, + Message: "avoid allocations with utf8.FullRuneInString", + Args: []int{0}, + StringTargeted: false, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "FullRuneInString", + }, + Generate: &checker.Generate{ + Pattern: `FullRune($0)`, + Returns: 1, + }, + }, + "FullRuneInString": { + Type: checker.Function, + Message: "avoid allocations with utf8.FullRune", + Args: []int{0}, + StringTargeted: true, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "FullRune", + }, + Generate: &checker.Generate{ + Pattern: `FullRuneInString($0)`, + Returns: 1, + }, + }, + + "RuneCount": { + Type: checker.Function, + Message: "avoid allocations with utf8.RuneCountInString", + Args: []int{0}, + StringTargeted: false, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "RuneCountInString", + }, + Generate: &checker.Generate{ + Pattern: `RuneCount($0)`, + Returns: 1, + }, + }, + "RuneCountInString": { + Type: checker.Function, + Message: "avoid allocations with utf8.RuneCount", + Args: []int{0}, + StringTargeted: true, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "RuneCount", + }, + Generate: &checker.Generate{ + Pattern: `RuneCountInString($0)`, + Returns: 1, + }, + }, + + "DecodeLastRune": { + Type: checker.Function, + Message: "avoid allocations with utf8.DecodeLastRuneInString", + Args: []int{0}, + StringTargeted: false, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "DecodeLastRuneInString", + }, + Generate: &checker.Generate{ + Pattern: `DecodeLastRune($0)`, + Returns: 2, + }, + }, + "DecodeLastRuneInString": { + Type: checker.Function, + Message: "avoid allocations with utf8.DecodeLastRune", + Args: []int{0}, + StringTargeted: true, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "DecodeLastRune", + }, + Generate: &checker.Generate{ + Pattern: `DecodeLastRuneInString($0)`, + Returns: 2, + }, + }, + "DecodeRune": { + Type: checker.Function, + Message: "avoid allocations with utf8.DecodeRuneInString", + Args: []int{0}, + StringTargeted: false, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "DecodeRuneInString", + }, + Generate: &checker.Generate{ + Pattern: `DecodeRune($0)`, + Returns: 2, + }, + }, + "DecodeRuneInString": { + Type: checker.Function, + Message: "avoid allocations with utf8.DecodeRune", + Args: []int{0}, + StringTargeted: true, + Alternative: checker.Alternative{ + Package: "unicode/utf8", + Function: "DecodeRune", + }, + Generate: &checker.Generate{ + Pattern: `DecodeRuneInString($0)`, + Returns: 2, + }, + }, } diff --git a/testdata/bytes.go b/testdata/bytes.go index 0a547fe..f2bc26c 100644 --- a/testdata/bytes.go +++ b/testdata/bytes.go @@ -162,32 +162,32 @@ func main_bytes() { { - _ = bytes.ContainsRune([]byte("foobar"), rune('ф')) // want `avoid allocations with strings\.ContainsRune` + _ = bytes.ContainsRune([]byte("foobar"), 'ф') // want `avoid allocations with strings\.ContainsRune` } { - _ = bytes.ContainsRune([]byte{'f','o','o','b','a','r'}, rune('ф')) + _ = bytes.ContainsRune([]byte{'f','o','o','b','a','r'}, 'ф') } { - _ = ContainsRune([]byte("foobar"), rune('ф')) // want `avoid allocations with strings\.ContainsRune` + _ = ContainsRune([]byte("foobar"), 'ф') // want `avoid allocations with strings\.ContainsRune` } { - _ = ContainsRune([]byte{'f','o','o','b','a','r'}, rune('ф')) + _ = ContainsRune([]byte{'f','o','o','b','a','r'}, 'ф') } { - _ = pkg.ContainsRune([]byte("foobar"), rune('ф')) // want `avoid allocations with strings\.ContainsRune` + _ = pkg.ContainsRune([]byte("foobar"), 'ф') // want `avoid allocations with strings\.ContainsRune` } { - _ = pkg.ContainsRune([]byte{'f','o','o','b','a','r'}, rune('ф')) + _ = pkg.ContainsRune([]byte{'f','o','o','b','a','r'}, 'ф') } { diff --git a/testdata/utf8.go b/testdata/utf8.go index 042a44a..636c8e7 100644 --- a/testdata/utf8.go +++ b/testdata/utf8.go @@ -10,6 +10,246 @@ import ( func main_utf8() { + { + + _,_ = utf8.DecodeLastRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeLastRuneInString` + } + + { + + _,_ = utf8.DecodeLastRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = DecodeLastRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeLastRuneInString` + } + + { + + _,_ = DecodeLastRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = pkg.DecodeLastRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeLastRuneInString` + } + + { + + _,_ = pkg.DecodeLastRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = utf8.DecodeLastRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeLastRune` + } + + { + + _,_ = utf8.DecodeLastRuneInString("foobar") + } + + { + + _,_ = DecodeLastRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeLastRune` + } + + { + + _,_ = DecodeLastRuneInString("foobar") + } + + { + + _,_ = pkg.DecodeLastRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeLastRune` + } + + { + + _,_ = pkg.DecodeLastRuneInString("foobar") + } + + { + + _,_ = utf8.DecodeRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeRuneInString` + } + + { + + _,_ = utf8.DecodeRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = DecodeRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeRuneInString` + } + + { + + _,_ = DecodeRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = pkg.DecodeRune([]byte("foobar")) // want `avoid allocations with utf8\.DecodeRuneInString` + } + + { + + _,_ = pkg.DecodeRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _,_ = utf8.DecodeRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeRune` + } + + { + + _,_ = utf8.DecodeRuneInString("foobar") + } + + { + + _,_ = DecodeRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeRune` + } + + { + + _,_ = DecodeRuneInString("foobar") + } + + { + + _,_ = pkg.DecodeRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.DecodeRune` + } + + { + + _,_ = pkg.DecodeRuneInString("foobar") + } + + { + + _ = utf8.FullRune([]byte("foobar")) // want `avoid allocations with utf8\.FullRuneInString` + } + + { + + _ = utf8.FullRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = FullRune([]byte("foobar")) // want `avoid allocations with utf8\.FullRuneInString` + } + + { + + _ = FullRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = pkg.FullRune([]byte("foobar")) // want `avoid allocations with utf8\.FullRuneInString` + } + + { + + _ = pkg.FullRune([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = utf8.FullRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.FullRune` + } + + { + + _ = utf8.FullRuneInString("foobar") + } + + { + + _ = FullRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.FullRune` + } + + { + + _ = FullRuneInString("foobar") + } + + { + + _ = pkg.FullRuneInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.FullRune` + } + + { + + _ = pkg.FullRuneInString("foobar") + } + + { + + _ = utf8.RuneCount([]byte("foobar")) // want `avoid allocations with utf8\.RuneCountInString` + } + + { + + _ = utf8.RuneCount([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = RuneCount([]byte("foobar")) // want `avoid allocations with utf8\.RuneCountInString` + } + + { + + _ = RuneCount([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = pkg.RuneCount([]byte("foobar")) // want `avoid allocations with utf8\.RuneCountInString` + } + + { + + _ = pkg.RuneCount([]byte{'f','o','o','b','a','r'}) + } + + { + + _ = utf8.RuneCountInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.RuneCount` + } + + { + + _ = utf8.RuneCountInString("foobar") + } + + { + + _ = RuneCountInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.RuneCount` + } + + { + + _ = RuneCountInString("foobar") + } + + { + + _ = pkg.RuneCountInString(string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with utf8\.RuneCount` + } + + { + + _ = pkg.RuneCountInString("foobar") + } + { _ = utf8.Valid([]byte("foobar")) // want `avoid allocations with utf8\.ValidString`