Skip to content

Commit 8273271

Browse files
committed
SA4003: skip generated files
Code generators shouldn't have to avoid emitting comparisons that are sometimes superfluous. Recently, this affected stringer[1][2] [1]: golang/go#41436 (comment) [2]: https://go-review.googlesource.com/c/tools/+/712482
1 parent e1d6482 commit 8273271

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

staticcheck/sa4003/sa4003.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math"
1010

1111
"honnef.co/go/tools/analysis/code"
12+
"honnef.co/go/tools/analysis/facts/generated"
1213
"honnef.co/go/tools/analysis/lint"
1314
"honnef.co/go/tools/analysis/report"
1415
"honnef.co/go/tools/go/types/typeutil"
@@ -21,7 +22,7 @@ var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
2122
Analyzer: &analysis.Analyzer{
2223
Name: "SA4003",
2324
Run: run,
24-
Requires: []*analysis.Analyzer{inspect.Analyzer},
25+
Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer},
2526
},
2627
Doc: &lint.RawDocumentation{
2728
Title: `Comparing unsigned values against negative values is pointless`,
@@ -125,31 +126,31 @@ func run(pass *analysis.Pass) (any, error) {
125126

126127
if (expr.Op == token.GTR || expr.Op == token.GEQ) && (isobj(expr.Y, maxMathConst) || isLiteral(expr.Y, maxLiteral)) ||
127128
(expr.Op == token.LSS || expr.Op == token.LEQ) && (isobj(expr.X, maxMathConst) || isLiteral(expr.X, maxLiteral)) {
128-
report.Report(pass, expr, fmt.Sprintf("no value of type %s is greater than %s", basic, maxMathConst))
129+
report.Report(pass, expr, fmt.Sprintf("no value of type %s is greater than %s", basic, maxMathConst), report.FilterGenerated())
129130
}
130131

131132
if expr.Op == token.LEQ && (isobj(expr.Y, maxMathConst) || isLiteral(expr.Y, maxLiteral)) ||
132133
expr.Op == token.GEQ && (isobj(expr.X, maxMathConst) || isLiteral(expr.X, maxLiteral)) {
133-
report.Report(pass, expr, fmt.Sprintf("every value of type %s is <= %s", basic, maxMathConst))
134+
report.Report(pass, expr, fmt.Sprintf("every value of type %s is <= %s", basic, maxMathConst), report.FilterGenerated())
134135
}
135136

136137
if (basic.Info() & types.IsUnsigned) != 0 {
137138
if (expr.Op == token.LSS && isZeroLiteral(expr.Y)) ||
138139
(expr.Op == token.GTR && isZeroLiteral(expr.X)) {
139-
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than 0", basic))
140+
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than 0", basic), report.FilterGenerated())
140141
}
141142
if expr.Op == token.GEQ && isZeroLiteral(expr.Y) ||
142143
expr.Op == token.LEQ && isZeroLiteral(expr.X) {
143-
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= 0", basic))
144+
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= 0", basic), report.FilterGenerated())
144145
}
145146
} else {
146147
if (expr.Op == token.LSS || expr.Op == token.LEQ) && (isobj(expr.Y, minMathConst) || isLiteral(expr.Y, minLiteral)) ||
147148
(expr.Op == token.GTR || expr.Op == token.GEQ) && (isobj(expr.X, minMathConst) || isLiteral(expr.X, minLiteral)) {
148-
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than %s", basic, minMathConst))
149+
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than %s", basic, minMathConst), report.FilterGenerated())
149150
}
150151
if expr.Op == token.GEQ && (isobj(expr.Y, minMathConst) || isLiteral(expr.Y, minLiteral)) ||
151152
expr.Op == token.LEQ && (isobj(expr.X, minMathConst) || isLiteral(expr.X, minLiteral)) {
152-
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= %s", basic, minMathConst))
153+
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= %s", basic, minMathConst), report.FilterGenerated())
153154
}
154155
}
155156

0 commit comments

Comments
 (0)