Skip to content

Commit

Permalink
fix VerbosityZero check panic
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjunmyfm192085 committed Sep 9, 2022
1 parent f68289b commit dae0ebb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions logcheck/pkg/logcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,14 @@ func isVerbosityZero(expr ast.Expr) bool {
return false
}

if lit, ok := subCallExpr.Args[0].(*ast.BasicLit); ok && lit.Value == "0" {
return true
if lit, ok := subCallExpr.Args[0].(*ast.BasicLit); ok {
if lit.Value == "0" {
return true
}
return false
}

if id, ok := subCallExpr.Args[0].(*ast.Ident); ok && id.Obj.Kind == 2 {
if id, ok := subCallExpr.Args[0].(*ast.Ident); ok && id.Obj != nil && id.Obj.Kind == 2 {
v, ok := id.Obj.Decl.(*ast.ValueSpec)
if !ok || len(v.Values) != 1 {
return false
Expand Down
26 changes: 26 additions & 0 deletions logcheck/testdata/src/doNotAllowVerbosityZeroLogs/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This fake package is created as golang.org/x/tools/go/analysis/analysistest
// expects it to be here for loading. This package is used to test allow-unstructured
// flag which suppresses errors when unstructured logging is used.
// This is a test file for unstructured logging static check tool unit tests.

package Verbosity

const (
LogLevel = 4
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func verbosityLogging() {
klog.V(1).Info("test log")
klog.V(1).Infof("test log")
klog.V(1).Infoln("test log")
klog.V(LogLevel).InfoS("I'm logging at level 4.")
klog.V(1).InfoS("I'm logging at level 1.")
klog.V(oneConst).InfoS("I'm logging at level 1.")
klog.V(oneVar).InfoS("I'm logging at level 1.")
Expand Down

0 comments on commit dae0ebb

Please sign in to comment.