Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2849)

Issue 98120043: code review 98120043: go.tools/cmd/vet: detect stupid boolean conditions (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
9 years, 11 months ago by josharian
Modified:
9 years, 9 months ago
Reviewers:
r, rsc
CC:
golang-codereviews, jscrockett01, r, gri, rsc
Visibility:
Public.

Description

go.tools/cmd/vet: detect stupid boolean conditions This CL introduces two vet checks. Statistics and code below are from a recent 50mb corpus of public code. 1. Check for redundant conjunctions and disjunctions. This check caught 26 instances, of which 20 were clearly copy/paste bugs and 6 appeared to be mere duplication. A typical example: if xResolution < 0 || xResolution < 0 { panic("SetSize(): width < 0 || height < 0") } 2. Check for expressions of the form 'x != c1 || x != c2' or 'x == c1 && x == c2', with c1 and c2 constant expressions. This check caught 16 instances, of which all were bugs. A typical example: if rf.uri.Scheme != "http" || rf.uri.Scheme != "ftp" { rf.uri.Scheme = "file" } Fixes issue 7622.

Patch Set 1 #

Patch Set 2 : diff -r 15f81970dbe3 https://code.google.com/p/go.tools #

Patch Set 3 : diff -r 15f81970dbe3 https://code.google.com/p/go.tools #

Patch Set 4 : diff -r 15f81970dbe3 https://code.google.com/p/go.tools #

Patch Set 5 : diff -r b6a3b105fbb0 https://code.google.com/p/go.tools #

Patch Set 6 : diff -r b6a3b105fbb0 https://code.google.com/p/go.tools #

Patch Set 7 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 8 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 9 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 10 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 11 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 12 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 13 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 14 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Total comments: 20

Patch Set 15 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Total comments: 8

Patch Set 16 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 17 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Total comments: 9

Patch Set 18 : diff -r 6041790e232e https://code.google.com/p/go.tools #

Patch Set 19 : diff -r 682706fb8611 https://code.google.com/p/go.tools #

Unified diffs Side-by-side diffs Delta from patch set Stats (+304 lines, -0 lines) Patch
A cmd/vet/bool.go View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 chunk +185 lines, -0 lines 0 comments Download
M cmd/vet/doc.go View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 chunk +6 lines, -0 lines 0 comments Download
A cmd/vet/testdata/bool.go View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 chunk +113 lines, -0 lines 0 comments Download

Messages

Total messages: 13
josharian
Hello golang-codereviews@googlegroups.com (cc: r@golang.org, rsc@golang.org), I'd like you to review this change to https://code.google.com/p/go.tools
9 years, 10 months ago (2014-06-26 17:57:14 UTC) #1
jscrockett01
In #2 you meant to say "with y and z constant expressions" instead of "x ...
9 years, 10 months ago (2014-06-26 18:03:02 UTC) #2
josharian
> In #2 you meant to say "with y and z constant expressions" instead of ...
9 years, 10 months ago (2014-06-26 18:04:41 UTC) #3
r
this looks interesting. i'd like gri to look at how the ast is used. https://codereview.appspot.com/98120043/diff/240001/cmd/vet/bool.go ...
9 years, 10 months ago (2014-06-26 18:11:03 UTC) #4
gri
Some initial feedback. I still need to look at commutativeDisjuncts. https://codereview.appspot.com/98120043/diff/240001/cmd/vet/bool.go File cmd/vet/bool.go (right): https://codereview.appspot.com/98120043/diff/240001/cmd/vet/bool.go#newcode32 ...
9 years, 10 months ago (2014-06-26 19:04:23 UTC) #5
gri
https://codereview.appspot.com/98120043/diff/240001/cmd/vet/bool.go File cmd/vet/bool.go (right): https://codereview.appspot.com/98120043/diff/240001/cmd/vet/bool.go#newcode46 cmd/vet/bool.go:46: func commutativeDisjuncts(f *File, e *ast.BinaryExpr) [][]ast.Expr { I think ...
9 years, 10 months ago (2014-06-27 00:44:34 UTC) #6
josharian
PTAL Thanks for the detailed, helpful feedback. I'm not sure I've done it justice. > ...
9 years, 9 months ago (2014-07-01 02:05:19 UTC) #7
rsc
lgtm; leaving for r thanks very much for implementing this. it's my most common obvious ...
9 years, 9 months ago (2014-07-01 12:50:00 UTC) #8
josharian
PTAL Thanks, rsc. I also removed seenExpr; it was complicated, not quite correct, and didn't ...
9 years, 9 months ago (2014-07-01 15:33:00 UTC) #9
r
LGTM https://codereview.appspot.com/98120043/diff/300001/cmd/vet/bool.go File cmd/vet/bool.go (right): https://codereview.appspot.com/98120043/diff/300001/cmd/vet/bool.go#newcode52 cmd/vet/bool.go:52: // commutativeSets all reorderable sets of expressions in ...
9 years, 9 months ago (2014-07-01 16:08:57 UTC) #10
gri
FYI https://codereview.appspot.com/98120043/diff/300001/cmd/vet/bool.go File cmd/vet/bool.go (right): https://codereview.appspot.com/98120043/diff/300001/cmd/vet/bool.go#newcode52 cmd/vet/bool.go:52: // commutativeSets all reorderable sets of expressions in ...
9 years, 9 months ago (2014-07-01 16:12:40 UTC) #11
josharian
All done. I'll leave this ~8hrs in case anyone wants to look again before I ...
9 years, 9 months ago (2014-07-01 16:34:52 UTC) #12
josharian
9 years, 9 months ago (2014-07-02 17:40:02 UTC) #13
*** Submitted as
https://code.google.com/p/go/source/detail?r=a2a0f87c4b38&repo=tools ***

go.tools/cmd/vet: detect stupid boolean conditions

This CL introduces two vet checks. Statistics and code below are from a recent
50mb corpus of public code.

1. Check for redundant conjunctions and disjunctions. This check caught 26
instances, of which 20 were clearly copy/paste bugs and 6 appeared to be mere
duplication. A typical example:

if xResolution < 0 || xResolution < 0 {
        panic("SetSize(): width < 0 || height < 0")
}

2. Check for expressions of the form 'x != c1 || x != c2' or 'x == c1 && x ==
c2', with c1 and c2 constant expressions. This check caught 16 instances, of
which all were bugs. A typical example:

if rf.uri.Scheme != "http" || rf.uri.Scheme != "ftp" {
        rf.uri.Scheme = "file"
}

Fixes issue 7622.

LGTM=rsc, r
R=golang-codereviews, jscrockett01, r, gri, rsc
CC=golang-codereviews
https://codereview.appspot.com/98120043
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b