Skip to content

Commit

Permalink
staticcheck: handle imports of deprecated github.com/golang/protobuf/…
Browse files Browse the repository at this point in the history
…proto specially

github.com/golang/protobuf has deprecated the proto package, but their
protoc-gen-go still imports the package and uses one of its constants,
"to enforce a weak dependency on a sufficiently new version of the
legacy package".

Staticcheck would flag the import of this deprecated package in all
code generated by protoc-gen-go. Instead of forcing the project to
change their project structure, we choose to ignore such imports in
code generated by protoc-gen-go. The import still gets flagged in code
not generated by protoc-gen-go.

Upstream issue: golang/protobuf#1077
  • Loading branch information
dominikh committed May 15, 2020
1 parent 508b5eb commit f4c568f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions facts/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
Goyacc
Cgo
Stringer
ProtocGenGo
)

var (
Expand Down Expand Up @@ -51,6 +52,8 @@ func isGenerated(path string) (Generator, bool) {
return Goyacc, true
case "by cmd/cgo;":
return Cgo, true
case "by protoc-gen-go.":
return ProtocGenGo, true
}
if strings.HasPrefix(text, `by "stringer `) {
return Stringer, true
Expand Down
2 changes: 1 addition & 1 deletion staticcheck/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var Analyzers = lintutil.InitializeAnalyzers(Docs, map[string]*analysis.Analyzer
"SA1018": makeCallCheckerAnalyzer(checkStringsReplaceZeroRules),
"SA1019": {
Run: CheckDeprecated,
Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Deprecated},
Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Deprecated, facts.Generated},
},
"SA1020": makeCallCheckerAnalyzer(checkListenAddressRules),
"SA1021": makeCallCheckerAnalyzer(checkBytesEqualIPRules),
Expand Down
6 changes: 6 additions & 0 deletions staticcheck/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,12 @@ func CheckDeprecated(pass *analysis.Pass) (interface{}, error) {
p := spec.Path.Value
path := p[1 : len(p)-1]
if depr, ok := deprs.Packages[imp]; ok {
if path == "github.com/golang/protobuf/proto" {
gen, ok := code.Generator(pass, spec.Path.Pos())
if ok && gen == facts.ProtocGenGo {
return
}
}
report.Report(pass, spec, fmt.Sprintf("package %s is deprecated: %s", path, depr.Msg))
}
}
Expand Down
3 changes: 3 additions & 0 deletions staticcheck/testdata/src/CheckDeprecated/not-protobuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pkg

import _ "github.com/golang/protobuf/proto" // want `Alas, it is deprecated\.`
5 changes: 5 additions & 0 deletions staticcheck/testdata/src/CheckDeprecated/protobuf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package proto exists.
//
// Deprecated: Alas, it is deprecated.
package proto

0 comments on commit f4c568f

Please sign in to comment.