-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: enable staticcheck and prealloc on nogo (#35486)
ref #35345
- Loading branch information
1 parent
a2fe74f
commit d4dc6b5
Showing
43 changed files
with
855 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "prealloc", | ||
srcs = ["analyzer.go"], | ||
importpath = "github.com/pingcap/tidb/build/linter/prealloc", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//build/linter/util", | ||
"@com_github_golangci_prealloc//:prealloc", | ||
"@org_golang_x_tools//go/analysis", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// 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. | ||
|
||
package prealloc | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/golangci/prealloc" | ||
"github.com/pingcap/tidb/build/linter/util" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
// Settings is the settings for preallocation. | ||
type Settings struct { | ||
Simple bool | ||
RangeLoops bool `mapstructure:"range-loops"` | ||
ForLoops bool `mapstructure:"range-loops"` | ||
} | ||
|
||
// Name is the name of the analyzer. | ||
const Name = "prealloc" | ||
|
||
// Analyzer is the analyzer struct of prealloc. | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: Name, | ||
Doc: "Finds slice declarations that could potentially be preallocated", | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
s := &Settings{ | ||
Simple: true, | ||
RangeLoops: true, | ||
ForLoops: false, | ||
} | ||
for _, f := range pass.Files { | ||
hints := prealloc.Check([]*ast.File{f}, s.Simple, s.RangeLoops, s.ForLoops) | ||
for _, hint := range hints { | ||
pass.Reportf(hint.Pos, "[%s] Consider preallocating %s", Name, util.FormatCode(hint.DeclaredSliceName)) | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func init() { | ||
util.SkipAnalyzer(Analyzer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("//build/linter/staticcheck:def.bzl", "ANALYZERS") | ||
|
||
[ | ||
go_library( | ||
name = analyzer, | ||
srcs = [ | ||
"analyzer.go", | ||
"util.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/build/linter/staticcheck/" + analyzer, | ||
visibility = ["//visibility:public"], | ||
x_defs = {"name": analyzer}, | ||
deps = [ | ||
"//build/linter/util", | ||
"@co_honnef_go_tools//analysis/lint", | ||
"@co_honnef_go_tools//quickfix", | ||
"@co_honnef_go_tools//simple", | ||
"@co_honnef_go_tools//staticcheck", | ||
"@co_honnef_go_tools//stylecheck", | ||
"@co_honnef_go_tools//unused", | ||
"@org_golang_x_tools//go/analysis", | ||
], | ||
) | ||
for analyzer in ANALYZERS | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// 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. | ||
|
||
package staticcheck | ||
|
||
import ( | ||
"github.com/pingcap/tidb/build/linter/util" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var ( | ||
// Value to be added during stamping | ||
name = "dummy value please replace using x_defs" | ||
|
||
// Analyzer is an analyzer from staticcheck. | ||
Analyzer *analysis.Analyzer | ||
) | ||
|
||
func init() { | ||
Analyzer = FindAnalyzerByName(name) | ||
util.SkipAnalyzer(Analyzer) | ||
} |
Oops, something went wrong.