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

Enhancement of unreachable_code_linter to Detect Unreachable Code in Loops #2105

Closed
MEO265 opened this issue Sep 4, 2023 · 2 comments · Fixed by #2141
Closed

Enhancement of unreachable_code_linter to Detect Unreachable Code in Loops #2105

MEO265 opened this issue Sep 4, 2023 · 2 comments · Fixed by #2141
Labels
feature a feature request or enhancement

Comments

@MEO265
Copy link
Contributor

MEO265 commented Sep 4, 2023

The unreachable_code_linter should also mark code inside if, else, for, and while loops as unreachable, especially when a return or stop function is called.

The following code currently generates only one annotation:

x <- function() { 
  if(TRUE) { 
    stop('This is a test') 
    5+5 
  } 
  return(5L)
  4+4 
} 

And this one generates none:

x <- function() { 
  if(TRUE) { 
    stop('This is a test') 
    5+5 
  } 
  4+4 
} 
@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Sep 4, 2023

I think this is a duplicate of #1428, though I don't think if(TRUE) constitutes unreachable code. I think it's more relevant for unnecessary nesting (which we have a linter for, though not in {lintr}) yet IIRC

oh, I misunderstood. yes, I agree it's unreachable.

@MEO265
Copy link
Contributor Author

MEO265 commented Sep 9, 2023

In loops, attention should also be paid to next and break.

@MichaelChirico I'm not sure if that's a part of what you might have meant in your TODO as well.

# TODO(michaelchirico): the logic could be extended to terminal if statements
# or control flows (for/while). There shouldn't really be such a thing as
# a terminal for/while (owing to ExplicitReturnLinter forcing these to
# be followed by return(invisible()) or similar), but could be included to
# catch comments for completeness / robustness as a standalone function.
# Terminal if statements are a bit messy, but would have some payoff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants