Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix VerbosityZero check panic #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions logcheck/pkg/logcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,15 @@ 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 {
// When Constants of value is defined in different files, the id.Obj will be nil, we should filter this condition.
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