Linter checks for the switch(rand(L, H)) pattern#302
Merged
SpaceManiac merged 4 commits intoSpaceManiac:masterfrom Feb 8, 2022
Merged
Linter checks for the switch(rand(L, H)) pattern#302SpaceManiac merged 4 commits intoSpaceManiac:masterfrom
SpaceManiac merged 4 commits intoSpaceManiac:masterfrom
Conversation
if the range is not fully covered by cases a warning is thrown if a case is outside of the rand range a warning is thrown
Contributor
Author
|
Warnings emitted on tgstation master for reference: |
sohpeach
reviewed
Feb 7, 2022
I'm dumb
Contributor
|
Can you make it possible to disable warning If I replace |
Owner
|
I recommend leaving it blank with an explanatory comment: switch(rand(1, 20))
if(1 to 9)
bad_effect()
if(10)
// Neutral roll, do nothing.
if(11 to 20)
good_effect()This also works fine with switch(rand(1, 20))
if(1 to 17)
get_hurt()
if(18 to 20)
; // Defense succeeded, no damage. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A pattern such as
is not uncommon in SS13 code. Sadly, it is also not uncommon for people to add new cases to a longer switch of this form and forget to change the
randarguments at the top. This PR adds a linter warning for when therandrange is not fully covered by the cases and for when a case lies completely outside of therandrange.This currently generates 7 warnings on tgstation master. Sadly, it is (at least to me) not quite clear if the omissions of a part of the
randrange are intentional there or not. However, I believe that even if they are intentional it is worth adding the missing part of the range as a case with empty body explicitly to make the intent clear to future developers.As a less related (but perhaps more important) addition this PR now also adds a warning for switch branches of the form
if(X || Y)which is pretty much always a mistake and should instead beif(X, Y). This lint generates 5 warnings on current tg code, seemingly all being actual mistakes.