Text file src/go/printer/testdata/statements.input

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package statements
     6  
     7  var expr bool
     8  
     9  func use(x interface{}) {}
    10  
    11  // Formatting of multi-line return statements.
    12  func _f() {
    13  	return
    14  	return x, y, z
    15  	return T{}
    16  	return T{1, 2, 3},
    17  		x, y, z
    18  	return T{1, 2, 3},
    19  		x, y,
    20  		z
    21  	return T{1,
    22  		2,
    23  		3}
    24  	return T{1,
    25  		2,
    26  		3,
    27  	}
    28  	return T{
    29  		1,
    30  		2,
    31  		3}
    32  	return T{
    33  		1,
    34  		2,
    35  		3,
    36  	}
    37  	return T{
    38  		1,
    39  		T{1, 2, 3},
    40  		3,
    41  	}
    42  	return T{
    43  		1,
    44  		T{1,
    45  			2, 3},
    46  		3,
    47  	}
    48  	return T{
    49  		1,
    50  		T{1,
    51  			2,
    52  			3},
    53  		3,
    54  	}
    55  	return T{
    56  			1,
    57  			2,
    58  		}, nil
    59  	return T{
    60  			1,
    61  			2,
    62  		},
    63  		T{
    64  			x: 3,
    65  			y: 4,
    66  		}, nil
    67  	return T{
    68  			1,
    69  			2,
    70  		},
    71  		nil
    72  	return T{
    73  			1,
    74  			2,
    75  		},
    76  		T{
    77  			x: 3,
    78  			y: 4,
    79  		},
    80  		nil
    81  	return x + y +
    82  		z
    83  	return func() {}
    84  	return func() {
    85  		_ = 0
    86  	}, T{
    87  		1, 2,
    88  	}
    89  	return func() {
    90  		_ = 0
    91  	}
    92  	return func() T {
    93  		return T {
    94  			1, 2,
    95  		}
    96  	}
    97  }
    98  
    99  // Formatting of multi-line returns: test cases from issue 1207.
   100  func F() (*T, os.Error) {
   101         return &T{
   102                 X: 1,
   103                 Y: 2,
   104         },
   105                 nil
   106  }
   107  
   108  func G() (*T, *T, os.Error) {
   109         return &T{
   110                 X: 1,
   111                 Y: 2,
   112         },
   113                 &T{
   114                         X: 3,
   115                         Y: 4,
   116                 },
   117                 nil
   118  }
   119  
   120  func _() interface{} {
   121  	return &fileStat{
   122  			name:    basename(file.name),
   123  			size:    mkSize(d.FileSizeHigh, d.FileSizeLow),
   124  			modTime: mkModTime(d.LastWriteTime),
   125  			mode:    mkMode(d.FileAttributes),
   126  			sys:     mkSysFromFI(&d),
   127  		}, nil
   128  }
   129  
   130  // Formatting of if-statement headers.
   131  func _() {
   132  	if true {}
   133  	if; true {}  // no semicolon printed
   134  	if expr{}
   135  	if;expr{}  // no semicolon printed
   136  	if (expr){}  // no parens printed
   137  	if;((expr)){}  // no semicolon and parens printed
   138  	if x:=expr;true{
   139  	use(x)}
   140  	if x:=expr; expr {use(x)}
   141  }
   142  
   143  
   144  // Formatting of switch-statement headers.
   145  func _() {
   146  	switch {}
   147  	switch;{}  // no semicolon printed
   148  	switch expr {}
   149  	switch;expr{}  // no semicolon printed
   150  	switch (expr) {}  // no parens printed
   151  	switch;((expr)){}  // no semicolon and parens printed
   152  	switch x := expr; { default:use(
   153  x)
   154  	}
   155  	switch x := expr; expr {default:use(x)}
   156  }
   157  
   158  
   159  // Formatting of switch statement bodies.
   160  func _() {
   161  	switch {
   162  	}
   163  
   164  	switch x := 0; x {
   165  	case 1:
   166  		use(x)
   167  		use(x)  // followed by an empty line
   168  
   169  	case 2:  // followed by an empty line
   170  
   171  		use(x)  // followed by an empty line
   172  
   173  	case 3:  // no empty lines
   174  		use(x)
   175  		use(x)
   176  	}
   177  
   178  	switch x {
   179  	case 0:
   180  		use(x)
   181  	case 1:  // this comment should have no effect on the previous or next line
   182  		use(x)
   183  	}
   184  
   185  	switch x := 0; x {
   186  	case 1:
   187  		x = 0
   188  		// this comment should be indented
   189  	case 2:
   190  		x = 0
   191  	// this comment should not be indented, it is aligned with the next case
   192  	case 3:
   193  		x = 0
   194  		/* indented comment
   195  		   aligned
   196  		   aligned
   197  		*/
   198  		// bla
   199  		/* and more */
   200  	case 4:
   201  		x = 0
   202  	/* not indented comment
   203  	   aligned
   204  	   aligned
   205  	*/
   206  	// bla
   207  	/* and more */
   208  	case 5:
   209  	}
   210  }
   211  
   212  
   213  // Formatting of selected select statements.
   214  func _() {
   215  	select {
   216  	}
   217  	select { /* this comment should not be tab-aligned because the closing } is on the same line */ }
   218  	select { /* this comment should be tab-aligned */
   219  	}
   220  	select { // this comment should be tab-aligned
   221  	}
   222  	select { case <-c: }
   223  }
   224  
   225  
   226  // Formatting of for-statement headers for single-line for-loops.
   227  func _() {
   228  	for{}
   229  	for expr {}
   230  	for (expr) {}  // no parens printed
   231  	for;;{}  // no semicolons printed
   232  	for x :=expr;; {use( x)}
   233  	for; expr;{}  // no semicolons printed
   234  	for; ((expr));{}  // no semicolons and parens printed
   235  	for; ; expr = false {}
   236  	for x :=expr; expr; {use(x)}
   237  	for x := expr;; expr=false {use(x)}
   238  	for;expr;expr =false {}
   239  	for x := expr;expr;expr = false { use(x) }
   240  	for x := range []int{} { use(x) }
   241  	for x := range (([]int{})) { use(x) }  // no parens printed
   242  }
   243  
   244  
   245  // Formatting of for-statement headers for multi-line for-loops.
   246  func _() {
   247  	for{
   248  	}
   249  	for expr {
   250  	}
   251  	for (expr) {
   252  	}  // no parens printed
   253  	for;;{
   254  	}  // no semicolons printed
   255  	for x :=expr;; {use( x)
   256  	}
   257  	for; expr;{
   258  	}  // no semicolons printed
   259  	for; ((expr));{
   260  	}  // no semicolons and parens printed
   261  	for; ; expr = false {
   262  	}
   263  	for x :=expr; expr; {use(x)
   264  	}
   265  	for x := expr;; expr=false {use(x)
   266  	}
   267  	for;expr;expr =false {
   268  	}
   269  	for x := expr;expr;expr = false {
   270  	use(x)
   271  	}
   272  	for range []int{} {
   273  	println("foo")}
   274  	for x := range []int{} {
   275  	use(x) }
   276  	for x := range (([]int{})) {
   277  	use(x) }  // no parens printed
   278  }
   279  
   280  
   281  // Formatting of selected short single- and multi-line statements.
   282  func _() {
   283  	if cond {}
   284  	if cond {
   285  	} // multiple lines
   286  	if cond {} else {} // else clause always requires multiple lines
   287  
   288  	for {}
   289  	for i := 0; i < len(a); 1++ {}
   290  	for i := 0; i < len(a); 1++ { a[i] = i }
   291  	for i := 0; i < len(a); 1++ { a[i] = i
   292  	} // multiple lines
   293  
   294  	for range a{}
   295  	for _ = range a{}
   296  	for _, _ = range a{}
   297  	for i := range a {}
   298  	for i := range a { a[i] = i }
   299  	for i := range a { a[i] = i
   300  	} // multiple lines
   301  
   302  	go func() { for { a <- <-b } }()
   303  	defer func() { if x := recover(); x != nil { err = fmt.Sprintf("error: %s", x.msg) } }()
   304  }
   305  
   306  
   307  // Don't remove mandatory parentheses around composite literals in control clauses.
   308  func _() {
   309  	// strip parentheses - no composite literals or composite literals don't start with a type name
   310  	if (x) {}
   311  	if (((x))) {}
   312  	if ([]T{}) {}
   313  	if (([]T{})) {}
   314  	if ; (((([]T{})))) {}
   315  
   316  	for (x) {}
   317  	for (((x))) {}
   318  	for ([]T{}) {}
   319  	for (([]T{})) {}
   320  	for ; (((([]T{})))) ; {}
   321  
   322  	switch (x) {}
   323  	switch (((x))) {}
   324  	switch ([]T{}) {}
   325  	switch ; (((([]T{})))) {}
   326  
   327  	for _ = range ((([]T{T{42}}))) {}
   328  
   329  	// leave parentheses - composite literals start with a type name
   330  	if (T{}) {}
   331  	if ((T{})) {}
   332  	if ; ((((T{})))) {}
   333  
   334  	for (T{}) {}
   335  	for ((T{})) {}
   336  	for ; ((((T{})))) ; {}
   337  
   338  	switch (T{}) {}
   339  	switch ; ((((T{})))) {}
   340  
   341  	for _ = range (((T1{T{42}}))) {}
   342  
   343  	if x == (T{42}[0]) {}
   344  	if (x == T{42}[0]) {}
   345  	if (x == (T{42}[0])) {}
   346  	if (x == (((T{42}[0])))) {}
   347  	if (((x == (T{42}[0])))) {}
   348  	if x == a + b*(T{42}[0]) {}
   349  	if (x == a + b*T{42}[0]) {}
   350  	if (x == a + b*(T{42}[0])) {}
   351  	if (x == a + ((b * (T{42}[0])))) {}
   352  	if (((x == a + b * (T{42}[0])))) {}
   353  	if (((a + b * (T{42}[0])) == x)) {}
   354  	if (((a + b * (T{42}[0])))) == x {}
   355  
   356  	if (struct{x bool}{false}.x) {}
   357  	if (struct{x bool}{false}.x) == false {}
   358  	if (struct{x bool}{false}.x == false) {}
   359  }
   360  
   361  
   362  // Extra empty lines inside functions. Do respect source code line
   363  // breaks between statement boundaries but print at most one empty
   364  // line at a time.
   365  func _() {
   366  
   367  	const _ = 0
   368  
   369  	const _ = 1
   370  	type _ int
   371  	type _ float
   372  
   373  	var _ = 0
   374  	var x = 1
   375  
   376  	// Each use(x) call below should have at most one empty line before and after.
   377  	// Known bug: The first use call may have more than one empty line before
   378  	//            (see go/printer/nodes.go, func linebreak).
   379  
   380  
   381  
   382  	use(x)
   383  
   384  	if x < x {
   385  
   386  		use(x)
   387  
   388  	} else {
   389  
   390  		use(x)
   391  
   392  	}
   393  }
   394  
   395  
   396  // Formatting around labels.
   397  func _() {
   398  	L:
   399  }
   400  
   401  
   402  func _() {
   403  	// this comment should be indented
   404  	L: ;  // no semicolon needed
   405  }
   406  
   407  
   408  func _() {
   409  	switch 0 {
   410  	case 0:
   411  		L0: ;  // semicolon required
   412  	case 1:
   413  		L1: ;  // semicolon required
   414  	default:
   415  		L2: ;  // no semicolon needed
   416  	}
   417  }
   418  
   419  
   420  func _() {
   421  	f()
   422  L1:
   423  	f()
   424  L2:
   425  	;
   426  L3:
   427  }
   428  
   429  
   430  func _() {
   431  	// this comment should be indented
   432  	L:
   433  }
   434  
   435  
   436  func _() {
   437  	L: _ = 0
   438  }
   439  
   440  
   441  func _() {
   442  	// this comment should be indented
   443  	L: _ = 0
   444  }
   445  
   446  
   447  func _() {
   448  	for {
   449  	L1: _ = 0
   450  	L2:
   451  		_ = 0
   452  	}
   453  }
   454  
   455  
   456  func _() {
   457  		// this comment should be indented
   458  	for {
   459  	L1: _ = 0
   460  	L2:
   461  		_ = 0
   462  	}
   463  }
   464  
   465  
   466  func _() {
   467  	if true {
   468  		_ = 0
   469  	}
   470  	_ = 0  // the indentation here should not be affected by the long label name
   471  AnOverlongLabel:
   472  	_ = 0
   473  
   474  	if true {
   475  		_ = 0
   476  	}
   477  	_ = 0
   478  
   479  L:	_ = 0
   480  }
   481  
   482  
   483  func _() {
   484  	for {
   485  		goto L
   486  	}
   487  L:
   488  
   489  	MoreCode()
   490  }
   491  
   492  
   493  func _() {
   494  	for {
   495  		goto L
   496  	}
   497  L:	// A comment on the same line as the label, followed by a single empty line.
   498  	// Known bug: There may be more than one empty line before MoreCode()
   499  	//            (see go/printer/nodes.go, func linebreak).
   500  
   501  
   502  
   503  
   504  	MoreCode()
   505  }
   506  
   507  
   508  func _() {
   509  	for {
   510  		goto L
   511  	}
   512  L:
   513  
   514  
   515  
   516  
   517  	// There should be a single empty line before this comment.
   518  	MoreCode()
   519  }
   520  
   521  
   522  func _() {
   523  	for {
   524  		goto AVeryLongLabelThatShouldNotAffectFormatting
   525  	}
   526  AVeryLongLabelThatShouldNotAffectFormatting:
   527  	// There should be a single empty line after this comment.
   528  
   529  	// There should be a single empty line before this comment.
   530  	MoreCode()
   531  }
   532  
   533  
   534  // Formatting of empty statements.
   535  func _() {
   536  	;;;;;;;;;;;;;;;;;;;;;;;;;
   537  }
   538  
   539  func _() {;;;;;;;;;;;;;;;;;;;;;;;;;
   540  }
   541  
   542  func _() {;;;;;;;;;;;;;;;;;;;;;;;;;}
   543  
   544  func _() {
   545  f();;;;;;;;;;;;;;;;;;;;;;;;;
   546  }
   547  
   548  func _() {
   549  L:;;;;;;;;;;;;
   550  }
   551  
   552  func _() {
   553  L:;;;;;;;;;;;;
   554  	f()
   555  }
   556  

View as plain text