github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/src/cmd/vet/testdata/bool.go (about)

     1  // Copyright 2014 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  // This file contains tests for the bool checker.
     6  
     7  package testdata
     8  
     9  import "io"
    10  
    11  func RatherStupidConditions() {
    12  	var f, g func() int
    13  	if f() == 0 || f() == 0 { // OK f might have side effects
    14  	}
    15  	if v, w := f(), g(); v == w || v == w { // ERROR "redundant or: v == w || v == w"
    16  	}
    17  	_ = f == nil || f == nil // ERROR "redundant or: f == nil || f == nil"
    18  
    19  	_ = i == byte(1) || i == byte(1) // TODO conversions are treated as if they may have side effects
    20  
    21  	var c chan int
    22  	_ = 0 == <-c || 0 == <-c                                  // OK subsequent receives may yield different values
    23  	for i, j := <-c, <-c; i == j || i == j; i, j = <-c, <-c { // ERROR "redundant or: i == j || i == j"
    24  	}
    25  
    26  	var i, j, k int
    27  	_ = i+1 == 1 || i+1 == 1         // ERROR "redundant or: i\+1 == 1 || i\+1 == 1"
    28  	_ = i == 1 || j+1 == i || i == 1 // ERROR "redundant or: i == 1 || i == 1"
    29  
    30  	_ = i == 1 || i == 1 || f() == 1 // ERROR "redundant or: i == 1 || i == 1"
    31  	_ = i == 1 || f() == 1 || i == 1 // OK f may alter i as a side effect
    32  	_ = f() == 1 || i == 1 || i == 1 // ERROR "redundant or: i == 1 || i == 1"
    33  
    34  	// Test partition edge cases
    35  	_ = f() == 1 || i == 1 || i == 1 || j == 1 // ERROR "redundant or: i == 1 || i == 1"
    36  	_ = f() == 1 || j == 1 || i == 1 || i == 1 // ERROR "redundant or: i == 1 || i == 1"
    37  	_ = i == 1 || f() == 1 || i == 1 || i == 1 // ERROR "redundant or: i == 1 || i == 1"
    38  	_ = i == 1 || i == 1 || f() == 1 || i == 1 // ERROR "redundant or: i == 1 || i == 1"
    39  	_ = i == 1 || i == 1 || j == 1 || f() == 1 // ERROR "redundant or: i == 1 || i == 1"
    40  	_ = j == 1 || i == 1 || i == 1 || f() == 1 // ERROR "redundant or: i == 1 || i == 1"
    41  	_ = i == 1 || f() == 1 || f() == 1 || i == 1
    42  
    43  	_ = i == 1 || (i == 1 || i == 2)             // ERROR "redundant or: i == 1 || i == 1"
    44  	_ = i == 1 || (f() == 1 || i == 1)           // OK f may alter i as a side effect
    45  	_ = i == 1 || (i == 1 || f() == 1)           // ERROR "redundant or: i == 1 || i == 1"
    46  	_ = i == 1 || (i == 2 || (i == 1 || i == 3)) // ERROR "redundant or: i == 1 || i == 1"
    47  
    48  	var a, b bool
    49  	_ = i == 1 || (a || (i == 1 || b)) // ERROR "redundant or: i == 1 || i == 1"
    50  
    51  	// Check that all redundant ors are flagged
    52  	_ = j == 0 ||
    53  		i == 1 ||
    54  		f() == 1 ||
    55  		j == 0 || // ERROR "redundant or: j == 0 || j == 0"
    56  		i == 1 || // ERROR "redundant or: i == 1 || i == 1"
    57  		i == 1 || // ERROR "redundant or: i == 1 || i == 1"
    58  		i == 1 ||
    59  		j == 0 ||
    60  		k == 0
    61  
    62  	_ = i == 1*2*3 || i == 1*2*3 // ERROR "redundant or: i == 1\*2\*3 || i == 1\*2\*3"
    63  
    64  	// These test that redundant, suspect expressions do not trigger multiple errors.
    65  	_ = i != 0 || i != 0 // ERROR "redundant or: i != 0 || i != 0"
    66  	_ = i == 0 && i == 0 // ERROR "redundant and: i == 0 && i == 0"
    67  
    68  	// and is dual to or; check the basics and
    69  	// let the or tests pull the rest of the weight.
    70  	_ = 0 != <-c && 0 != <-c         // OK subsequent receives may yield different values
    71  	_ = f() != 0 && f() != 0         // OK f might have side effects
    72  	_ = f != nil && f != nil         // ERROR "redundant and: f != nil && f != nil"
    73  	_ = i != 1 && i != 1 && f() != 1 // ERROR "redundant and: i != 1 && i != 1"
    74  	_ = i != 1 && f() != 1 && i != 1 // OK f may alter i as a side effect
    75  	_ = f() != 1 && i != 1 && i != 1 // ERROR "redundant and: i != 1 && i != 1"
    76  }
    77  
    78  func RoyallySuspectConditions() {
    79  	var i, j int
    80  
    81  	_ = i == 0 || i == 1 // OK
    82  	_ = i != 0 || i != 1 // ERROR "suspect or: i != 0 || i != 1"
    83  	_ = i != 0 || 1 != i // ERROR "suspect or: i != 0 || 1 != i"
    84  	_ = 0 != i || 1 != i // ERROR "suspect or: 0 != i || 1 != i"
    85  	_ = 0 != i || i != 1 // ERROR "suspect or: 0 != i || i != 1"
    86  
    87  	_ = (0 != i) || i != 1 // ERROR "suspect or: 0 != i || i != 1"
    88  
    89  	_ = i+3 != 7 || j+5 == 0 || i+3 != 9 // ERROR "suspect or: i\+3 != 7 || i\+3 != 9"
    90  
    91  	_ = i != 0 || j == 0 || i != 1 // ERROR "suspect or: i != 0 || i != 1"
    92  
    93  	_ = i != 0 || i != 1<<4 // ERROR "suspect or: i != 0 || i != 1<<4"
    94  
    95  	_ = i != 0 || j != 0
    96  	_ = 0 != i || 0 != j
    97  
    98  	var s string
    99  	_ = s != "one" || s != "the other" // ERROR "suspect or: s != .one. || s != .the other."
   100  
   101  	_ = "et" != "alii" || "et" != "cetera"         // ERROR "suspect or: .et. != .alii. || .et. != .cetera."
   102  	_ = "me gustas" != "tu" || "le gustas" != "tu" // OK we could catch this case, but it's not worth the code
   103  
   104  	var err error
   105  	_ = err != nil || err != io.EOF // TODO catch this case?
   106  
   107  	// Sanity check and.
   108  	_ = i != 0 && i != 1 // OK
   109  	_ = i == 0 && i == 1 // ERROR "suspect and: i == 0 && i == 1"
   110  	_ = i == 0 && 1 == i // ERROR "suspect and: i == 0 && 1 == i"
   111  	_ = 0 == i && 1 == i // ERROR "suspect and: 0 == i && 1 == i"
   112  	_ = 0 == i && i == 1 // ERROR "suspect and: 0 == i && i == 1"
   113  }