Rubocop: A semicolon in the same line as the method signature is allowed

Created on 8 Apr 2017  路  5Comments  路  Source: rubocop-hq/rubocop

A semicolon in the same line as the method signature isn鈥檛 violating any existing cop.

Expected behavior

I expect RuboCop (Style/Semicolon) to complain about having a semicolon after def foo.

Actual behavior

No offenses detected.

Steps to reproduce the problem

Run rubocop on this file:

# frozen_string_literal: true

def foo; nil
end

RuboCop version

$ rubocop -V
0.48.1 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-darwin15)
bug

All 5 comments

Likely a bug in the detection of one-line methods.

Hm. I don't particularly like the suggested behaviour. I am more inclined to consider this a case of a misnamed cop. Perhaps it should be RedundantSemicolon? Disallowing semicolons completely contradicts the Style Guide on things like empty classes, e.g. class Foo; end, and opens a can of worms with semantic changes.

Perhaps SingleLineMethod can be extended to cover the case above? Although technically it's also not a single line method. More like a 1.5 line method. 馃槄

When opening the issue, I wasn鈥檛 sure whether to blame Semicolon or SingleLineMethod. You could argue that SingleLineMethod should ensure a newline before end and a newline after the method signature.

@Drenmi The purpose of this cop was to find multiple expressions on the same line, that should actually be on separate lines. Many early day cops have horrible names. I didn't really think much about them back then.

And, of course, like most very old cops - this one has poor documentation and no code examples. 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikegee picture mikegee  路  3Comments

printercu picture printercu  路  3Comments

mlammers picture mlammers  路  3Comments

bquorning picture bquorning  路  3Comments

david942j picture david942j  路  3Comments