]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: allow large shifts of constants
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 19 Jul 2016 00:05:54 +0000 (17:05 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 18 Aug 2016 23:39:37 +0000 (23:39 +0000)
Large shifts of constants are frequently
used for fancy 32/64 bit detection.

This removes 14 false positives from the
standard library.

Updates #11041

Change-Id: Ib39346e5c161da04c38a6a3067932ef43bf74f2d
Reviewed-on: https://go-review.googlesource.com/27155
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/vet/shift.go
src/cmd/vet/testdata/shift.go

index 8c038b4bdd792b916fbaf71e374cd1d23508a30d..55f3ea369f740ae418bd2f901e2b6092eae212b2 100644 (file)
@@ -41,6 +41,12 @@ func checkShift(f *File, node ast.Node) {
 // checkLongShift checks if shift or shift-assign operations shift by more than
 // the length of the underlying variable.
 func checkLongShift(f *File, node ast.Node, x, y ast.Expr) {
+       if f.pkg.types[x].Value != nil {
+               // Ignore shifts of constants.
+               // These are frequently used for bit-twiddling tricks
+               // like ^uint(0) >> 63 for 32/64 bit detection and compatibility.
+               return
+       }
        v := f.pkg.types[y].Value
        if v == nil {
                return
index 6624f09cc1617bd19a4991104f945a28428574bc..99acaadf6d0f014b0c67b1144229454045b21610 100644 (file)
@@ -75,4 +75,6 @@ func ShiftTest() {
        _ = p >> 32 // ERROR "p might be too small for shift of 32"
        p <<= 32    // ERROR "p might be too small for shift of 32"
        p >>= 32    // ERROR "p might be too small for shift of 32"
+
+       const oneIf64Bit = ^uint(0) >> 63 // allow large shifts of constants; they are used for 32/64 bit compatibility tricks
 }