Skip to content

Commit

Permalink
fix: byte as argument for string conversion (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov authored May 27, 2023
1 parent 1fabc88 commit 9bcfd19
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ bin/*
dist/*
tmp/*
out*
new_funcs*
sandbox*
demo*
.task*
.ipynb*
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export PATH := $(PWD)/bin:$(PATH) # ./bin to $PATH
export SHELL := bash # Default Shell

GOPKGS := $(shell go list ./... | grep -vE "(cmd|examples|plugin|testdata|backup)" | tr -s '\n' ',' | sed 's/.\{1\}$$//' )
GOPKGS := $(shell go list ./... | grep -vE "(cmd|sandbox|testdata)" | tr -s '\n' ',' | sed 's/.\{1\}$$//' )


build:
Expand Down Expand Up @@ -33,7 +33,7 @@ test-generate:
go run ./cmd/internal/generate-tests/ "$(PWD)/testdata"

lints:
golangci-lint run --no-config ./... -D deadcode --skip-dirs "^(cmd|plugin|examples|testdata)"
golangci-lint run --no-config ./... -D deadcode --skip-dirs "^(cmd|sandbox|testdata)"


cover:
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tasks:
- make build
- make build-race
- task: lints
- make test-generate
# - make test-generate
- task: tests
- cmd: go run ./cmd/mirror/ --with-tests --with-debug ./demo
ignore_error: true
Expand Down
9 changes: 7 additions & 2 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ func (c *Checker) Handle(v *Violation, ce *ast.CallExpr) (map[int]ast.Expr, bool
}

// wrong argument type
if v.Targets == c.Type(call.Args[0]) {
typeOf := c.Type(call.Args[0])
if typeOf != Strings && typeOf != Bytes {
// rune or byte in string argument
continue
} else if v.Targets == typeOf {
// same type like string("string") <- this is bad, but other linters
// (like unconvert) can handle it
continue
}

Expand All @@ -91,7 +97,6 @@ func (c *Checker) callConverts(ce *ast.CallExpr) bool {
switch ce.Fun.(type) {
case *ast.ArrayType, *ast.Ident:
res := c.Type(ce.Fun)

return res == "[]byte" || res == "string"
}

Expand Down
17 changes: 17 additions & 0 deletions testdata/issue-26.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"strings"
)

func foobar_byte() {
var strBuilder strings.Builder
var text string = "text"
var b byte = text[0]

fmt.Printf("%T\n", b)

strBuilder.WriteString(string(b))
fmt.Println(strBuilder.String())
}

0 comments on commit 9bcfd19

Please sign in to comment.