-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the maincheck linter that ensures all the main functions are decl…
…ared in a file that is called main.go GitOrigin-RevId: 6d554550b9b9edd19bda3c7ab9a5d16e28a8d017
Showing
14 changed files
with
98 additions
and
12 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
File renamed without changes.
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,9 @@ | ||
load("//lint:go.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["maincheck.go"], | ||
importpath = "github.com/scionproto/scion/tools/lint/maincheck", | ||
visibility = ["//visibility:public"], | ||
deps = ["@org_golang_x_tools//go/analysis:go_default_library"], | ||
) |
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,72 @@ | ||
// Copyright 2022 Anapaya Systems | ||
// | ||
// 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 maincheck | ||
|
||
import ( | ||
"go/ast" | ||
"path/filepath" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var permitted = []string{ | ||
"main.go", | ||
"testmain.go", // bazel uses this for unit and integration tests. | ||
"go_default_mock_gomock_prog.go", // bazel uses this for mock generation. | ||
} | ||
|
||
// Analyzer contains an analyzer that makes sure that go target is always a | ||
// func literal that calls defer log.HandlePanic as first statement. | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: "maincheck", | ||
Doc: "Check that main function is located in a main.go file", | ||
Run: run, | ||
RunDespiteErrors: true, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
for _, file := range pass.Files { | ||
// If this is not a main package, we can exit early. | ||
if file.Name.Name != "main" { | ||
return nil, nil | ||
} | ||
|
||
for _, dec := range file.Decls { | ||
fun, ok := dec.(*ast.FuncDecl) | ||
if !ok { | ||
continue | ||
} | ||
if fun.Name.Name != "main" { | ||
continue | ||
} | ||
base := filepath.Base(pass.Fset.File(file.Name.NamePos).Name()) | ||
found := func() bool { | ||
for _, p := range permitted { | ||
if base == p { | ||
return true | ||
} | ||
} | ||
return false | ||
}() | ||
|
||
if !found { | ||
pass.Reportf(fun.Pos(), | ||
"main function must be located in a file called main.go instead of %s", base, | ||
) | ||
} | ||
} | ||
} | ||
return nil, nil | ||
} |
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.