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

chore: add missing tests for var-declarations, fix any/interface{} type issue #1154

Merged
merged 4 commits into from
Dec 2, 2024

Conversation

denisvmedia
Copy link
Collaborator

Fixes #1152

testdata/var_declarations_type_inference.go Outdated Show resolved Hide resolved
testdata/var_declarations_zero_value.go Outdated Show resolved Hide resolved

func zeroValueExample() {
var y int // No warning, zero value is implicit
var z1 any = 0 // No warning, zero value for any is nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, in cases of the form var <id> any = <value> where value is not nil the rule must warn with should omit type any from declaration of var <id>; it will be inferred from the right-hand side because var <id> = <value> is equivalent

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, are you saying that var <id> any = <non_nil_value> is the same as var <id> = <value>?

Copy link
Collaborator

@chavacava chavacava Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

https://go.dev/play/p/TORLftc7jlR with any
https://go.dev/play/p/dYvM0OlUstA without any

both outputs are the same

a value's is 100 and it is of type int

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if an initial value is provided then declaring the variable of being type any has no utility

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, I think there is a difference. With any it is a dynamic type and it can be changed to any value later. Consider this:

package main

func main() {
	var a1 = 1
	// a1 = "string" // fail to compile
	var a2 any = 1
	a2 = "string" // this will compile
	_ = a1
	_ = a2
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declaring the variable of being type any has no utility

I believe there can be a reason to declare an any type variable in case if you plan to have an arbitrary value for it later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops; you are right... I did not think in these kind of (ugly) cases

@denisvmedia
Copy link
Collaborator Author

@chavacava I updated the tests. Are you fine with the current state?

@ccoVeille
Copy link
Contributor

I don't understand the last commit that provide a strange import order

@denisvmedia
Copy link
Collaborator Author

It reverts what I committed by mistake.

@denisvmedia denisvmedia merged commit dde8344 into master Dec 2, 2024
5 checks passed
@denisvmedia denisvmedia deleted the var-declarations-fix branch December 2, 2024 12:06
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

Successfully merging this pull request may close these issues.

False positive with var-declaration
3 participants