Skip to content

Commit 3ec579c

Browse files
checkBoundInRange: one function for int and float
1 parent f81fef4 commit 3ec579c

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

pkg/analysis/numericbounds/analyzer.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,23 @@ func checkBoundsWithinTypeRange(pass *analysis.Pass, field *ast.Field, prefix, t
189189
checkBoundInRange(pass, field, prefix, maximum, int64(minSafeInt64), int64(maxSafeInt64), "maximum", "JavaScript-safe int64",
190190
"Consider using a string type to avoid precision loss in JavaScript clients")
191191
case "float32":
192-
checkFloatBoundInRange(pass, field, prefix, minimum, minFloat32, maxFloat32, "minimum", "float32")
193-
checkFloatBoundInRange(pass, field, prefix, maximum, minFloat32, maxFloat32, "maximum", "float32")
192+
checkBoundInRange(pass, field, prefix, minimum, minFloat32, maxFloat32, "minimum", "float32")
193+
checkBoundInRange(pass, field, prefix, maximum, minFloat32, maxFloat32, "maximum", "float32")
194194
case "float64":
195-
checkFloatBoundInRange(pass, field, prefix, minimum, minFloat64, maxFloat64, "minimum", "float64")
196-
checkFloatBoundInRange(pass, field, prefix, maximum, minFloat64, maxFloat64, "maximum", "float64")
195+
checkBoundInRange(pass, field, prefix, minimum, minFloat64, maxFloat64, "minimum", "float64")
196+
checkBoundInRange(pass, field, prefix, maximum, minFloat64, maxFloat64, "maximum", "float64")
197197
}
198198
}
199199

200-
// checkBoundInRange checks if an integer bound value is within the valid range.
201-
// Uses generics to avoid type conversions.
202-
func checkBoundInRange[T constraints.Integer](pass *analysis.Pass, field *ast.Field, prefix string, value float64, minBound, maxBound T, boundType, typeName string, extraMsg ...string) {
200+
// checkBoundInRange checks if a bound value is within the valid range.
201+
// Uses generics to work with both integer and float types.
202+
func checkBoundInRange[T constraints.Integer | constraints.Float](pass *analysis.Pass, field *ast.Field, prefix string, value float64, minBound, maxBound T, boundType, typeName string, extraMsg ...string) {
203203
if value < float64(minBound) || value > float64(maxBound) {
204-
msg := fmt.Sprintf("%s has %s bound %%v that is outside the %s range [%%d, %%d]", prefix, boundType, typeName)
204+
msg := fmt.Sprintf("%s has %s bound %%v that is outside the %s range [%%v, %%v]", prefix, boundType, typeName)
205205
if len(extraMsg) > 0 {
206206
msg += ". " + extraMsg[0]
207207
}
208208

209209
pass.Reportf(field.Pos(), msg, value, minBound, maxBound)
210210
}
211211
}
212-
213-
// checkFloatBoundInRange checks if a float bound value is within the valid range.
214-
func checkFloatBoundInRange[T constraints.Float](pass *analysis.Pass, field *ast.Field, prefix string, value float64, minBound, maxBound T, boundType, typeName string) {
215-
if value < float64(minBound) || value > float64(maxBound) {
216-
pass.Reportf(field.Pos(), "%s has %s bound %v that is outside the valid %s range", prefix, boundType, value, typeName)
217-
}
218-
}

0 commit comments

Comments
 (0)