floatcheck

package module
v0.0.0-...-566f5c9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2025 License: MIT Imports: 6 Imported by: 0

README

floatcheck

Go Report Card

floatcheck is a custom linter for Go that detects potential floating-point precision issues in your code.

What it detects

  • Usage of floating-point format specifiers (e.g., %.nf, %.f) in fmt.Sprintf
  • Division operations involving floating-point numbers
  • Equality comparisons (==, !=) between floating-point numbers
  • Relational comparisons (<, <=, >, >=) between floating-point numbers

Warning: This linter warns about potential patterns. It is up to the user to determine if the warning is an actual issue in their context.

Installation & Usage

1. To check for all patterns at once
go install github.com/yudppp/floatcheck/cmd/floatcheck
go vet -vettool=$(which floatcheck) your_project_path/...
2. To use each check individually
  • Format only: floatcheck_format
  • Division only: floatcheck_division
  • Comparison only: floatcheck_comparison
go install github.com/yudppp/floatcheck/cmd/floatcheck_format
# or
go install github.com/yudppp/floatcheck/cmd/floatcheck_division
# or
go install github.com/yudppp/floatcheck/cmd/floatcheck_comparison

Testing

go test ./...

Or you can manually check the samples under the testdata directory.

Example

package main
import "fmt"
func main() {
  var pi float64 = 3.14159
  var result float32 = 1.0 / 3.0
  formatted := fmt.Sprintf("%.2f", pi) // warning
  if result == 0.333 { // warning
    println("Result is approximately one-third")
  }
}

Contributing

Pull requests and issues are welcome! Please follow the Go style guide and add tests for new features or bug fixes.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllAnalyzer = &analysis.Analyzer{
	Name: "floatcheck_all",
	Doc:  "checks for potential floating-point precision issues in fmt.Sprintf formatting, division, and comparison",
	Run: func(pass *analysis.Pass) (any, error) {
		inspect := func(node ast.Node) bool {
			checkFormat(pass, node)
			checkDivision(pass, node)
			checkComparison(pass, node)
			return true
		}
		for _, file := range pass.Files {
			ast.Inspect(file, inspect)
		}
		return nil, nil
	},
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer for all checks

View Source
var ComparisonAnalyzer = &analysis.Analyzer{
	Name: "floatcheck_comparison",
	Doc:  "checks for potential floating-point comparison issues",
	Run: func(pass *analysis.Pass) (any, error) {
		inspect := func(node ast.Node) bool {
			checkComparison(pass, node)
			return true
		}
		for _, file := range pass.Files {
			ast.Inspect(file, inspect)
		}
		return nil, nil
	},
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer for comparison check only

View Source
var DivisionAnalyzer = &analysis.Analyzer{
	Name: "floatcheck_division",
	Doc:  "checks for potential floating-point division precision issues",
	Run: func(pass *analysis.Pass) (any, error) {
		inspect := func(node ast.Node) bool {
			checkDivision(pass, node)
			return true
		}
		for _, file := range pass.Files {
			ast.Inspect(file, inspect)
		}
		return nil, nil
	},
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer for division check only

View Source
var FormatAnalyzer = &analysis.Analyzer{
	Name: "floatcheck_format",
	Doc:  "checks for potential floating-point precision issues in fmt.Sprintf formatting",
	Run: func(pass *analysis.Pass) (any, error) {
		inspect := func(node ast.Node) bool {
			checkFormat(pass, node)
			return true
		}
		for _, file := range pass.Files {
			ast.Inspect(file, inspect)
		}
		return nil, nil
	},
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer for format check only

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
floatcheck command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL