From bc2aed1fd011d6e4ea2a18aea2f4b7083161b935 Mon Sep 17 00:00:00 2001 From: chavacava Date: Mon, 22 Sep 2025 12:39:22 +0200 Subject: [PATCH] rename rule as unnecessary-if --- README.md | 2 +- RULES_DESCRIPTIONS.md | 6 ++--- config/config.go | 2 +- ...ssary_conditional.go => unnecessary_if.go} | 24 +++++++++---------- test/unnecessary_conditional_test.go | 12 ---------- test/unnecessary_if_test.go | 12 ++++++++++ ...ssary_conditional.go => unnecessary_if.go} | 2 +- 7 files changed, 30 insertions(+), 30 deletions(-) rename rule/{unnecessary_conditional.go => unnecessary_if.go} (82%) delete mode 100644 test/unnecessary_conditional_test.go create mode 100644 test/unnecessary_if_test.go rename testdata/{unnecessary_conditional.go => unnecessary_if.go} (98%) diff --git a/README.md b/README.md index 18b8475..9f18dfb 100644 --- a/README.md +++ b/README.md @@ -591,7 +591,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a | [`unexported-naming`](./RULES_DESCRIPTIONS.md#unexported-naming) | n/a | Warns on wrongly named un-exported symbols | no | no | | [`unexported-return`](./RULES_DESCRIPTIONS.md#unexported-return) | n/a | Warns when a public return is from unexported type. | yes | yes | | [`unhandled-error`](./RULES_DESCRIPTIONS.md#unhandled-error) | []string | Warns on unhandled errors returned by function calls | no | yes | -| [`unnecessary-conditional`](./RULES_DESCRIPTIONS.md#unnecessary-conditional) | n/a | Identifies `if-else` statements that can be replaced by simpler statements | no | no | +| [`unnecessary-if`](./RULES_DESCRIPTIONS.md#unnecessary-if) | n/a | Identifies `if-else` statements that can be replaced by simpler statements | no | no | | [`unnecessary-format`](./RULES_DESCRIPTIONS.md#unnecessary-format) | n/a | Identifies calls to formatting functions where the format string does not contain any formatting verbs | no | no | | [`unnecessary-stmt`](./RULES_DESCRIPTIONS.md#unnecessary-stmt) | n/a | Suggests removing or simplifying unnecessary statements | no | no | | [`unreachable-code`](./RULES_DESCRIPTIONS.md#unreachable-code) | n/a | Warns on unreachable code | no | no | diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md index 1c3d0ca..6ee5706 100644 --- a/RULES_DESCRIPTIONS.md +++ b/RULES_DESCRIPTIONS.md @@ -87,7 +87,7 @@ List of all available rules. - [unexported-naming](#unexported-naming) - [unexported-return](#unexported-return) - [unhandled-error](#unhandled-error) -- [unnecessary-conditional](#unnecessary-conditional) +- [unnecessary-if](#unnecessary-if) - [unnecessary-format](#unnecessary-format) - [unnecessary-stmt](#unnecessary-stmt) - [unreachable-code](#unreachable-code) @@ -1500,14 +1500,14 @@ arguments = [ ] ``` -## unnecessary-conditional +## unnecessary-if _Description_: Detects unnecessary `if-else` statements that return or assign a boolean value based on a condition and suggests a simplified, direct return or assignment. The `if-else` block is redundant because the condition itself is already a boolean expression. The simplified version is immediately clearer, more idiomatic, and reduces cognitive load for the reader. -### Examples (unnecessary-conditional) +### Examples (unnecessary-if) ```go if y <= 0 { diff --git a/config/config.go b/config/config.go index 01b5ccc..ba97832 100644 --- a/config/config.go +++ b/config/config.go @@ -115,7 +115,7 @@ var allRules = append([]lint.Rule{ &rule.UnsecureURLSchemeRule{}, &rule.InefficientMapLookupRule{}, &rule.ForbiddenCallInWgGoRule{}, - &rule.UnnecessaryConditionalRule{}, + &rule.UnnecessaryIfRule{}, }, defaultRules...) // allFormatters is a list of all available formatters to output the linting results. diff --git a/rule/unnecessary_conditional.go b/rule/unnecessary_if.go similarity index 82% rename from rule/unnecessary_conditional.go rename to rule/unnecessary_if.go index 1dc86d2..f2f8174 100644 --- a/rule/unnecessary_conditional.go +++ b/rule/unnecessary_if.go @@ -9,18 +9,18 @@ import ( "github.com/mgechev/revive/lint" ) -// UnnecessaryConditionalRule warns on if...else statements with both branches being the same. -type UnnecessaryConditionalRule struct{} +// UnnecessaryIfRule warns on if...else statements that can be replaced by simpler expressions. +type UnnecessaryIfRule struct{} // Apply applies the rule to given file. -func (*UnnecessaryConditionalRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnnecessaryIfRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) } - w := &lintUnnecessaryConditional{onFailure: onFailure} + w := &lintUnnecessaryIf{onFailure: onFailure} for _, decl := range file.AST.Decls { fn, ok := decl.(*ast.FuncDecl) if !ok || fn.Body == nil { @@ -34,11 +34,11 @@ func (*UnnecessaryConditionalRule) Apply(file *lint.File, _ lint.Arguments) []li } // Name returns the rule name. -func (*UnnecessaryConditionalRule) Name() string { - return "unnecessary-conditional" +func (*UnnecessaryIfRule) Name() string { + return "unnecessary-if" } -type lintUnnecessaryConditional struct { +type lintUnnecessaryIf struct { onFailure func(lint.Failure) } @@ -49,7 +49,7 @@ type lintUnnecessaryConditional struct { // or // // if cond { = } else { = }. -func (w *lintUnnecessaryConditional) Visit(node ast.Node) ast.Visitor { +func (w *lintUnnecessaryIf) Visit(node ast.Node) ast.Visitor { ifStmt, ok := node.(*ast.IfStmt) if !ok { return w // not an if statement @@ -111,7 +111,7 @@ var relationalOppositeOf = map[token.Token]token.Token{ // condAsString yields the string representation of the given condition expression. // The method will try to minimize the negations in the resulting expression. -func (*lintUnnecessaryConditional) condAsString(cond ast.Expr, mustNegate bool) string { +func (*lintUnnecessaryIf) condAsString(cond ast.Expr, mustNegate bool) string { result := astutils.GoFmt(cond) if mustNegate { @@ -136,7 +136,7 @@ func (*lintUnnecessaryConditional) condAsString(cond ast.Expr, mustNegate bool) // iff both then and else statements are of the form = // If the replacement != "" then the second return value is the Boolean value // of in the then-side assignment, false otherwise. -func (w *lintUnnecessaryConditional) replacementForAssignmentStmt(thenStmt *ast.AssignStmt, elseStmts []ast.Stmt) (replacement string, thenBool bool) { +func (w *lintUnnecessaryIf) replacementForAssignmentStmt(thenStmt *ast.AssignStmt, elseStmts []ast.Stmt) (replacement string, thenBool bool) { thenBoolStr, ok := w.isSingleBooleanLiteral(thenStmt.Rhs) if !ok { return "", false @@ -166,7 +166,7 @@ func (w *lintUnnecessaryConditional) replacementForAssignmentStmt(thenStmt *ast. // iff both then and else statements are of the form return // If the replacement != "" then the second return value is the string representation // of in the then-side assignment, "" otherwise. -func (w *lintUnnecessaryConditional) replacementForReturnStmt(thenStmt *ast.ReturnStmt, elseStmts []ast.Stmt) (replacement string, thenBool bool) { +func (w *lintUnnecessaryIf) replacementForReturnStmt(thenStmt *ast.ReturnStmt, elseStmts []ast.Stmt) (replacement string, thenBool bool) { thenBoolStr, ok := w.isSingleBooleanLiteral(thenStmt.Results) if !ok { return "", false @@ -188,7 +188,7 @@ func (w *lintUnnecessaryConditional) replacementForReturnStmt(thenStmt *ast.Retu // isSingleBooleanLiteral returns the string representation of and true // if the given list of expressions has exactly one element and that element is a bool literal (true or false), // otherwise it returns "" and false. -func (*lintUnnecessaryConditional) isSingleBooleanLiteral(exprs []ast.Expr) (string, bool) { +func (*lintUnnecessaryIf) isSingleBooleanLiteral(exprs []ast.Expr) (string, bool) { if len(exprs) != 1 { return "", false } diff --git a/test/unnecessary_conditional_test.go b/test/unnecessary_conditional_test.go deleted file mode 100644 index 3c0fa56..0000000 --- a/test/unnecessary_conditional_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package test - -import ( - "testing" - - "github.com/mgechev/revive/lint" - "github.com/mgechev/revive/rule" -) - -func TestUnnecessaryConditional(t *testing.T) { - testRule(t, "unnecessary_conditional", &rule.UnnecessaryConditionalRule{}, &lint.RuleConfig{}) -} diff --git a/test/unnecessary_if_test.go b/test/unnecessary_if_test.go new file mode 100644 index 0000000..f6712b0 --- /dev/null +++ b/test/unnecessary_if_test.go @@ -0,0 +1,12 @@ +package test + +import ( + "testing" + + "github.com/mgechev/revive/lint" + "github.com/mgechev/revive/rule" +) + +func TestUnnecessaryIf(t *testing.T) { + testRule(t, "unnecessary_if", &rule.UnnecessaryIfRule{}, &lint.RuleConfig{}) +} diff --git a/testdata/unnecessary_conditional.go b/testdata/unnecessary_if.go similarity index 98% rename from testdata/unnecessary_conditional.go rename to testdata/unnecessary_if.go index 3b1ada7..4862449 100644 --- a/testdata/unnecessary_conditional.go +++ b/testdata/unnecessary_if.go @@ -1,6 +1,6 @@ package fixtures -func unnecessaryConditional() bool { +func unnecessaryIf() bool { var cond bool var id bool