modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue21317.go (about)

     1  // run
     2  
     3  // Copyright 2017 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  // As of "Mon 6 Nov 2017", run.go doesn't yet have proper
     8  // column matching so instead match the output manually
     9  // by exec-ing
    10  
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"io/ioutil"
    16  	"log"
    17  	"os"
    18  	"os/exec"
    19  	"runtime"
    20  	"strings"
    21  )
    22  
    23  func main() {
    24  	if runtime.Compiler != "gc" || runtime.GOOS == "nacl" {
    25  		return
    26  	}
    27  
    28  	f, err := ioutil.TempFile("", "issue21317.go")
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	fmt.Fprintf(f, `
    33  package main
    34  
    35  import "fmt"
    36  
    37  func main() {
    38          n, err := fmt.Println(1)
    39  }
    40  `)
    41  	f.Close()
    42  	defer os.RemoveAll(f.Name())
    43  
    44  	// compile and test output
    45  	cmd := exec.Command("go", "tool", "compile", f.Name())
    46  	out, err := cmd.CombinedOutput()
    47  	if err == nil {
    48  		log.Fatalf("expected cmd/compile to fail")
    49  	}
    50  	wantErrs := []string{
    51  		"7:9: n declared and not used",
    52  		"7:12: err declared and not used",
    53  	}
    54  	outStr := string(out)
    55  	for _, want := range wantErrs {
    56  		if !strings.Contains(outStr, want) {
    57  			log.Fatalf("failed to match %q\noutput: %q", want, outStr)
    58  		}
    59  	}
    60  }