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

cmd/vet : check empty "case" in "switch" #8391

Closed
gopherbot opened this issue Jul 19, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@gopherbot
Copy link

commented Jul 19, 2014

by nicolas.riesch:

When converting C code to Go, it is easy to introduce the following mistake because in
Go, "case" don't fallthrough:

---- C code ----

switch (grade) {
   case 'A' :
      printf("Excellent !\n" );
      break;
   case 'B' :
   case 'C' :
      printf("Not bad !\n" );
      break;
   default :
      printf("Try again !\n" );
   }

---- Go code ----

switch grade {
   case 'A' :
      fmt.Println("Excellent !" );
   case 'B' : <======= BUG INTRODUCED HERE
   case 'C' :
      fmt.Println("Not bad !" );
   default :
      fmt.Println("Try again !" );
   }


It is easy to forget to merge "case" clauses, as the above should be:

   case 'B',
    'C' :
      fmt.Println("Not bad !" );


It would be nice if vet could detect such consecutive CASE clauses, which are often
mistakes.

Other easier idea is to make "go fmt" write a semicolon in a line following an
empty CASE clause, just to make emtpy clauses more visible:

   case 'B' :
      ;
   case 'C' :
      fmt.Println("Not bad !" );
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2014

Comment 1:

I agree that this is an easy mistake when converting from C to Go, but it's normal in Go
to write a case clause with no statements if there is nothing to do.  We don't want to
change that in go vet or go fmt.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 25, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.