| 1 |
package sa4003 |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
"go/constant" |
| 7 |
"go/token" |
| 8 |
"go/types" |
| 9 |
"math" |
| 10 |
|
| 11 |
"honnef.co/go/tools/analysis/code" |
| 12 |
"honnef.co/go/tools/analysis/facts/generated" |
| 13 |
"honnef.co/go/tools/analysis/lint" |
| 14 |
"honnef.co/go/tools/analysis/report" |
| 15 |
"honnef.co/go/tools/go/types/typeutil" |
| 16 |
|
| 17 |
"golang.org/x/tools/go/analysis" |
| 18 |
"golang.org/x/tools/go/analysis/passes/inspect" |
| 19 |
) |
| 20 |
|
| 21 |
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ |
| 22 |
Analyzer: &analysis.Analyzer{ |
| 23 |
Name: "SA4003", |
| 24 |
Run: run, |
| 25 |
Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer}, |
| 26 |
}, |
| 27 |
Doc: &lint.RawDocumentation{ |
| 28 |
Title: `Comparing unsigned values against negative values is pointless`, |
| 29 |
Since: "2017.1", |
| 30 |
Severity: lint.SeverityWarning, |
| 31 |
MergeIf: lint.MergeIfAll, |
| 32 |
}, |
| 33 |
}) |
| 34 |
|
| 35 |
var Analyzer = SCAnalyzer.Analyzer |
| 36 |
|
| 37 |
func run(pass *analysis.Pass) (any, error) { |
| 38 |
isobj := func(expr ast.Expr, name string) bool { |
| 39 |
if name == "" { |
| 40 |
return false |
| 41 |
} |
| 42 |
sel, ok := expr.(*ast.SelectorExpr) |
| 43 |
if !ok { |
| 44 |
return false |
| 45 |
} |
| 46 |
return typeutil.IsObject(pass.TypesInfo.ObjectOf(sel.Sel), name) |
| 47 |
} |
| 48 |
|
| 49 |
fn := func(node ast.Node) { |
| 50 |
expr := node.(*ast.BinaryExpr) |
| 51 |
tx := pass.TypesInfo.TypeOf(expr.X) |
| 52 |
basic, ok := tx.Underlying().(*types.Basic) |
| 53 |
if !ok { |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
// We only check for the math constants and integer literals, not for all constant expressions. This is to avoid |
| 58 |
// false positives when constant values differ under different build tags. |
| 59 |
var ( |
| 60 |
maxMathConst string |
| 61 |
minMathConst string |
| 62 |
maxLiteral constant.Value |
| 63 |
minLiteral constant.Value |
| 64 |
) |
| 65 |
|
| 66 |
switch basic.Kind() { |
| 67 |
case types.Uint8: |
| 68 |
maxMathConst = "math.MaxUint8" |
| 69 |
minLiteral = constant.MakeUint64(0) |
| 70 |
maxLiteral = constant.MakeUint64(math.MaxUint8) |
| 71 |
case types.Uint16: |
| 72 |
maxMathConst = "math.MaxUint16" |
| 73 |
minLiteral = constant.MakeUint64(0) |
| 74 |
maxLiteral = constant.MakeUint64(math.MaxUint16) |
| 75 |
case types.Uint32: |
| 76 |
maxMathConst = "math.MaxUint32" |
| 77 |
minLiteral = constant.MakeUint64(0) |
| 78 |
maxLiteral = constant.MakeUint64(math.MaxUint32) |
| 79 |
case types.Uint64: |
| 80 |
maxMathConst = "math.MaxUint64" |
| 81 |
minLiteral = constant.MakeUint64(0) |
| 82 |
maxLiteral = constant.MakeUint64(math.MaxUint64) |
| 83 |
case types.Uint: |
| 84 |
// TODO(dh): we could chose 32 bit vs 64 bit depending on the file's build tags |
| 85 |
maxMathConst = "math.MaxUint64" |
| 86 |
minLiteral = constant.MakeUint64(0) |
| 87 |
maxLiteral = constant.MakeUint64(math.MaxUint64) |
| 88 |
|
| 89 |
case types.Int8: |
| 90 |
minMathConst = "math.MinInt8" |
| 91 |
maxMathConst = "math.MaxInt8" |
| 92 |
minLiteral = constant.MakeInt64(math.MinInt8) |
| 93 |
maxLiteral = constant.MakeInt64(math.MaxInt8) |
| 94 |
case types.Int16: |
| 95 |
minMathConst = "math.MinInt16" |
| 96 |
maxMathConst = "math.MaxInt16" |
| 97 |
minLiteral = constant.MakeInt64(math.MinInt16) |
| 98 |
maxLiteral = constant.MakeInt64(math.MaxInt16) |
| 99 |
case types.Int32: |
| 100 |
minMathConst = "math.MinInt32" |
| 101 |
maxMathConst = "math.MaxInt32" |
| 102 |
minLiteral = constant.MakeInt64(math.MinInt32) |
| 103 |
maxLiteral = constant.MakeInt64(math.MaxInt32) |
| 104 |
case types.Int64: |
| 105 |
minMathConst = "math.MinInt64" |
| 106 |
maxMathConst = "math.MaxInt64" |
| 107 |
minLiteral = constant.MakeInt64(math.MinInt64) |
| 108 |
maxLiteral = constant.MakeInt64(math.MaxInt64) |
| 109 |
case types.Int: |
| 110 |
// TODO(dh): we could chose 32 bit vs 64 bit depending on the file's build tags |
| 111 |
minMathConst = "math.MinInt64" |
| 112 |
maxMathConst = "math.MaxInt64" |
| 113 |
minLiteral = constant.MakeInt64(math.MinInt64) |
| 114 |
maxLiteral = constant.MakeInt64(math.MaxInt64) |
| 115 |
} |
| 116 |
|
| 117 |
isLiteral := func(expr ast.Expr, c constant.Value) bool { |
| 118 |
if c == nil { |
| 119 |
return false |
| 120 |
} |
| 121 |
return code.IsIntegerLiteral(pass, expr, c) |
| 122 |
} |
| 123 |
isZeroLiteral := func(expr ast.Expr) bool { |
| 124 |
return code.IsIntegerLiteral(pass, expr, constant.MakeInt64(0)) |
| 125 |
} |
| 126 |
|
| 127 |
if (expr.Op == token.GTR || expr.Op == token.GEQ) && (isobj(expr.Y, maxMathConst) || isLiteral(expr.Y, maxLiteral)) || |
| 128 |
(expr.Op == token.LSS || expr.Op == token.LEQ) && (isobj(expr.X, maxMathConst) || isLiteral(expr.X, maxLiteral)) { |
| 129 |
report.Report(pass, expr, fmt.Sprintf("no value of type %s is greater than %s", basic, maxMathConst), report.FilterGenerated()) |
| 130 |
} |
| 131 |
|
| 132 |
if expr.Op == token.LEQ && (isobj(expr.Y, maxMathConst) || isLiteral(expr.Y, maxLiteral)) || |
| 133 |
expr.Op == token.GEQ && (isobj(expr.X, maxMathConst) || isLiteral(expr.X, maxLiteral)) { |
| 134 |
report.Report(pass, expr, fmt.Sprintf("every value of type %s is <= %s", basic, maxMathConst), report.FilterGenerated()) |
| 135 |
} |
| 136 |
|
| 137 |
if (basic.Info() & types.IsUnsigned) != 0 { |
| 138 |
if (expr.Op == token.LSS && isZeroLiteral(expr.Y)) || |
| 139 |
(expr.Op == token.GTR && isZeroLiteral(expr.X)) { |
| 140 |
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than 0", basic), report.FilterGenerated()) |
| 141 |
} |
| 142 |
if expr.Op == token.GEQ && isZeroLiteral(expr.Y) || |
| 143 |
expr.Op == token.LEQ && isZeroLiteral(expr.X) { |
| 144 |
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= 0", basic), report.FilterGenerated()) |
| 145 |
} |
| 146 |
} else { |
| 147 |
if (expr.Op == token.LSS || expr.Op == token.LEQ) && (isobj(expr.Y, minMathConst) || isLiteral(expr.Y, minLiteral)) || |
| 148 |
(expr.Op == token.GTR || expr.Op == token.GEQ) && (isobj(expr.X, minMathConst) || isLiteral(expr.X, minLiteral)) { |
| 149 |
report.Report(pass, expr, fmt.Sprintf("no value of type %s is less than %s", basic, minMathConst), report.FilterGenerated()) |
| 150 |
} |
| 151 |
if expr.Op == token.GEQ && (isobj(expr.Y, minMathConst) || isLiteral(expr.Y, minLiteral)) || |
| 152 |
expr.Op == token.LEQ && (isobj(expr.X, minMathConst) || isLiteral(expr.X, minLiteral)) { |
| 153 |
report.Report(pass, expr, fmt.Sprintf("every value of type %s is >= %s", basic, minMathConst), report.FilterGenerated()) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
code.Preorder(pass, fn, (*ast.BinaryExpr)(nil)) |
| 159 |
return nil, nil |
| 160 |
} |