Repository navigation
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1850 +/- ##
==========================================
- Coverage 85.80% 85.77% -0.04%
==========================================
Files 111 111
Lines 9581 9602 +21
==========================================
+ Hits 8221 8236 +15
- Misses 1360 1366 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
Can you please rebase and fix the conflicts? Thanks |
LeadingZeros, TrailingZeros, OnesCount and Len in math/bits return at most the width of their operand, so converting their result to any integer type cannot overflow. The range analysis didn't know these calls and reported every such conversion.
ma7bor
force-pushed
the
g115-math-bits
branch
from
November 30, 2026 17:19
3bf951b to
1171130
Compare
This branch was successfully deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
G115 reports converting the result of a
math/bitsbit count to a small integer type, although these functions can't return more than the width of their operand:Len,LeadingZeros,TrailingZerosandOnesCountreturn anintbetween 0 and 8, 16, 32 or 64, and their unsuffixed forms take auint, which has at most 64 bits. So the result fits even inint8anduint8. On master all eight variants I tried (Len64,Len32,Len,TrailingZeros64,LeadingZeros32,OnesCount64,OnesCount8, andint8(bits.Len64(x))) are reported.The range analysis now gives these calls the range 0 to their width in
ComputeRange, as it does for theminandmaxbuiltins, and treats them as non-negative inisNonNegativeRecursive. The match is on the package path and the function name, so othermath/bitsfunctions are unchanged, and so is arithmetic on a bit count:uint8(bits.Len64(x) * 8)anduint8(bits.Reverse64(x))are still reported.I added both to
testutils/g115_samples.go: a sample with five conversions of bit counts that expects no issue, and one with the two conversions above that expects two. Without the change, the first sample fails with five G115 issues. With it,go test ./analyzers/...passes, andgo vetandgofmtare clean, on Go 1.27.4. I didn't run golangci-lint locally.