1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00

rename rule as unnecessary-if

This commit is contained in:
chavacava
2025-09-22 12:39:22 +02:00
parent 7cdb6cfc2b
commit bc2aed1fd0
7 changed files with 30 additions and 30 deletions

View File

@@ -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 |

View File

@@ -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 {

View File

@@ -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.

View File

@@ -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 { <idX> = <bool literal> } else { <idX> = <bool literal> }.
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 <idX> = <bool literal>
// If the replacement != "" then the second return value is the Boolean value
// of <bool literal> 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 <bool literal>
// If the replacement != "" then the second return value is the string representation
// of <bool literal> 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 <bool literal> 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
}

View File

@@ -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{})
}

View File

@@ -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{})
}

View File

@@ -1,6 +1,6 @@
package fixtures
func unnecessaryConditional() bool {
func unnecessaryIf() bool {
var cond bool
var id bool