Skip to content
Permalink
Browse files

cmd/vet: add check for -0.0

DO NOT REVIEW

This is a quick hack for golang#19732.
It needs more thought, tests, and docs.

Updates golang#19675

Change-Id: Id1a323ba7ec001b2f1a88f30497defc6b823d409
  • Loading branch information...
josharian committed Mar 28, 2017
1 parent 95bfd92 commit f1849c6e350611941c48536768abfb707b7255c1
Showing with 35 additions and 0 deletions.
  1. +3 −0 src/cmd/vet/main.go
  2. +32 −0 src/cmd/vet/zero.go
@@ -141,6 +141,7 @@ var (
rangeStmt *ast.RangeStmt
returnStmt *ast.ReturnStmt
structType *ast.StructType
unaryExpr *ast.UnaryExpr

// checkers is a two-level map.
// The outer level is keyed by a nil pointer, one of the AST vars above.
@@ -496,6 +497,8 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
key = returnStmt
case *ast.StructType:
key = structType
case *ast.UnaryExpr:
key = unaryExpr
}
for _, fn := range f.checkers[key] {
fn(f, node)
@@ -0,0 +1,32 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file contains a check for -0.0.

package main

import (
"go/ast"
"go/constant"
"go/token"
)

func init() {
register("minuszero",
"check for -0.0",
checkMinusZero,
unaryExpr)
}

func checkMinusZero(f *File, n ast.Node) {
u := n.(*ast.UnaryExpr)
if u.Op != token.SUB {
return
}
v := f.pkg.types[u.X].Value
if v == nil || v.Kind() != constant.Float || v.String() != "0" {
return
}
f.Badf(u.Pos(), "minus zero: %s", f.gofmt(u))
}

0 comments on commit f1849c6

Please sign in to comment.
You can’t perform that action at this time.