All Projects → alexkohler → nakedret

alexkohler / nakedret

Licence: MIT license
nakedret is a Go static analysis tool to find naked returns in functions greater than a specified function length.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to nakedret

Sonar Dotnet
Code analyzer for C# and VB.NET projects https://redirect.sonarsource.com/plugins/vbnet.html
Stars: ✭ 466 (+468.29%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Sonar Java
☕️ SonarSource Static Analyzer for Java Code Quality and Security
Stars: ✭ 745 (+808.54%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Phpdoc Parser
Next-gen phpDoc parser with support for intersection types and generics
Stars: ✭ 569 (+593.9%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Dg
[LLVM Static Slicer] Various program analyses, construction of dependence graphs and program slicing of LLVM bitcode.
Stars: ✭ 242 (+195.12%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
identypo
identypo is a Go static analysis tool to find typos in identifiers (functions, function calls, variables, constants, type declarations, packages, labels).
Stars: ✭ 26 (-68.29%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Prealloc
prealloc is a Go static analysis tool to find slice declarations that could potentially be preallocated.
Stars: ✭ 419 (+410.98%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
lints
Lint all your JavaScript, CSS, HTML, Markdown and Dockerfiles with a single command
Stars: ✭ 14 (-82.93%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Phpstan Doctrine
Doctrine extensions for PHPStan
Stars: ✭ 338 (+312.2%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
sonarlint4netbeans
SonarLint integration for Apache Netbeans
Stars: ✭ 23 (-71.95%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
phpstan-nette
Nette Framework class reflection extension for PHPStan & framework-specific rules
Stars: ✭ 87 (+6.1%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
eba
EBA is a static bug finder for C.
Stars: ✭ 14 (-82.93%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
unimport
unimport is a Go static analysis tool to find unnecessary import aliases.
Stars: ✭ 64 (-21.95%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Sonarjs
SonarSource Static Analyzer for JavaScript and TypeScript
Stars: ✭ 696 (+748.78%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Cfmt
cfmt is a tool to wrap Go comments over a certain length to a new line.
Stars: ✭ 28 (-65.85%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Phpinspectionsea
A Static Code Analyzer for PHP (a PhpStorm/Idea Plugin)
Stars: ✭ 1,211 (+1376.83%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Phpstan
PHP Static Analysis Tool - discover bugs in your code without running it!
Stars: ✭ 10,534 (+12746.34%)
Mutual labels:  static-code-analysis, static-analysis, static-analyzer
Forbidden Apis
Policeman's Forbidden API Checker
Stars: ✭ 216 (+163.41%)
Mutual labels:  static-code-analysis, static-analysis
Revive
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Stars: ✭ 3,139 (+3728.05%)
Mutual labels:  static-code-analysis, static-analysis
Phpstan Phpunit
PHPUnit extensions and rules for PHPStan
Stars: ✭ 247 (+201.22%)
Mutual labels:  static-code-analysis, static-analysis
Spotbugs
SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.
Stars: ✭ 2,569 (+3032.93%)
Mutual labels:  static-code-analysis, static-analysis

nakedret

nakedret is a Go static analysis tool to find naked returns in functions greater than a specified function length.

Installation

go get -u github.com/alexkohler/nakedret

Usage

Similar to other Go static anaylsis tools (such as golint, go vet) , nakedret can be invoked with one or more filenames, directories, or packages named by its import path. Nakedret also supports the ... wildcard.

nakedret [flags] files/directories/packages

Currently, the only flag supported is -l, which is an optional numeric flag to specify the maximum length a function can be (in terms of line length). If not specified, it defaults to 5.

Purpose

As noted in Go's Code Review comments:

Naked returns are okay if the function is a handful of lines. Once it's a medium sized function, be explicit with your return values. Corollary: it's not worth it to name result parameters just because it enables you to use naked returns. Clarity of docs is always more important than saving a line or two in your function.

This tool aims to catch naked returns on non-trivial functions.

Example

Let's take the types package in the Go source as an example:

$ nakedret -l 25 types/
types/check.go:245 checkFiles naked returns on 26 line function 
types/typexpr.go:443 collectParams naked returns on 53 line function 
types/stmt.go:275 caseTypes naked returns on 27 line function 
types/lookup.go:275 MissingMethod naked returns on 39 line function

Below is one of the not so intuitive uses of naked returns in types/lookup.go found by nakedret (nakedret will return the line number of the last naked return in the function):

func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool) {
	// fast path for common case
	if T.Empty() {
		return
	}

	// TODO(gri) Consider using method sets here. Might be more efficient.

	if ityp, _ := V.Underlying().(*Interface); ityp != nil {
		// TODO(gri) allMethods is sorted - can do this more efficiently
		for _, m := range T.allMethods {
			_, obj := lookupMethod(ityp.allMethods, m.pkg, m.name)
			switch {
			case obj == nil:
				if static {
					return m, false
				}
			case !Identical(obj.Type(), m.typ):
				return m, true
			}
		}
		return
	}

	// A concrete type implements T if it implements all methods of T.
	for _, m := range T.allMethods {
		obj, _, _ := lookupFieldOrMethod(V, false, m.pkg, m.name)

		f, _ := obj.(*Func)
		if f == nil {
			return m, false
		}

		if !Identical(f.typ, m.typ) {
			return m, true
		}
	}

	return
}

TODO

  • Unit tests (may require some refactoring to do correctly)
  • supporting toggling of build.Context.UseAllFiles may be useful for some.
  • Configuration on whether or not to run on test files
  • Vim quickfix format?

Contributing

Pull requests welcome!

Other static analysis tools

If you've enjoyed nakedret, take a look at my other static anaylsis tools!

  • unimport - Finds unnecessary import aliases
  • prealloc - Finds slice declarations that could potentially be preallocated.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].