Skip to content

Linter Rule: Disallow Ruby literals in ERB without output

Rule: erb-no-unused-literals

Description

Disallow Ruby literal values inside ERB tags that do not produce output or side effects. Writing literals like <% "string" %> or <% 123 %> has no effect. The value is evaluated and discarded, making the line functionally useless.

This rule detects and warns about ERB tags containing:

  • Literal strings
  • Numeric values (integers, floats, rationals, imaginary)
  • true and false
  • nil
  • Symbols
  • Arrays, hashes, and ranges
  • Regular expressions

Rationale

These expressions are evaluated but unused, they don't produce output (not <%= ... %>), and they don't perform side effects (no assignments, method calls, or control flow). They're likely accidental leftovers, debugging artifacts, or misunderstandings of ERB syntax.

Examples

✅ Good

erb
<% if logged_in? %>
  <p>Welcome!</p>
<% end %>
erb
<%= "Hello, #{user.name}" %>

🚫 Bad

erb
<% "Logged in" %>
Avoid using silent ERB tags for literals. `"Logged in"` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% 42 %>
Avoid using silent ERB tags for literals. `42` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% true %>
Avoid using silent ERB tags for literals. `true` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% false %>
Avoid using silent ERB tags for literals. `false` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% nil %>
Avoid using silent ERB tags for literals. `nil` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% [:foo, :bar] %>
Avoid using silent ERB tags for literals. `[:foo, :bar]` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% /pattern/ %>
Avoid using silent ERB tags for literals. `/pattern/` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% { key: "value" } %>
Avoid using silent ERB tags for literals. `{ key: "value" }` is evaluated but never used or output. (erb-no-unused-literals)
erb
<% :symbol %>
Avoid using silent ERB tags for literals. `:symbol` is evaluated but never used or output. (erb-no-unused-literals)

References

-

Released under the MIT License.