Skip to content
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

False-positives alerts for interface type conversions #259

Open
Ragnar-BY opened this issue Nov 18, 2024 · 2 comments
Open

False-positives alerts for interface type conversions #259

Ragnar-BY opened this issue Nov 18, 2024 · 2 comments

Comments

@Ragnar-BY
Copy link

Last version golangci-lint use errcheck v1.8.0
Now errcheck gives alerts for code like:

	var a interface{}
	a = 5

	switch a.(type) {
	case string:
		fmt.Println("this is string", a.(string))
	case int:
		fmt.Println("this is int", a.(int))
	default:
		fmt.Println("unknown type")
	}

I think it is related to changes from #246
I believe that it is false-positive, because type is checked in switch-case

@kisielk
Copy link
Owner

kisielk commented Nov 18, 2024

Here's a test case if someone wants to have a crack at fixing this:

--- a/errcheck/testdata/src/assert/main.go
+++ b/errcheck/testdata/src/assert/main.go
@@ -1,5 +1,7 @@
 package assert
 
+import "fmt"
+
 func main() {
        var i interface{}
        _ = i.(string) // want "unchecked error"
@@ -16,6 +18,18 @@ func main() {
                _ = i.(int) // want "unchecked error"
        case nil:
        }
+
+       var a interface{}
+       a = 5
+
+       switch a.(type) {
+       case string:
+               fmt.Println("this is string", a.(string))
+       case int:
+               fmt.Println("this is int", a.(int))
+       default:
+               fmt.Println("unknown type")
+       }
 }

@dtcaciuc
Copy link
Collaborator

dtcaciuc commented Nov 20, 2024

Current errcheck behavior is consistent with gopls. Recommended action is to reassign variable to make it assume the resolved type
gopls-type-assert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants