summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mgechev/revive/rule/constant-logical-expr.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-04-04 03:29:12 +0800
committerGitHub <noreply@github.com>2020-04-03 22:29:12 +0300
commit4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad (patch)
treedd5dc2cae6ebae21826ffcce937533559be45a07 /vendor/github.com/mgechev/revive/rule/constant-logical-expr.go
parent4af7c47b38d382d105726f9553a1a68d46882cbf (diff)
downloadgitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.tar.gz
gitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.zip
Rename scripts to build and add revive command as a new build tool command (#10942)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'vendor/github.com/mgechev/revive/rule/constant-logical-expr.go')
-rw-r--r--vendor/github.com/mgechev/revive/rule/constant-logical-expr.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/vendor/github.com/mgechev/revive/rule/constant-logical-expr.go b/vendor/github.com/mgechev/revive/rule/constant-logical-expr.go
new file mode 100644
index 0000000000..6a91561111
--- /dev/null
+++ b/vendor/github.com/mgechev/revive/rule/constant-logical-expr.go
@@ -0,0 +1,88 @@
+package rule
+
+import (
+ "github.com/mgechev/revive/lint"
+ "go/ast"
+ "go/token"
+)
+
+// ConstantLogicalExprRule warns on constant logical expressions.
+type ConstantLogicalExprRule struct{}
+
+// Apply applies the rule to given file.
+func (r *ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
+ var failures []lint.Failure
+
+ onFailure := func(failure lint.Failure) {
+ failures = append(failures, failure)
+ }
+
+ astFile := file.AST
+ w := &lintConstantLogicalExpr{astFile, onFailure}
+ ast.Walk(w, astFile)
+ return failures
+}
+
+// Name returns the rule name.
+func (r *ConstantLogicalExprRule) Name() string {
+ return "constant-logical-expr"
+}
+
+type lintConstantLogicalExpr struct {
+ file *ast.File
+ onFailure func(lint.Failure)
+}
+
+func (w *lintConstantLogicalExpr) Visit(node ast.Node) ast.Visitor {
+ switch n := node.(type) {
+ case *ast.BinaryExpr:
+ if !w.isOperatorWithLogicalResult(n.Op) {
+ return w
+ }
+
+ if gofmt(n.X) != gofmt(n.Y) { // check if subexpressions are the same
+ return w
+ }
+
+ if n.Op == token.EQL {
+ w.newFailure(n, "expression always evaluates to true")
+ return w
+ }
+
+ if w.isInequalityOperator(n.Op) {
+ w.newFailure(n, "expression always evaluates to false")
+ return w
+ }
+
+ w.newFailure(n, "left and right hand-side sub-expressions are the same")
+ }
+
+ return w
+}
+
+func (w *lintConstantLogicalExpr) isOperatorWithLogicalResult(t token.Token) bool {
+ switch t {
+ case token.LAND, token.LOR, token.EQL, token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
+ return true
+ }
+
+ return false
+}
+
+func (w *lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
+ switch t {
+ case token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
+ return true
+ }
+
+ return false
+}
+
+func (w lintConstantLogicalExpr) newFailure(node ast.Node, msg string) {
+ w.onFailure(lint.Failure{
+ Confidence: 1,
+ Node: node,
+ Category: "logic",
+ Failure: msg,
+ })
+}