-
Notifications
You must be signed in to change notification settings - Fork 282
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
indent-error-flow incorrectly identifying conditional attribution as redundant #564
Comments
Hi @sonalys, thanks for filling the issue. For the example code you provided,
As the message says, if necessary, you'll need to move the variable declaration to its own line. Something like: a, b := 1, 2
if a != 1 {
return a
}
return b The current implementation of the rule in One common critic to the refactoring proposed by {
a, b := 1, 2
if a != 1 {
return a
}
return b
} Note: avoiding warnings in cases like those of your example will require to add a data flow analysis ("any variable defined in the |
yes, improving the message would be great, because it doesn't seem to justify the behaviour it wants with the actual message err := returnsError()
if err != nil {
return err
}
myNewVar, err = myFuncThatReturnsVarAndError()
if err != nil {
return err
} because we will need to either declare myNewVar outside the if-statement, or make the err variable to be scoped. if myNewVar, err := myFuncThatReturnsVarAndError(); err != nil {
return err
} else {
useMyNewVarIntoSomethingBecauseItsStillInScope(myNewVar)
} it will not work I think pre-declaring this stuff outside if-statements can make the code more difficult to read and bigger, an example of the first case var err error
err = returnsError()
if err != nil {
return err
}
var myNewVar MyType
myNewVar, err = myFuncThatReturnsVarAndError()
if err != nil {
...
} when it could be if err := returnsError(); err != nil {
return err
}
if myNewVar, err := myFuncThatReturnsVarAndError(); err != nil {
return err
} else {
useMyNewVarIntoSomethingBecauseItsStillInScope(myNewVar)
} |
As I've said, for the example you provided,
If in your case is not generating such a message, please provide the actual code you are linting. There are many subjects that are a matter of personal taste, thus which is more easy to read if myNewVar, err := myFuncThatReturnsVarAndError(); err != nil {
return err
} else {
useMyNewVarIntoSomethingBecauseItsStillInScope(myNewVar)
} or myNewVar, err := myFuncThatReturnsVarAndError()
if err != nil {
return err
} else {
useMyNewVarIntoSomethingBecauseItsStillInScope(myNewVar)
} It is not something we will discuss :) |
Not a bug. |
Describe the bug
When we declare variables inside if statements, and returns inside the if block, on the else statement, we can no longer use other declared variables because revive detects indent-error-flow redundancy.
To Reproduce
Steps to reproduce the behavior:
go get -u github.com/mgechev/revive
Expected behavior
This should not be redundant, because we are only declaring those variables inside the if statement, and it should not be possible to access b without the else statement.
The text was updated successfully, but these errors were encountered: