-
Notifications
You must be signed in to change notification settings - Fork 286
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
string-of-int missed a cast that go vet
caught
#498
Comments
Hi @Groxx, thanks for filling the issue (and sorry for the late response) Il will study the problem this weekend but at a first glance I suspect that the rule does not have enough type information to deduce that the function func (w *lintStringInt) isIntExpression(es []ast.Expr) bool {
...
t := w.file.Pkg.TypeOf(es[0])
if t == nil {
return false
}
... returns
Rules do not mutate the AST or the type information. Anyway to be sure you can activate only |
Hi again @Groxx , I've reproduced the bug and after analysis, I confirm that the problem's root is the lack of type information about the variable To fix, I think, we will need to evolve the type-checking mechanism in order to use the latest building libraries of the language. |
It's still kinda strange that it can't figure that type out though, as it's a direct call to a Anyway. I'm not entirely sure what you mean by the "evolve the type-checking mechanism in order to use the latest building libraries of the language" - I don't suppose there's a github issue or something around it already? |
Ah hah. I can make a minimal repro with two packages, like below. Seems like type information isn't propagated? // in a file:
import (
"fmt"
"./other"
)
func BadCast() {
i := other.ThingReturningInt()
s := string(i)
fmt.Println(s)
}
// in an "other" subfolder
package other
func ThingReturningInt() int {
return 5
} If that's known: https://pkg.go.dev/golang.org/x/tools/go/analysis can at least make that kind of lookup work, I've built tools with it in the past. It's not really suitable out-of-the-box except for "toys" due to how its runner calls |
|
The bug
I just started hooking up revive, and results are very good so far! Many thanks for this project :)
When adding the
string-of-int
check, I toyed around with the equivalentgo vet -stringintconv
check, and found a discrepancy that seems.... odd.Undoing a fix in this commit reproduces it, the
shardIDstr := string(shardID)
line below is not found:Since the same variable is used in
strconv.Itoa(shardID)
, it's pretty clear thatshardID
is an int, and this is not a line that should have succeeded.go vet -stringintconv ./...
with go 1.15.7 finds it, as further evidence:I'm really not sure where this behavior could be coming from, as the
string-of-int
code is pretty simple at a glance, and it found all the other instances.My suspicion is that there could be some mutating of the AST / type data by other rules, but I don't really know where to start looking for that.
To Reproduce
I've tried making a small repro for this, but the same kinds of lines of code + the same config file don't reproduce it... so unfortunately I can only point you to the repo where I encountered this. Thankfully it's open source!
git checkout beb64a7b
make lint | grep string-of-int
should download and build everything necessary automatically. Everything should be isolated / not install a bunch of stuff globally on your machine.Revive's version and related libraries are all pinned by the
go.mod
, and the config toml and--exclude
flags are part of themake lint
target (and they will be printed), so this should be easily reproducible.Let me know if you have any trouble running those steps, I'd be happy to try to fix it :)
The text was updated successfully, but these errors were encountered: