github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/lint/passes/fmtsafe/testdata/src/a/a.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package a
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util/log"
    17  	"github.com/cockroachdb/errors"
    18  	"go.etcd.io/etcd/raft"
    19  )
    20  
    21  var unsafeStr = "abc %d"
    22  
    23  const constOk = "safe %d"
    24  
    25  func init() {
    26  	_ = recover()
    27  
    28  	_ = errors.New(unsafeStr) // want `message argument is not a constant expression`
    29  
    30  	// Even though the following is trying to opt out of the linter,
    31  	// the opt out fails because the code is not in a test.
    32  
    33  	_ = errors.New(unsafeStr /*nolint:fmtsafe*/) // want `message argument is not a constant expression`
    34  
    35  	_ = errors.New("safestr")
    36  	_ = errors.New(constOk)
    37  	_ = errors.New("abo" + constOk)
    38  	_ = errors.New("abo" + unsafeStr) // want `message argument is not a constant expression`
    39  
    40  	_ = errors.Newf("safe %d", 123)
    41  	_ = errors.Newf(constOk, 123)
    42  	_ = errors.Newf(unsafeStr, 123) // want `format argument is not a constant expression`
    43  	_ = errors.Newf("abo"+constOk, 123)
    44  	_ = errors.Newf("abo"+unsafeStr, 123) // want `format argument is not a constant expression`
    45  
    46  	ctx := context.Background()
    47  
    48  	log.Errorf(ctx, "safe %d", 123)
    49  	log.Errorf(ctx, constOk, 123)
    50  	log.Errorf(ctx, unsafeStr, 123) // want `format argument is not a constant expression`
    51  	log.Errorf(ctx, "abo"+constOk, 123)
    52  	log.Errorf(ctx, "abo"+unsafeStr, 123) // want `format argument is not a constant expression`
    53  
    54  	var m myLogger
    55  	var l raft.Logger = m
    56  
    57  	l.Infof("safe %d", 123)
    58  	l.Infof(constOk, 123)
    59  	l.Infof(unsafeStr, 123) // want `format argument is not a constant expression`
    60  	l.Infof("abo"+constOk, 123)
    61  	l.Infof("abo"+unsafeStr, 123) // want `format argument is not a constant expression`
    62  }
    63  
    64  type myLogger struct{}
    65  
    66  func (m myLogger) Info(args ...interface{}) {
    67  	log.Errorf(context.Background(), "", args...)
    68  }
    69  
    70  func (m myLogger) Infof(_ string, args ...interface{}) {
    71  	log.Errorf(context.Background(), "ignoredfmt", args...)
    72  }