DEV Community

Augusts Bautra
Augusts Bautra

Posted on

False positives of Lint/Void in assignments

Today I was working on autofixing rubocop offences and Lint/Void gave me a nasty surprise. Supposedly in Ruby the return value of assignment methods like def field=(value) will be special in that it's always the value passed, irrespective of what the last expression in the method body was. While true, this rule has an unusual exception - being called via super. In that case the last expression is returned, confusoring everyone.

Try this code and see:

class Parent
  def value=(v)
    @value = v
    :p # should never be returned, but using super in an inheriting class does get to it.
  end

  def value
    @value
  end
end

class Child < Parent
  def value=(v)
    parent_value = super
    parent_explicit_value = super(v)
    puts "Parent's value assignment returns #{parent_value}, whereas passing arg explicitly returns #{parent_explicit_value}"

    @value = v*10
    :c
  end
end

Child.new.value = 1
Parent's value assignment returns p, whereas passing arg explicitly returns p
#=> 1
Enter fullscreen mode Exit fullscreen mode

Not sure what the takeaway here is. The cop is useful because it warns about code that will have no effect, but I guess it has a false positive case in assignment methods.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay