Skip to content

SI-8731 warning if @switch is ignored #4016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2014
Merged

Conversation

lrytz
Copy link
Member

@lrytz lrytz commented Sep 30, 2014

For matches with two or fewer cases, @switch is ignored. This should
not happen silently.

@scala-jenkins scala-jenkins added this to the 2.11.3 milestone Sep 30, 2014
@lrytz lrytz changed the title SI-8087 warning if @switch is ignored SI-8731 warning if @switch is ignored Sep 30, 2014
@lrytz
Copy link
Member Author

lrytz commented Sep 30, 2014

Review by @gkossakowski

}
val requireSwitch = hasSwitchAnnotation && lengthMax3(casesNoSubstOnly) > 2
if (hasSwitchAnnotation && !requireSwitch)
reporter.warning(scrut.pos, "ignoring @switch, warnings are only issued for matches with more than two cases")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be for matches with two or less cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but maybe i have to come up with a clearer wording.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe :)
i understood this as if you have a pm with 1 | 2 cases and switch annotation, than this warning is issued, as for 2 or less cases switch is ignored.

@lrytz
Copy link
Member Author

lrytz commented Oct 6, 2014

cc @adriaanm

// matches with two or fewer cases need not apply for switchiness (if-then-else will do)
val requireSwitch = treeInfo.isSwitchAnnotation(tpt.tpe) && casesNoSubstOnly.lengthCompare(2) > 0
// `case 1 | 2` is considered as two cases.
def lengthMax3[T](l: List[T]): Int = l match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wrap the lengthMax3(casesNoSubstOnly) > 2 in def exceedsTwoCasesOrAlts (to explain what kind of 'length' we're measuring), which contains lengthMax3 as a local method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, thanks. so in general you agree with the additional warning?

@gkossakowski
Copy link
Contributor

I think the warning makes sense to introduce. I agree with @adriaanm's suggestion to clarify the condition as expressed in code.

@lrytz
Copy link
Member Author

lrytz commented Oct 6, 2014

OK. I'll get it fixed up later today.

For matches with two or fewer cases, @switch is ignored. This should
not happen silently.
@lrytz
Copy link
Member Author

lrytz commented Oct 6, 2014

created a method exceedsTwoCasesOrAlts and also changed the warning message. now it says matches with two cases or fewer are emitted using if-then-else instead of switch.

@adriaanm
Copy link
Contributor

adriaanm commented Oct 7, 2014

LGTM

gkossakowski added a commit that referenced this pull request Oct 7, 2014
SI-8731 warning if @switch is ignored
@gkossakowski gkossakowski merged commit d4601ba into scala:2.11.x Oct 7, 2014
@lrytz lrytz deleted the t8731 branch October 13, 2014 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants