All Projects → kyoh86 → Scopelint

kyoh86 / Scopelint

Licence: bsd-3-clause
scopelint checks for unpinned variables in go programs

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Scopelint

li18nt
🌎 Lint your i18n translation files. Detect conflicting properties, duplicates and make it more readable and easier to maintain by formatting it!
Stars: ✭ 29 (-73.64%)
Mutual labels:  lint, linter, cli-app
Lockfile Lint
Lint an npm or yarn lockfile to analyze and detect security issues
Stars: ✭ 411 (+273.64%)
Mutual labels:  linter, lint
Gitlint
Linting for your git commit messages
Stars: ✭ 404 (+267.27%)
Mutual labels:  linter, lint
Gradle Lint Plugin
A pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts.
Stars: ✭ 473 (+330%)
Mutual labels:  linter, lint
Detekt
Static code analysis for Kotlin
Stars: ✭ 4,169 (+3690%)
Mutual labels:  linter, lint
Linter
Linter for Dart.
Stars: ✭ 372 (+238.18%)
Mutual labels:  linter, lint
Ktlint
An anti-bikeshedding Kotlin linter with built-in formatter
Stars: ✭ 4,629 (+4108.18%)
Mutual labels:  linter, lint
gandalf-lint
Bad Code Shall Not Pass
Stars: ✭ 29 (-73.64%)
Mutual labels:  lint, linter
Richgo
Enrich `go test` outputs with text decorations.
Stars: ✭ 544 (+394.55%)
Mutual labels:  cli-app, golang-tools
Svlint
SystemVerilog linter
Stars: ✭ 103 (-6.36%)
Mutual labels:  linter, lint
Phplint
🐛 A tool that can speed up linting of php files by running several lint processes at once.
Stars: ✭ 646 (+487.27%)
Mutual labels:  linter, lint
Exakat
The Exakat Engine : smart static analysis for PHP
Stars: ✭ 346 (+214.55%)
Mutual labels:  linter, lint
Reviewdog
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language
Stars: ✭ 4,541 (+4028.18%)
Mutual labels:  linter, lint
Awesome Lint
Linter for Awesome lists
Stars: ✭ 385 (+250%)
Mutual labels:  linter, lint
Goreporter
A Golang tool that does static analysis, unit testing, code review and generate code quality report.
Stars: ✭ 2,943 (+2575.45%)
Mutual labels:  golang-tools, linter
Checkmake
experimental linter/analyzer for Makefiles
Stars: ✭ 420 (+281.82%)
Mutual labels:  linter, lint
Stylelint
A mighty, modern linter that helps you avoid errors and enforce conventions in your styles.
Stars: ✭ 9,350 (+8400%)
Mutual labels:  linter, lint
selective
Statically find HTML anti patterns using CSS Selectors
Stars: ✭ 15 (-86.36%)
Mutual labels:  lint, linter
elint
A easy way to lint your code
Stars: ✭ 38 (-65.45%)
Mutual labels:  lint, linter
Redbot
REDbot is lint for HTTP.
Stars: ✭ 475 (+331.82%)
Mutual labels:  linter, lint

scopelint

Go Report Card CircleCI Coverage Status

scopelint checks for unpinned variables in go programs.

OBSOLETED

Use looppointer or exportloopref instead.

If you want to find lints as nervous as possible (with some false-positives), you should use looppointer.

If you want to find lints as accurately as possible (with some lints ignored), you should use exportloopref.

What's this?

Sample problem code from: https://github.com/kyoh86/scopelint/blob/master/example/readme.go

6  values := []string{"a", "b", "c"}
7  var funcs []func()
8  for _, val := range values {
9  	funcs = append(funcs, func() {
10 		fmt.Println(val)
11 	})
12 }
13 for _, f := range funcs {
14 	f()
15 }
16 /*output:
17 c
18 c
19 c
20 */
21 var copies []*string
22 for _, val := range values {
23 	copies = append(copies, &val)
24 }
25 /*(in copies)
26 &"c"
27 &"c"
28 &"c"
29 */

In Go, the val variable in the above loops is actually a single variable. So in many case (like the above), using it makes for us annoying bugs.

You can find them with scopelint, and fix it.

$ scopelint ./example/readme.go
example/readme.go:10:16: Using the variable on range scope "val" in function literal
example/readme.go:23:28: Using a reference for the variable on range scope "val"
Found 2 lint problems; failing.

(Fixed sample):

values := []string{"a", "b", "c"}
var funcs []func()
for _, val := range values {
  val := val // pin!
	funcs = append(funcs, func() {
		fmt.Println(val)
	})
}
for _, f := range funcs {
	f()
}
var copies []*string
for _, val := range values {
  val := val // pin!
	copies = append(copies, &val)
}

Install

go get -u github.com/kyoh86/scopelint

Use

Give the package paths of interest as arguments:

scopelint github.com/kyoh86/scopelint/example

To check all packages recursively in the current directory:

scopelint ./...

And also, scopelint supports the following options:

  • The --set-exit-status flag makes it to set exit status to 1 if any problem variables are found (if you DO NOT it, set --no-set-exit-status)
  • The --vendor flag enables checking in the vendor directories (if you DO NOT it, set --no-vendor flag)
  • The --test flag enables checking in the *_test.go files" (if you DO NOT it, set --no-test flag)

Nolint

To ignore issues from use an option comment like //scopelint:ignore. For example, if it's used inline (not from the beginning of the line), it ignores issues only for the line.

var copies []*string
for _, val := range values {
	copies = append(copies, &val) //scopelint:ignore
}

To ignore issues for the block of code put it on the beginning of a line before the block:

var copies []*string
//scopelint:ignore
for _, val := range values {
	copies = append(copies, &val)
}

Also, you can exclude all issues in a file by:

//scopelint:ignore
package pkg

You may add a comment explaining or justifying why //scopelint:ignore is being used on the same line as the flag itself:

//scopelint:ignore // any comment

Use with gometalinter

scopelint can be used with gometalinter in --linter flag.

gometalinter --disable-all --linter 'scope:scopelint {path}:^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$'

Exit Codes

scopelint returns 1 if any problems were found in the checked files. It returns 2 if there were any other failures.

Known Issues

  • False positive for for table tests #4
    • I won't fix it because scopelint supports ignore them all. => nolint

TODO

  • Write tests
  • License (Some codes copied from golint)
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].