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

compiler: switch statements report false positive "declared and not used" errors #230

Closed
zapateo opened this issue Jul 30, 2019 · 2 comments
Labels
bug Bug: something already implemented does not work as it should tchecker Related to type checker

Comments

@zapateo
Copy link
Member

zapateo commented Jul 30, 2019

package main

func main() {
	var x interface{}
	switch x := x.(type) {
	case float32:
		_ = x
	default:
	}
}

returns error: main:5:9: x declared and not used

The reason is the following: x is declared and marked as used by the float32 case, but it's not used by the default case. A possible solution could be declaring x outside the float32 case, but this is not possibile: x has as different type according to the case where it's used, so must have a different declaration for every case of the switch.

@zapateo zapateo added bug Bug: something already implemented does not work as it should tchecker Related to type checker labels Jul 30, 2019
@zapateo
Copy link
Member Author

zapateo commented Aug 1, 2019

A workaround for the problem is using the variable in every case.

package main

func main() {
	var y interface{}
	switch x := y.(type) {
	case float32:
		_ = x
	default:
		_ = x
	}
}

@zapateo zapateo changed the title compiler: false positive "declared and not used" compiler: switch statements report false positive "declared and not used" errors Aug 1, 2019
@gazerro gazerro added this to the Milestone 1 milestone Aug 17, 2019
zapateo added a commit that referenced this issue Nov 20, 2019
* Reference to #467.
* Reference to #458.
* Reference to #230.
* Reference to #278.
zapateo added a commit that referenced this issue Nov 25, 2019
* See #230.
* Fix test 'typeswitch.go'
* Enable the test 'scriggo_by_example/switch.go'.
@zapateo
Copy link
Member Author

zapateo commented Nov 25, 2019

The commit c2b582d marks every identifier declared in a type switch statement as used, so this error is not reported any more.
Now the error is not reported even if the variable is never used: see #474 for more informations.

@zapateo zapateo closed this as completed Nov 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug: something already implemented does not work as it should tchecker Related to type checker
Projects
None yet
Development

No branches or pull requests

2 participants