predeclared

command module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

README

predeclared Build Status Godoc

Find code that overrides one of Go's predeclared identifiers (new, make, append uint, etc.).

The list of predeclared identifiers can be found in the spec.

go get github.com/nishanths/predeclared

See godoc or run predeclared without arguments to print usage.

Test

cd passes/predeclared && go test

Examples

Given a package with the file:

package pkg // import "example.org/foo/pkg"

func copy()  {}
func print() {}

func foo() string {
	string := "x"
	return string
}

type int struct{}

running:

predeclared example.org/foo/pkg

prints:

example.go:3:6: function "copy" has same name as predeclared identifier
example.go:4:6: function "print" has same name as predeclared identifier
example.go:7:2: variable "string" has same name as predeclared identifier
example.go:11:6: type "int" has same name as predeclared identifier

Running the program on the standard library's text package and subpackages produces:

$ predeclared text/...
/usr/local/go/src/text/template/parse/node.go:554:33: param "true" has same name as predeclared identifier
/usr/local/go/src/text/template/parse/node.go:599:3: variable "rune" has same name as predeclared identifier
/usr/local/go/src/text/template/funcs.go:164:36: param "cap" has same name as predeclared identifier
/usr/local/go/src/text/template/funcs.go:233:3: variable "cap" has same name as predeclared identifier
/usr/local/go/src/text/template/template.go:218:30: param "new" has same name as predeclared identifier
/usr/local/go/src/text/template/exec_test.go:232:21: param "error" has same name as predeclared identifier

Documentation

Overview

Command predeclared prints the names and locations of declarations in the given packages that have the same name as one of Go's predeclared identifiers (eg., int, string, delete, copy, append).

Usage

The command line usage is:

predeclared [flags] [packages...]

Run 'predeclared' without arguments for help.

Flags

The '-q' boolean flag, if set, indicates to the command to check struct field names, interface methods, and method names — in addition to the default checks. (These checks aren't included by default since fields and method are always accessed by a qualifier—à la obj.Field—and hence are less likely to cause confusion when reading code even if they have the same name as a predeclared identifier.)

The '-ignore' string flag can be used to specify predeclared identifiers to not report issues for. For example, to not report overriding of the predeclared identifiers 'new' and 'real', set the flag like so:

-ignore=new,real

Directories

Path Synopsis
passes
predeclared
Package predeclared provides a static analysis (used by the predeclared command) that can detect declarations in Go code that shadow one of Go's predeclared identifiers.
Package predeclared provides a static analysis (used by the predeclared command) that can detect declarations in Go code that shadow one of Go's predeclared identifiers.

Jump to

Keyboard shortcuts

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