New mismatched indentations warnings?
I recently got a failure in my test suite because ruby head warns of indentation it considers mismatched:
$ ruby -w -e " case when :foo end " -e:3: warning: mismatched indentations at 'when' with 'case' at 2
I hope this is not intentional and will be fixed.
Updated by shevegen (Robert A. Heiler) about 2 years ago
I think in the long run it would be nice if ruby hackers have more
control over non-error messages (e. g. warnings).
Some may prefer ruby providing cues for a more consistent code
layout but others may not necessarily care that much. In both
situations it would be nice to be able to decide on the verbosity
level of warnings.
Updated by marcandre (Marc-Andre Lafortune) about 2 years ago
I strongly believe that it is not Ruby's parser job to warn us about styling, especially if there's no strong reason to suspect that it's a programmer error.
I realize rubocop is not official or part of MRI, but it has the required qualities to do this:
1) requires opt-in (i.e. users wanting to enforce a particular style can use rubocop; others don't need to)
2) configurable (i.e. rules can be turned off or tweaked)
3) exception can be dealt with with comments in the code (e.g. # rubocop:disable Some/Rule)
4) warning/errors are generated when desired, not all the time (e.g. during tests/manually, but not when actually running the code)
rubocop can even autocorrect according to these desired specs. Ruby MRI has none of these features!
Note that rubocop already supports
Layout/CaseIndentation, can issue warning, error, or ignore it. It supports two different alignment styles, and supports any desired indent.
In short: warnings should be kept to a strict minimum in MRI, and enforcing a style for
case/when is a terrible use of a warning.
Would you consider reverting this, or asking Matz?
Updated by zverok (Victor Shepelev) about 2 years ago
I agree with @marcandre. I used to prefer "indented when" style once, and still think it is pretty acceptable.
When Ruby has introduced interpreter warnings about mismatched indentation, I thought it is a good thing, allowing to catch obvious typos like this:
def foo bar.each do end # forgot to close do/end of "each", hard to spot after a large methods #... # Without that warning, the interpreter error about missing "end" at the very end of the file, # far from the actual point of error
But for being useful this way, the interpreter should report only absolutely obvious and unambiguous typos, not "non-preferred style". Ruby is too mature to introduce hardcoded style preferences, I believe.