forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkers: refactor and fix hexLiteral checker (go-critic#789)
Also renamed it from hexLiterals to hexLiteral. Fixes go-critic#780 Signed-off-by: Iskander Sharipov <[email protected]>
- Loading branch information
Showing
6 changed files
with
106 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
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 | ||
} |
This file contains 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
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.