Skip to content
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

Linter checks for the switch(rand(L, H)) pattern #302

Merged
merged 4 commits into from
Feb 8, 2022

Conversation

pali6
Copy link
Contributor

@pali6 pali6 commented Feb 7, 2022

A pattern such as

switch(rand(1, 3))
  if(1)
    foo()
  if(2)
    bar()
  if(3)
    baz()

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 rand arguments at the top. This PR adds a linter warning for when the rand range is not fully covered by the cases and for when a case lies completely outside of the rand range.

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 rand range 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 be if(X, Y). This lint generates 5 warnings on current tg code, seemingly all being actual mistakes.

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
@pali6
Copy link
Contributor Author

pali6 commented Feb 7, 2022

Warnings emitted on tgstation master for reference:

code/modules/mob/mob_helpers.dm, line 103, column 3:
warning: Switch branches on rand() with range 1 to 20 but no case branch triggers for 2

code/modules/mob/mob_helpers.dm, line 142, column 3:
warning: Switch branches on rand() with range 1 to 15 but no case branch triggers for 6

code/datums/brain_damage/severe.dm, line 182, column 2:
warning: Switch branches on rand() with range 1 to 6 but no case branch triggers for 6

code/datums/components/singularity.dm, line 300, column 3:
warning: Elements in a switch-case branch separated by ||, this is likely in error and should be replaced by a comma

code/datums/components/singularity.dm, line 303, column 2:
warning: Elements in a switch-case branch separated by ||, this is likely in error and should be replaced by a comma

code/modules/surgery/advanced/lobotomy.dm, line 54, column 2:
warning: Switch branches on rand() with range 1 to 4 but no case branch triggers for 4

code/modules/hydroponics/hydroponics_chemreact.dm, line 15, column 2:
warning: Switch branches on rand() with range 0 to 100 but no case branch triggers for 0

code/game/machinery/requests_console.dm, line 319, column 3:
warning: Elements in a switch-case branch separated by ||, this is likely in error and should be replaced by a comma

code/modules/vending/_vending.dm, line 500, column 4:
warning: Switch branches on rand() with range 1 to 100 but no case branch triggers for 26

code/modules/power/singularity/singularity.dm, line 341, column 3:
warning: Elements in a switch-case branch separated by ||, this is likely in error and should be replaced by a comma

code/modules/power/singularity/singularity.dm, line 344, column 2:
warning: Elements in a switch-case branch separated by ||, this is likely in error and should be replaced by a comma

code/modules/ruins/lavalandruin_code/elephantgraveyard.dm, line 137, column 2:
warning: Switch branches on rand() with range 1 to 8 but no case branch triggers for 8

Copy link
Owner

@SpaceManiac SpaceManiac left a comment

Choose a reason for hiding this comment

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

Extremely good, thank you

@SpaceManiac SpaceManiac merged commit 0a121f6 into SpaceManiac:master Feb 8, 2022
@pali6 pali6 deleted the switch-rand-range-checks branch February 8, 2022 01:11
@KIBORG04
Copy link
Contributor

KIBORG04 commented Feb 9, 2022

Can you make it possible to disable warning warning Switch branches to rand() with a range from 1 to 6, but without branch triggers for 6 in configuration ? It is not always possible to solve this by inserting the missing IFs. Example:

/proc/some_func()
    for(var/i in 1 to 10)
        switch(rand(1, 20)
            if(1 to 2)
                ..code..
            if(3 to 15)
                ..code..
            if(16 to 20)
                ???
        do_post_switch()

If I replace ??? to break than get the wrong result, to continiue the same, to return the same. And there are no other options.

@SpaceManiac
Copy link
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 else in the final position, but for some reason not on the final if, but you can use a lone semicolon in that case:

switch(rand(1, 20))
    if(1 to 17)
        get_hurt()
    if(18 to 20)
        ; // Defense succeeded, no damage.

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.

4 participants