• This repository has been archived on 25/May/2021
  • Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

scopelint checks for unpinned variables in go programs

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)

More Repositories

1

richgo

Enrich `go test` outputs with text decorations.
Go
839
star
2

exportloopref

Go
121
star
3

looppointer

An analyzer that checks for pointers to enclosing loop variables.
Go
38
star
4

momiji

🍁 My color scheme for vim/iTerm2, "momiji" that means Japanese "autumn leaves"🍂
Vim Script
38
star
5

gogh

GO GitHub project manager
Go
31
star
6

vim-ripgrep

A plugin for Vim8/Neovim to search text by ripgrep (rg) asynchronously
Vim Script
28
star
7

xdg

Light weight helper functions in golang to get config, data and cache files according to the XDG Base Directory Specification.
Go
18
star
8

gordon

Go
15
star
9

denops-ollama.vim

TypeScript
15
star
10

telescope-windows.nvim

Lua
13
star
11

vim-editerm

Shell
13
star
12

climbdir.nvim

Lua
11
star
13

git-vertag

A tool to manage version-tag with the semantic versioning specification.
Go
8
star
14

gitstat.nvim

A lua plugin for neovim that shows git-status on the top of the editor.
Lua
8
star
15

git-branches

Retrieve the list of branches with last commit author
Go
7
star
16

go-docbase

A Go library for accessing the Docbase API v1/v2
Go
7
star
17

git-statuses

finds local git repositories and show statuses of them
Go
7
star
18

ddu-source-git_log

ddu.vim source collects commit log by using `git log` command
TypeScript
6
star
19

ddu-filter-converter_hl_dir

ddu.vim converter that highlights directory path of file-like items.
TypeScript
6
star
20

vim-jsonl

Syntax file for JSON Lines on vim/Neovim
Vim Script
6
star
21

zenn-auto-publish-action

JavaScript
5
star
22

vim-docbase

Vim Script
5
star
23

git-vertag-action

Dockerfile
5
star
24

csv2xlsx

Go
5
star
25

curtain.nvim

Vim Script
4
star
26

markdown-image.nvim

Markdownに置かれた画像を、適切な場所に配置し直して、 そこへのリンクに置き換えてくれるneovimプラグイン
Lua
4
star
27

go-spdx

The package parses SPDX license expression strings describing license terms.
Go
4
star
28

telescope-gogh.nvim

Lua
3
star
29

ddu-source-git_branch

ddu.vim source shows branches in the repository
TypeScript
3
star
30

backgroundfile.nvim

A neovim plugin to open a file in background (hidden floatwin).
Lua
3
star
31

goimportssw

Shorthand for `gofmt -s -w $1 && goimports -w $1`
Shell
2
star
32

zshist

Go
2
star
33

git-prompt

Go
2
star
34

ddu-source-github

A source for ddu.vim to manipulate GitHub
TypeScript
2
star
35

gigamoji

Go
2
star
36

inkdrop.nvim

A neovim client to access inkdrop
TypeScript
2
star
37

dotfiles

all of me in the computer
Lua
2
star
38

go-check-action

Go
2
star
39

bdelete-buffers.nvim

A vim plugin to enhance to close buffers
Lua
1
star
40

delete-buffers.nvim

1
star
41

vim-beedle

Vim Script
1
star
42

ddu-source-git_diff_tree

ddu.vim source collects files in the commit
TypeScript
1
star
43

unload-buffers.nvim

A Neovim plugin to enhance to close buffers
Lua
1
star
44

notifail

Go
1
star
45

pyenv-upgrade

Go
1
star
46

vim-packload

Vim Script
1
star
47

nolint

Go
1
star
48

telescope-zenn.nvim

Lua
1
star
49

nvim-minimal

Shell
1
star
50

ddu-source-zenn_dev

ddu.vim source collects articles in the repository for zenn.dev.
TypeScript
1
star
51

ddu-source-quickfix_history

Quickfix history source for ddu.vim
TypeScript
1
star
52

denops-zenn_dev.vim

A Vim plugin to manage Zenn.dev using denops.vim (without Node.js)
TypeScript
1
star