Skip to content

return_linter() for implicit returns has a false positive on nested early returns #2354

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

Closed
MichaelChirico opened this issue Nov 27, 2023 · 2 comments · Fixed by #2362
Closed
Labels
false-positive code that shouldn't lint, but does

Comments

@MichaelChirico
Copy link
Collaborator

          Corrected this test, and added the incorrect test as a new regression test.

That said, the new fix is more expedient than perfect -- it'll throw an incorrect lint here:

foo <- function() {
  if (TRUE) {
    if (TRUE) {
      return(1)
    }
    2
  } else {
    3
  }

(the return() is a valid early return within that branch).

But as this issue is not relevant to the implicit else returns feature, I'm pushing to follow-up.

Originally posted by @MichaelChirico in #2321 (comment)

@MichaelChirico
Copy link
Collaborator Author

Test case for regression:

test_that("explicit returns in control flow are linted", {
  lint_msg <- rex::rex("Use implicit return behavior")

  expect_lint(
    trim_some("
      foo <- function(bar) {
        if (TRUE) {
          return(bar)
        } else {
          return(NULL)
        }
      }
    "),
    list(lint_msg, lint_msg),
    return_linter()
  )
})

@MichaelChirico
Copy link
Collaborator Author

Also ensure that e.g. R/any_duplicated_linter.R does not lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive code that shouldn't lint, but does
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant