foobar.rb:156: warning: duplicated when clause is ignored
Is there any way for Ruby to tell me which are duplicated though?
Not that I know of, rb_compile_warning() is invoked from compile.c with
the line number on which the ‘case’ statement is.
I agree that it would be a nice feature.
The problem, though, seems to be elsewhere. How many ‘when’ clauses are
there in your script? Is it so high to make a manual check too
difficult?
If so, that might be a code smell, meaning that you should refactor your
code.
From a couple of tests I made, I observed something interesting.
This:
x = 1
case x
when 0
when 1
when 1
end
will cause the warning to appear.
This, on the other hand:
x = 1
case x
when 0
when 1
when 1 + 0
when 1
end
is enough to fool iseq_set_sequence(), making it print out no warnings
at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).
is enough to fool iseq_set_sequence(), making it print out no warnings
at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).
I wonder if I should file a ticket.
It isn’t necessarily duplicated. You might have redefined Numeric#+ to
have a side-effect. It would be a silly thing to do, but Ruby doesn’t
know you’ve not done that.
at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).
I wonder if I should file a ticket.
It isn’t necessarily duplicated. You might have redefined Numeric#+ to
have a side-effect. It would be a silly thing to do, but Ruby doesn’t
know you’ve not done that.
True, but there are still two identical “when 1”.
My point is that adding a “when 1 + 0” between them makes the warning go
away when it shouldn’t.
is enough to fool iseq_set_sequence(), making it print out no warnings
My point is that adding a “when 1 + 0” between them makes the warning go away
when it shouldn’t.
Or am I missing something?
I don’t think you’re missing anything. Considering the following causes
the warning as well:
x = 1
case x
when 1
when 0
when 1
end
I think you came up with a bug. Could you file a ticket on ruby-core’s
rubymine tracker?