-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: potential false positive in the "suspect or" check #28446
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
Comments
Edit: never mind, tired eyes should not review code. |
I'm not sure I follow this bug report. All three names are constants, and they're equal, so I presume that vet is seeing the condition as It's not that vet doesn't like conditions that are always true or false. I believe this check is to spot human error when writing repetitive boolean expressions. You're right that |
... will be always false iff the three constants are equal. And that's the very point of the test. If anyone later changes any of the {szKey,szVal,szBuf} constants, each of which tunes an independent thing, in a way such that they become not equal, the condition will become true and the |
My quick 2c: This is a false positive. But it’s the first one like we have ever had reported (and it has been years), and I don’t see a clean way to fix it, and there’s an easy workaround available. So let’s just take the workaround for now. |
Yes - that's why I started the paragraph with "all three names are constants" :) I don't think I've ever seen an init check to see if constants have been tampered with. Is that something that you expect your users to do? I think this is why this false positive has never been brought up before, like Josh says. |
I'm the prime suspect any time in the future. Been there, done that ;-) |
My previous comments weren't completely correct. The check documentation reads:
So this kind of expression is exactly what the vet check is supposed to flag. I mostly agree with Josh, that it should be easy enough for now to avoid this pattern and see if there are any other false positives reported in the future. I was going to pull in the boolean check author for some second thoughts, but he's ahead of me :)
|
I still don't get where from comes the idea that there's anything wrong with the above quoted expressions. Please ELI5 why, for example, a test for violating invariant If some/all of |
I think it doesn't distinguish named constants from unnamed ones. Perhaps it should - that would cover this false positive, but it might introduce some false negatives. It looks like none of the current tests use user-defined constants, so that might work. @josharian thoughts? |
That's correct. I think it'd be useful to gather some data from a corpus about the nature of the constant expressions when this check fires. |
I think the ultimate solution is to make this a warning. Along with unused import/variables, short edits in code for debugging is painful in Go. |
This comment has been minimized.
This comment has been minimized.
@dfang This is not related to the issue reported in this thread, assuming This:
is equivalent to Moreover, this:
is not equivalent to
so your change is not correct. For example, if |
Falling in the last line is weird because I don't see why you would implement x == a == b but @cznic is right the column |
See github.com/golang/go/issues/28446 for detail
* Combine all gocb loggers into a shared method * Work around vet false-positive for const equality checks. See github.com/golang/go/issues/28446 for detail * Move gocb log level equality checking to a test
What version of Go are you using (
go version
)?go version go1.11.1 linux/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?What did you do?
Using
github.com/cznic/ql
, branchfile2
, checked out at3497607bfaba518eab540d317a6c168396b8002f
.What did you expect to see?
Build succeeds, tests are executed and failures reported.
What did you see instead?
Build fails.
Additional info
The line go vet does not like is here
Line 37 tests for equality of the three values
szKey, szValue, szBuf
which other code depends on as it would have to be more complicated for the general case and the equality allow simplifying it. I see nothing wrong about the line, I donť thinkvet
should reject it.The
vet
test is located hereThe comment
if c1 and c2 are different then it's always true or always false.
is correct, but that does not imply something is wrong.There's nothing wrong in implementing
in Go as
Where the negation, rejected by
vet
, isWorkaround:
vet
is happy if the expression is rewritten aseven though the two versions are logical identities.
The text was updated successfully, but these errors were encountered: