File tree Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change
1
+ * [ #1228 ] ( https://github.com/rubocop/rubocop-rails/issues/1228 ) : Enhance ` Rails/SaveBang ` to properly handle instance variables. ([ @ydakuka ] [ ] )
Original file line number Diff line number Diff line change @@ -328,8 +328,9 @@ def explicit_return?(node)
328
328
end
329
329
330
330
def return_value_assigned? ( node )
331
- assignment = assignable_node ( node ) . parent
332
- assignment &.lvasgn_type?
331
+ return false unless ( assignment = assignable_node ( node ) . parent )
332
+
333
+ assignment . assignment?
333
334
end
334
335
335
336
def persist_method? ( node , methods = RESTRICT_ON_SEND )
Original file line number Diff line number Diff line change @@ -668,6 +668,56 @@ def whatever
668
668
RUBY
669
669
end
670
670
671
+ it "does not register an offense when using persisted? after #{ method } to a local variable" do
672
+ expect_no_offenses ( <<~RUBY )
673
+ user = User.#{ method }
674
+
675
+ if user.persisted?
676
+ foo
677
+ end
678
+ RUBY
679
+ end
680
+
681
+ it "does not register an offense when using persisted? after #{ method } to an instance variable" do
682
+ expect_no_offenses ( <<~RUBY )
683
+ @user = User.#{ method }
684
+
685
+ if @user.persisted?
686
+ foo
687
+ end
688
+ RUBY
689
+ end
690
+
691
+ it "does not register an offense when using persisted? after #{ method } to a global variable" do
692
+ expect_no_offenses ( <<~RUBY )
693
+ $user = User.#{ method }
694
+
695
+ if $user.persisted?
696
+ foo
697
+ end
698
+ RUBY
699
+ end
700
+
701
+ it "does not register an offense when using persisted? after #{ method } for multiple assignments" do
702
+ expect_no_offenses ( <<~RUBY )
703
+ a, b = User.#{ method } , User.new
704
+
705
+ if a.persisted?
706
+ foo
707
+ end
708
+ RUBY
709
+ end
710
+
711
+ it "does not register an offense when using persisted? after #{ method } for conditional assignments" do
712
+ expect_no_offenses ( <<~RUBY )
713
+ user ||= User.#{ method }
714
+
715
+ if user.persisted?
716
+ foo
717
+ end
718
+ RUBY
719
+ end
720
+
671
721
it "when using #{ method } with `||`" do
672
722
expect_no_offenses ( <<~RUBY )
673
723
def find_or_create(**opts)
You can’t perform that action at this time.
0 commit comments