Skip to content

Commit

Permalink
checkers: refactor and fix hexLiteral checker (go-critic#789)
Browse files Browse the repository at this point in the history
Also renamed it from hexLiterals to hexLiteral.

Fixes go-critic#780

Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte authored Feb 2, 2019
1 parent 5dafc45 commit ed5e8e7
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 116 deletions.
60 changes: 60 additions & 0 deletions checkers/hexLiteral_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package checkers

import (
"go/ast"
"go/token"
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcast"
)

func init() {
var info lintpack.CheckerInfo
info.Name = "hexLiteral"
info.Tags = []string{"style", "experimental"}
info.Summary = ""
info.Before = `
x := 0X12
y := 0xfF`
info.After = `
x := 0x12
// (A)
y := 0xff
// (B)
y := 0xFF`

collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&hexLiteralChecker{ctx: ctx})
})
}

type hexLiteralChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}

func (c *hexLiteralChecker) warn0X(lit *ast.BasicLit) {
suggest := "0x" + lit.Value[len("0X"):]
c.ctx.Warn(lit, "prefer 0x over 0X, s/%s/%s/", lit.Value, suggest)
}

func (c *hexLiteralChecker) warnMixedDigits(lit *ast.BasicLit) {
c.ctx.Warn(lit, "don't mix hex literal letter digits casing")
}

func (c *hexLiteralChecker) VisitExpr(expr ast.Expr) {
lit := astcast.ToBasicLit(expr)
if lit.Kind != token.INT || len(lit.Value) < 3 {
return
}
if strings.HasPrefix(lit.Value, "0X") {
c.warn0X(lit)
return
}
digits := lit.Value[len("0x"):]
if strings.ToLower(digits) != digits && strings.ToUpper(digits) != digits {
c.warnMixedDigits(lit)
}
}
94 changes: 0 additions & 94 deletions checkers/hexLiterals_checker.go

This file was deleted.

23 changes: 23 additions & 0 deletions checkers/testdata/hexLiteral/negative_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package checker_test

func negatives() {
_ = 0xFF
_ = 0xff
_ = "0xfF"
}

func noLetters() {
_ = 0x11
_ = 0x0
}

func digitsAndLetters() {
_ = 0x1aa
_ = 0x1AA
}

func decimals() {
_ = 1
_ = 10
_ = 12345
}
23 changes: 23 additions & 0 deletions checkers/testdata/hexLiteral/positive_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package checker_test

func bad0X() {
/*! prefer 0x over 0X, s/0X12/0x12/ */
_ = 0X12
/*! prefer 0x over 0X, s/0XEE/0xEE/ */
_ = 0XEE
/*! prefer 0x over 0X, s/0Xaa/0xaa/ */
_ = 0Xaa
}

func mixedLetterDigits() {
/*! don't mix hex literal letter digits casing */
_ = 0xfF
/*! don't mix hex literal letter digits casing */
_ = 0xFf
/*! don't mix hex literal letter digits casing */
_ = 0x11f0F
/*! don't mix hex literal letter digits casing */
_ = 0xff11FF
/*! don't mix hex literal letter digits casing */
_ = 0xabcdE
}
7 changes: 0 additions & 7 deletions checkers/testdata/hexLiterals/negative_tests.go

This file was deleted.

15 changes: 0 additions & 15 deletions checkers/testdata/hexLiterals/positive_tests.go

This file was deleted.

0 comments on commit ed5e8e7

Please sign in to comment.