You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We had an issue in production today that was caused by an autocorrect on a Lint/EnsureReturn case.
Basically, here's what it looked like:
moduleMyModulemodule_functiondefcall(some_arg:)# Lots of code here...# Here we have code that potentially defines a variable `some_var`, # depending on where it is located.casesome_stringwhen'string_one'ifsome_conditionsome_var=function_Aelse# More code with no reference to `some_var`endwhen'string_two'some_var=function_BendrescueExceptionType1=>e# ...rescueExceptionType2=>e# ... 2 more of theseensurereturnsome_varifsome_var# ...# Here is more code that returns something else if `some_var` is not defined.endend
What Rubocop did was change return some_var if some_var to just some_var, which did not return, went unnoticed by us, and caused an issue in production.
While I now understand that using return in an ensure block is not great practice and is considered code smell (did not know this until researching this today), is it possible to consider this Cop as not necessarily safe for autocorrect?
… multi-value returns
It is not possible to autocorrect a `return` in an `ensure`, as not only does it
modify control flow without the ensure block, it also overrides the normal return
value
This reverts rubocop#7934
Right, our auto-correction was wrong and I've had to remove it. There's no valid correction that can be done. The example you provided is a good example I believe;I can't provide an auto-correction myself and I suspect it involves a bit of refactoring to be written correctly. The cop is also right that some other exceptions might be swallowed without you knowing about it so a refactoring is indeed desirable.
I'm sorry you had issues in production. Thanks for raising the issue and providing a detailed example 👍
marcandre
added a commit
to marcandre/rubocop
that referenced
this issue
Jul 23, 2020
We had an issue in production today that was caused by an autocorrect on a
Lint/EnsureReturn
case.Basically, here's what it looked like:
What Rubocop did was change
return some_var if some_var
to justsome_var
, which did not return, went unnoticed by us, and caused an issue in production.While I now understand that using
return
in anensure
block is not great practice and is considered code smell (did not know this until researching this today), is it possible to consider this Cop as not necessarily safe for autocorrect?Thanks.
RuboCop version
$ bundle exec rubocop -V 0.85.0 (using Parser 2.7.1.3, rubocop-ast 0.0.3, running on ruby 2.5.7 x86_64-darwin19)
The text was updated successfully, but these errors were encountered: