-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
0 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,31 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/mweb/floatcompare" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
const floatcompareName = "floatcompare" | ||
|
||
func NewFloatCompare(settings *config.FloatCompareSettings) *goanalysis.Linter { | ||
a := floatcompare.NewAnalyzer() | ||
|
||
var cfg map[string]map[string]interface{} | ||
if settings != nil { | ||
d := map[string]interface{}{ | ||
"equalOnly": settings.EqualOnly, | ||
} | ||
|
||
cfg = map[string]map[string]interface{}{a.Name: d} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
floatcompareName, | ||
"search for float comparison, since these are potential errors", | ||
[]*analysis.Analyzer{a}, | ||
cfg, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
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
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,67 @@ | ||
// args: -Efloatcompare | ||
package testdata | ||
|
||
func EqualCompareIfFloats() { | ||
x, y := 400., 500. | ||
if 300. == 100. { // ERROR `float comparison found "300. == 100."` | ||
dummy() | ||
} | ||
if x == y { // ERROR `float comparison found "x == y"` | ||
dummy() | ||
} | ||
if 300.+200. == 10. { // ERROR `float comparison found "300.+200. == 10."` | ||
dummy() | ||
} | ||
if 300 == 200 { | ||
dummy() | ||
} | ||
} | ||
|
||
func NotEqualCompareIfFloats() { | ||
x, y := 400., 500. | ||
if 300. != 100. { // ERROR `float comparison found "300. != 100."`` | ||
|
||
dummy() | ||
} | ||
if x != y { // ERROR `float comparison found "x != y"` | ||
dummy() | ||
} | ||
} | ||
|
||
func EqualCompareIfCustomType() { | ||
type number float64 | ||
var x, y number = 300., 400. | ||
if x == y { // ERROR `float comparison found "x == y"` | ||
dummy() | ||
} | ||
} | ||
|
||
func GreaterLessCompareIfFloats() { | ||
if 300. >= 100. { // ERROR `float comparison found "300. >= 100."` | ||
dummy() | ||
} | ||
if 300. <= 100. { // ERROR `float comparison found "300. <= 100."` | ||
dummy() | ||
} | ||
if 300. < 100. { // ERROR `float comparison found "300. < 100."` | ||
dummy() | ||
} | ||
if 300. > 100. { // ERROR `float comparison found "300. > 100."` | ||
dummy() | ||
} | ||
} | ||
|
||
func SwitchStmtWithFloat() { | ||
switch 300. { // ERROR "float comparison with switch statement" | ||
case 100.: | ||
case 200: | ||
} | ||
} | ||
|
||
func EqualCompareSwitchFloats() { | ||
switch { | ||
case 100. == 200.: // ERROR `float comparison found "100. == 200."` | ||
} | ||
} | ||
|
||
func dummy() {} |