lib/rubocop/cop/lint/useless_setter_call.rb in rubocop-1.1.0 vs lib/rubocop/cop/lint/useless_setter_call.rb in rubocop-1.2.0

- old
+ new

@@ -3,10 +3,11 @@ module RuboCop module Cop module Lint # This cop checks for setter call to local variable as the final # expression of a function definition. + # Its auto-correction is marked as unsafe because return value will be changed. # # NOTE: There are edge cases in which the local variable references a # value that is also accessible outside the local scope. This is not # detected by the cop, and it can yield a false positive. # @@ -27,10 +28,12 @@ # x = Something.new # x.attr = 5 # x # end class UselessSetterCall < Base + extend AutoCorrector + MSG = 'Useless setter call to local variable `%<variable>s`.' ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].freeze def on_def(node) return unless node.body @@ -43,10 +46,12 @@ variable_name, = *receiver return unless tracker.contain_local_object?(variable_name) loc_name = receiver.loc.name - add_offense(loc_name, message: format(MSG, variable: loc_name.source)) + add_offense(loc_name, message: format(MSG, variable: loc_name.source)) do |corrector| + corrector.insert_after(last_expr, "\n#{indent(last_expr)}#{loc_name.source}") + end end alias on_defs on_def private