Skip to content

Bool Alias: comparison to bool constant #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
awesee opened this issue Nov 15, 2019 · 4 comments
Closed
3 tasks done

Bool Alias: comparison to bool constant #857

awesee opened this issue Nov 15, 2019 · 4 comments
Labels
bug Something isn't working dependencies Relates to an upstream dependency won't fix This will not be worked on

Comments

@awesee
Copy link

awesee commented Nov 15, 2019

Thank you for creating the issue!

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc).

Please include the following information:

Version of golangci-lint
$ golangci-lint --version
golangci-lint has version 1.20.1 built from 849044b on 2019-10-15T19:11:27Z
Config file
$ cat .golangci.yml
cat: .golangci.yml: No such file or directory
Go environment
$ go version && go env
go version go1.13.4 darwin/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/Shuo/go/bin"
GOCACHE="/Users/Shuo/Library/Caches/go-build"
GOENV="/Users/Shuo/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/Shuo/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/usr/local/Cellar/go/1.13.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r_/kfg_zdzx05qgnd5_wfrrr_l00000gn/T/go-build967944465=/tmp/go-build -gno-record-gcc-switches -fno-common"
Verbose output of running
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/Shuo/openset/leetcode/internal/description /Users/Shuo/openset/leetcode/internal /Users/Shuo/openset/leetcode /Users/Shuo/openset /Users/Shuo /Users /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (files|imports|name|types_sizes|compiled_files|deps|exports_file) took 419.885783ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 433.256µs 
INFO [runner] Processors filtering stat (out/in): max_per_file_from_linter: 1/1, max_from_linter: 1/1, path_shortener: 1/1, filename_unadjuster: 1/1, path_prettifier: 1/1, skip_dirs: 1/1, nolint: 1/1, cgo: 1/1, skip_files: 1/1, identifier_marker: 1/1, exclude: 1/1, diff: 1/1, max_same_issues: 1/1, source_code: 1/1, autogenerated_exclude: 1/1, exclude-rules: 1/1, uniq_by_line: 1/1 
INFO [runner] processing took 585.727µs with stages: nolint: 159.943µs, source_code: 132.447µs, exclude: 78.448µs, path_prettifier: 77.795µs, autogenerated_exclude: 63.181µs, identifier_marker: 35.112µs, skip_dirs: 13.96µs, filename_unadjuster: 13.125µs, uniq_by_line: 4.127µs, max_same_issues: 2.137µs, cgo: 1.713µs, path_shortener: 1.533µs, max_from_linter: 1.015µs, max_per_file_from_linter: 484ns, exclude-rules: 258ns, skip_files: 240ns, diff: 209ns 
INFO [runner] linters took 336.921437ms with stages: goanalysis_metalinter: 214.262917ms, unused: 121.955562ms 
internal/description/description.go:43:32: S1002: should omit comparison to bool constant, can be simplified to `problem.PaidOnly` (gosimple)
                if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive {
                                             ^
INFO File cache stats: 1 entries of total size 1.3KiB 
INFO Memory: 9 samples, avg is 68.9MB, max is 68.9MB 
INFO Execution took 781.125715ms       
@awesee
Copy link
Author

awesee commented Nov 15, 2019

package main

import "fmt"

type B bool

func (b B) Val() bool {
	// code like this can be simplified to `bool(b)`, not `b`
	return b == true
}

func (b B) String() string {
	if b {
		return "true"
	}
	return "false"
}

func main() {
	var b B
	// code like this can be simplified to `bool(b)`, not `b`
	// this may be ok, not need simplify. `foo.bar.bool.val == true` VS `bool(foo.bar.bool.val)`
	if b == true && fb() {
		fmt.Println("this is true")
	} else {
		fmt.Println("this is false")
	}
}

func fb() bool {
	return false
}

@tpounds
Copy link
Contributor

tpounds commented Dec 30, 2019

@openset Thanks for the report. In the provided examples are you saying 1. it's not returning an error when it should, 2. returning an error when it should not, or 3. returning an error with the wrong message?

@tpounds tpounds added bug Something isn't working feedback required Requires additional feedback labels Dec 30, 2019
@awesee
Copy link
Author

awesee commented Dec 31, 2019

@tpounds returning an error when it should not

$ golangci-lint run main.go 

main.go:9:9: S1002: should omit comparison to bool constant, can be simplified to `b` (gosimple)
        return b == true
               ^
main.go:23:5: S1002: should omit comparison to bool constant, can be simplified to `b` (gosimple)
        if b == true && fb() {
           ^

@tpounds tpounds added dependencies Relates to an upstream dependency won't fix This will not be worked on and removed feedback required Requires additional feedback labels Dec 31, 2019
@tpounds
Copy link
Contributor

tpounds commented Dec 31, 2019

@openset Thanks for the additional info. I was able to reproduce the issue you have described, unfortunately, gosimple's detection logic is implemented as part of the Staticcheck analyzers suite. I think the best path forward is to add //nolint directives to suppress the warnings as necessary and report the issue to the upstream project to change the behavior.

@tpounds tpounds closed this as completed Dec 31, 2019
vitalyisaev2 pushed a commit to vitalyisaev2/golangci-lint that referenced this issue Jan 24, 2020
This update should fix go get issues because of deps.

go-critic:
$ git cherry -v c3db6069acc5
+ fe28ac328f474c02e2383ca2bf44a606929a7048 checkers: add integration
tests with cgo for dupImports (golangci#846)
+ 48a15b03b630252319474ba5ddc8455d2aebf34f checkers: fix fold-ranges
for floats in boolExprSimplify (golangci#849)
+ 7bf73388643eb226addf2d5ed8a2c104be244b2e checkers: fix "Output:"
false positive in commentedOutCode (golangci#852)
+ f6f702b31734df26415c2bd135f272d1a34d2973 checkers: extend
yodaStyleExpr supported ops list (golangci#856)
+ 07bf84df361735ad1d90f84e79eb60c8386325c7 fix dependencies (golangci#857)
+ 1df30086654074503eab008bdde4f3ce1921128d checkers: fix collection
URL (golangci#860)

x/tools:
$ git cherry -v 685fecacd0a0 521d6ed310dd | fgrep -v internal/lsp
+ fe54fb35175bb1c0c175e2335e23d7fa90ca987a apidiff: represent a Report as a list of Changes
+ 15bbd99efc6f20619676dec93f914d12e5139e83 all: run go mod tidy
+ bb3b3ca95aec36bfc4a5cb10b58022e64aca735b go/packages: add some documentation for extractPackage
+ 4bf14f7f0668366a4275c6ef5e99bd8e807da1d3 internal/span: fix off-by-one in ToUTF16Column
+ cb2dda6eabdf9160e66ca7293897f984154a7f8b go/packages: deduplicate file parsing
+ 9e44c1c403071e0c2831952513e7b948587d94af go/internal/gccgoimporter: update package to match std lib version
+ 36563e24a2627da92566d43aa1c7a2dd895fc60d cmd/vet: verify potentially-recursive Stringers are actually Stringers
+ 31fd60d6bfdcfb2861856aa5b333bb135f6bbfd8 x/tools/go/packages/packagestest: fix GOPROXY file URLs for Windows
+ 2d660fb8a000e1c288dc2f2150401b364922ebe9 go/packages/packagestest: fix GOPROXY file URLs for Windows
+ 7af746645d5165109de0b5cb499980c22812dfc2 internal/span: fix another off-by-one in ToUTF16Column
+ 9d4d845e86f14303813298ede731a971dd65b593 cmd/goimports: add -format-only flag
+ 83df196e5764ed2415c28c1f39ba6cb3db747da0 internal/span: add a filename only print for spans
+ 5cec639030af3a6ada2732d9bfa1d3731ed55106 go/analysis: proposed fact enumeration API
+ 9cb3dcf692a103de0fd68c26f4f04183e0933f7c internal/span: update the offset if the end offset should be valid but is not
+ 2d16b83fe98cd1bed9e2ce9fdc86bd438d14aab7 go/vcs: ignore "mod" VCS type
+ 95299016986435f846545c27f956768ad3c3cb2f lostcancel: do not analyze cancel variable which defined outside current function scope
+ 8a42e17289ea392d63892321ce1f40bd07efcc9f compilebench: clean up different benchmark types
+ eeb76a0c47a3b97e99f330dc14174ef14777d2ba compilebench: factor running build tool commands
+ 60140f09094406e46618ef436a26dd8394983503 compilebench: add a linker benchmark
+ e31d36578abb3d202c4007c3747bf8ebb7c51011 compilebench: handle missing MemStats more gracefully
+ d996b19ee77cd9c8df974510f23b0696cedf1ca1 go/analysis/analysistest: fix word usage
+ 73554e0f78058c37e5421bc48273a72400172221 go/analysis/passes: fix bugs discovered in std
+ 35884eef200b5fc81c9044f644a8d9d911262488 cmd/vet: print help to stdout only
+ 45e43b2cb4facd370abb846ebf35575161849f3b go/packages/packagestest: fix MustCopyFileTree so that file fragments are always slash form
+ d81a07b7e58487eed036bf115fa834653590d6cd go/analysis/passes/bools: eliminate quadratic runtime, output
+ 2a413a02cc735933997fa1b467a7b27bc1b82567 godoc/static: let client use vet check returned by /compile endpoint
+ 4789ca9922f080dd3a8fc3c74df1c1306db2bb0b go/analysis/internal/analysisflags: call gob.Register on deleted analyzers
+ d1a3278ee74994e9aa609e9e711c616bba677d5d godoc/util: serve SVG files raw
+ 1da8801a9502f29f3f03edfc3149b947e6e1913c godoc: declare small victory over the punched card tyranny
+ 757ca719ca9689950c69081c10c5300fbb8e35db imports: rename to internal/imports
+ 0133cac3176f225883c5d817146de8633ed07ebc cmd/goimports: reuse cached state
+ 26647e34d3c04fd3eaef6fb824493b00af7b1b26 imports: allow nil Options in Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Relates to an upstream dependency won't fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants