Skip to content

New branch_exit_companions= (?) for unnecessary_nesting_linter() #2963

@MichaelChirico

Description

@MichaelChirico

Consider:

if (A) {
  stop('abc')
} else {
  warning('def')
}

if (B) {
  1 + 1
} else {
  warning("ghi")
}

if (C) {
  2 + 2
} else {
  myStop('fail')
}

We want the flexibility to allow warning() to be considered "parallel" to stop() in the A branch, but not to generate any lint in the B branch (which can't be unnested because there are no actual exit calls).

I think that requires a new parameter, I'm calling it branch_exit_companions= for now. This defines a set of calls that can "match" an exit call without themselves being considered one. Desired behavior:

linter A B C
unnecessary_nesting_linter() lint 'stop' no lint no lint
unnecessary_nesting_linter(branch_exit_calls = 'myStop') lint 'stop' no lint lint 'myStop'
unnecessary_nesting_linter(branch_exit_companions = 'warning') no lint no lint no lint

Current behavior:

lint(linters = list(
  default = unnecessary_nesting_linter(),
  warning_exit = unnecessary_nesting_linter(branch_exit_calls = 'warning')
), text = R"(
if (A) {
  stop('abc')
} else {
  warning('def')
}

if (B) {
  1 + 1
} else {
  warning("ghi")
}

if (C) {
  2 + 2
} else {
  myStop('fail')
}
)")

# <text>:2:1: warning: [default] Reduce the nesting of this if/else statement by unnesting the portion without an exit clause, i.e., stop().
# if (A) {
# ^~~~~~~~
# <text>:8:1: warning: [warning_exit] Reduce the nesting of this if/else statement by unnesting the portion without an exit clause, i.e., warning().
# if (B) {
# ^~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions