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

🐛 noUselessElse has a unhelpful/unclear description #920

Closed
1 task done
net-tech opened this issue Nov 27, 2023 · 1 comment · Fixed by #943
Closed
1 task done

🐛 noUselessElse has a unhelpful/unclear description #920

net-tech opened this issue Nov 27, 2023 · 1 comment · Fixed by #943
Assignees

Comments

@net-tech
Copy link

Environment information

CLI:
  Version:                      1.4.0
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.4.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         unset

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 true

Workspace:
  Open Documents:               0

What happened?

Currently, the description for the noUselessElse rule reads

Disallow else block when the if block breaks early.
If an if block breaks early using a breaking statement (return, break, continue, or throw), then the else block becomes useless. Its contents can be placed outside of the block.

which is not very clear in explaining why the else block becomes useless. This could result in people who cannot interpret the reasoning of why the else block becomes useless to disable the rule.

Expected result

A better alternative could be something like this

If an if block breaks early using a breaking statement (return, break, continue, or throw), the else block becomes unnecessary. This is because the contents of the else block will never be executed in conjunction with the if block, as the breaking statement ensures the control flow exits the if block immediately. Therefore, the else block is redundant, and its contents can be more effectively placed outside of the block, enhancing clarity and efficiency in the code structure.

Let me know if I should modify anything or make a PR for it.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@nstepien
Copy link
Contributor

Therefore, the else block is redundant, and its contents can be more effectively placed outside of the block, enhancing clarity and efficiency in the code structure.

if (a) {
  return ...;
} else if (b) {
  return ...;
}

Here the else is redundant, but the condition is not, so the "block" should remain:

if (a) {
  return ...;
}

if (b) {
  return ...;
}

I think rather than talking about "blocks", the description should talk about the else clause itself.

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 a pull request may close this issue.

3 participants