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

Suggest replacing a mapping with iterating over .values() or a constant Map. #531

Open
Luro02 opened this issue May 13, 2024 · 0 comments
Open
Labels
D-hard enhancement New feature or request low-priority new-lint A new lint.

Comments

@Luro02
Copy link
Collaborator

Luro02 commented May 13, 2024

What it does

It makes the code smarter. Note: This is difficult to notice, wouldn't make sense to subtract for this.

Lint Name

No response

Category

complexity

Example

return switch (string) {
    case "A" -> MyEnum.A,
    case "B" -> MyEnum.B,
    case "C" -> MyEnum.C,
    case "D" -> MyEnum.D,
    default -> throw new IllegalArgumentException("invalid input string: " + string);
};

Could be written as:

for (var value : MyEnum.values()) {
  if (string.equals(value.toString())) {
    return value;
  }
}

throw new IllegalArgumentException("invalid input string: " + string);
@Luro02 Luro02 added enhancement New feature or request new-lint A new lint. D-hard low-priority labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-hard enhancement New feature or request low-priority new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant