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

New parameters for if_switch_linter() to optionally skip lints on "complex" if/else usage #2413

Merged
merged 23 commits into from
Dec 28, 2023

Conversation

MichaelChirico
Copy link
Collaborator

Closes #2322

@codecov-commenter
Copy link

codecov-commenter commented Dec 11, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e0a1d24) 98.55% compared to head (e7e1227) 98.55%.

❗ Current head e7e1227 differs from pull request most recent head dcb2c74. Consider uploading reports for the commit dcb2c74 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2413   +/-   ##
=======================================
  Coverage   98.55%   98.55%           
=======================================
  Files         126      126           
  Lines        5735     5762   +27     
=======================================
+ Hits         5652     5679   +27     
  Misses         83       83           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AshesITR
Copy link
Collaborator

Should this lint switch() calls that violate the max_* bounds, too?

@MichaelChirico
Copy link
Collaborator Author

Should this lint switch() calls that violate the max_* bounds, too?

Interesting... possibly? I would wait for user request though, WDYT?

@AshesITR
Copy link
Collaborator

I feel the linter should enforce some consistency.
The reason why the lints would be suppressed are also valid reasons why the switch() should be broken up into an if () ... else if () ... chain.

@MichaelChirico
Copy link
Collaborator Author

MichaelChirico commented Dec 15, 2023

OK, let me double-check how this should work as I'm second-guessing myself trying to wrap my head around it.

# always lint if/else if/else if usage
# never lint switch()
if_switch_linter(max_branch_lines = 0, max_branch_expr = 0)

# only lint if/else if/else if of "low" complexity
# also lint switch() of "high" complexity
if_switch_linter(max_branch_lines = 2, max_branch_expr = 3)

i.e. these parameters require using if/else if/.../else for high-complexity switch-y cases, and require switch() for low-complexity switch-y cases. Is that right?

@AshesITR

This comment was marked as resolved.

@MichaelChirico

This comment was marked as outdated.

@AshesITR
Copy link
Collaborator

Looks good, I would make some of the tests "unbalanced" in terms of lines/exprs per branch to ensure the largest/most complex branch dictates behavior.

AFAICT there is no such test for switch().
Also, is there a metadata test for switch()?

@MichaelChirico
Copy link
Collaborator Author

@AshesITR
Copy link
Collaborator

AFAICT there is no such test for switch().

IIUC those are https://github.com/r-lib/lintr/pull/2413/files#diff-5a5a1de0b476f8f7c4ffcfbf4f4307c8cdfbbfa85367eccf61060e48e433c9e5R337-R340 and https://github.com/r-lib/lintr/pull/2413/files#diff-5a5a1de0b476f8f7c4ffcfbf4f4307c8cdfbbfa85367eccf61060e48e433c9e5R361-R364

In those tests, all switch branches have the same complexity (loc & expr count). That's what I meant by "balanced".

I'd add something like

switch(x,
  a = 1
  b = {
    2
    3
    4; 5; 6
  },
  c = {
    7
  }
)

to the mix

Also, is there a metadata test for switch()?

No, adding now

@MichaelChirico
Copy link
Collaborator Author

Thanks for clarifying, added some tests unbalanced across branches

R/if_switch_linter.R Outdated Show resolved Hide resolved
@AshesITR AshesITR merged commit 11eae86 into main Dec 28, 2023
20 checks passed
@AshesITR AshesITR deleted the if-switch-complexity branch December 28, 2023 17:28
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.

Parameterize if_switch_linter() not to require switch() for "highly-complicated" clauses
3 participants