Source file test/cannotassign.go

     1  // errorcheck
     2  
     3  // Copyright 2020 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test "cannot assign" errors
     8  
     9  package main
    10  
    11  func main() {
    12  	var s string = "hello"
    13  	s[1:2] = "a" // ERROR "cannot assign to .* (\(strings are immutable\))?"
    14  	s[3] = "b"   // ERROR "cannot assign to .* (\(strings are immutable\))?"
    15  
    16  	const n int = 1
    17  	const cs string = "hello"
    18  	n = 2        // ERROR "cannot assign to .* (\(declared const\))?"
    19  	cs = "hi"    // ERROR "cannot assign to .* (\(declared const\))?"
    20  	true = false // ERROR "cannot assign to .* (\(declared const\))?"
    21  
    22  	var m map[int]struct{ n int }
    23  	m[0].n = 7 // ERROR "cannot assign to struct field .* in map$"
    24  
    25  	1 = 7         // ERROR "cannot assign to 1"
    26  	"hi" = 7      // ERROR `cannot assign to "hi"`
    27  	nil = 7       // ERROR "cannot assign to nil"
    28  	len("") = 7   // ERROR `cannot assign to len\(""\)`
    29  	[]int{} = nil // ERROR "cannot assign to \[\]int\{\}"
    30  
    31  	var x int = 7
    32  	x + 1 = 7 // ERROR "cannot assign to x \+ 1"
    33  }
    34  

View as plain text