This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Potential false positive: variable used in multiple if statements #23
Comments
This was referenced Nov 12, 2021
Could you please advise what version of golangci-lint are you using? I ran v1.42.1 on the following function ten times in a row and it didn't report any findings. func notUsed_IfStmt_MultipleIfStmt_OK() {
err := getValue()
isDeleteNodeAllowed := err == nil
if isDeleteNodeAllowed {
fmt.Println("abc")
}
if isDeleteNodeAllowed {
fmt.Println("def")
}
} |
I'm using |
Sorry as a full example: main.go: package main
import (
"fmt"
)
func main() {
err := fmt.Errorf("test")
isDeleteNodeAllowed := err == nil
if isDeleteNodeAllowed {
fmt.Println("abc")
}
if isDeleteNodeAllowed {
fmt.Println("def")
}
} .golangci.yml: linters:
disable-all: true
enable:
- ifshort
linters-settings:
ifshort:
# Maximum length of variable declaration measured in number of characters, after which linter won't suggest using short syntax.
max-decl-chars: 50 output: main.go:11:2: variable 'isDeleteNodeAllowed' is only used in the if-statement (main.go:9:2); consider using short syntax (ifshort)
isDeleteNodeAllowed := err == nil |
I encountered a similar issue, I've been able to reproduce it with the following code: package main
func main() {
x := 0
for i := 0; i < 10; i++ {
if i%2 == 0 {
continue
} else {
x++
}
}
if x > 0 {
println(x)
}
} And the output of
|
Another case where a similar false positive occurs: err := doSomething()
doSomethingElse()
if err != nil {
fmr.Printf("Failed: %s\n", err)
} I am using |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm not sure if it's intentional but the following code produces a finding:
(full original code can be seen here, but the short version above also produces the finding)
In case it's not intentional, I think the problem is that
getNamedOccurrenceMap=>addFromCondition
only adds one occurrence for the first if. The second if is not added as additional occurrence becauseaddFromIdent=>occ.isComplete
returns true.Interestingly this issue is only reported in ~90-95% of the cases when ifshort is run.
Context: We are running ifshort via golangci-lint and we are also running the nolint linter.
For some reason that finding (on the original code) is not reported on every run. We've added a nolint statement, but in those cases where ifshort does not report the finding we'll get a finding that the nolint statement is not required.
The text was updated successfully, but these errors were encountered: