Skip to content

griesemer/tryhard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tryhard command

tryhard is a simple tool to list and rewrite try candidate statements. It operates on a file-by-file basis and does not type-check the code; potential statements are recognized and rewritten purely based on pattern matching. tryhard may report (a usually very small number of) false positives; the likelyhood for false positives may be lowered by providing the flag -err="err". It also undercounts try candidates because tryhard does not recognize opportunities for shared, deferred handlers. Use caution when using the rewrite feature (-r flag) and have a backup as needed.

The command accepts a list of files or directories which it processes in the order they are provided. Given a file, it operates on that file no matter the file path. Given a directory, it operates on all .go files in that directory, recursively. Files starting with a period are ignored. Files may be explicitly ignored with the -ignore flag.

tryhard considers each top-level function with a last result of named type error, which may or may not be the predefined type error. Inside these functions tryhard considers assignments of the form

v1, v2, ..., vn, <err> = f() // can also be := instead of =

followed by an if statement of the form

if <err> != nil {
	return ..., <err>
}

or an if statement with an init expression matching the above assignment. The error variable may have any name, unless specified explicitly with the -err flag; the variable may or may not be of type error or correspond to the result error. The return statement must contain one or more return expressions, with all but the last one denoting a zero value of sorts (a zero number literal, an empty string, an empty composite literal, etc.). The last result must be the variable .

Unless no files were found, tryhard reports various counts at the end of its run.

CAUTION: If the rewrite flag (-r) is specified, the file is updated in place! Make sure you can revert to the original.

Function literals (closures) are currently ignored.

Usage:

tryhard [flags] [path ...]

The flags are:

-l
	list positions of potential `try` candidate statements
-r
	rewrite potential `try` candidate statements to use `try`
-err
	name of error variable; using "" permits any name (default `""`)
-ignore string
    	ignore files with paths matching this (regexp) pattern (default `"vendor"`)

About

tryhard finds and rewrites code suitable for try.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages