Skip to content

Commit

Permalink
Add 'floatcompare' linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mweb committed Apr 5, 2022
1 parent f5b92e1 commit e89deb5
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ linters-settings:
- '*.Test'
- 'example.com/package.ExampleStruct'

floatcompare:
# Search only for == and != no other comparisons
# Default: false
equal-only: true

forbidigo:
# Forbid the following identifiers (list of regexp).
forbid:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-ps v1.0.0
github.com/moricho/tparallel v0.2.1
github.com/mweb/floatcompare v1.0.1
github.com/nakabonne/nestif v0.3.1
github.com/nishanths/exhaustive v0.7.11
github.com/nishanths/predeclared v0.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type LintersSettings struct {
ErrorLint ErrorLintSettings
Exhaustive ExhaustiveSettings
ExhaustiveStruct ExhaustiveStructSettings
FloatCompare FloatCompareSettings
Forbidigo ForbidigoSettings
Funlen FunlenSettings
Gci GciSettings
Expand Down Expand Up @@ -255,6 +256,10 @@ type ExhaustiveStructSettings struct {
StructPatterns []string `mapstructure:"struct-patterns"`
}

type FloatCompareSettings struct {
EqualOnly bool `mapstructure:"equal-only"`
}

type ForbidigoSettings struct {
Forbid []string `mapstructure:"forbid"`
ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"`
Expand Down
31 changes: 31 additions & 0 deletions pkg/golinters/floatcompare.go
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)
}
8 changes: 8 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var errorlintCfg *config.ErrorLintSettings
var exhaustiveCfg *config.ExhaustiveSettings
var exhaustiveStructCfg *config.ExhaustiveStructSettings
var floatCompareStructCfg *config.FloatCompareSettings
var gciCfg *config.GciSettings
var goModDirectivesCfg *config.GoModDirectivesSettings
var goMndCfg *config.GoMndSettings
Expand Down Expand Up @@ -140,6 +141,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
floatCompareStructCfg = &m.cfg.LintersSettings.FloatCompare
gciCfg = &m.cfg.LintersSettings.Gci
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
goMndCfg = &m.cfg.LintersSettings.Gomnd
Expand Down Expand Up @@ -289,6 +291,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/kyoh86/exportloopref"),

linter.NewConfig(golinters.NewFloatCompare(floatCompareStructCfg)).
WithSince("v.1.46.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/mweb/floatcompare"),

linter.NewConfig(golinters.NewForbidigo()).
WithSince("v1.34.0").
WithPresets(linter.PresetStyle).
Expand Down
67 changes: 67 additions & 0 deletions test/testdata/floatcompare.go
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() {}

0 comments on commit e89deb5

Please sign in to comment.