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 SomeListing.of(1, 2, 6, ...).contains(c) in place of c == 1 || c == 2 || c == 6 .... #555

Open
Luro02 opened this issue Jun 30, 2024 · 0 comments
Labels
D-easy Easy to implement. enhancement New feature or request low-priority new-lint A new lint.
Milestone

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Jun 30, 2024

What it does

Simplifies complex boolean expressions into shorter ones that are easier to read.

Some things to be careful of:

  • There should be at least X elements in the resulting list (maybe X=3?)
  • There should be at least one java.util import in the model (so list can be used)
  • Instead of List, one could use Set instead
  • Ignore expressions comparing with null like a == null || a == 1 || a == 2

Lint Name

USE_LISTING_CONTAINS

Category

complexity

Example

class Test {
    boolean isValid(char ch) {
        return ch == 'N' || ch == 'S' || ch == 'O' || ch == 'W' || ch == 'n' || ch == 's' || ch == 'o' || ch == 'w';
    }
}

Could be written as:

class Test {
    boolean isValid(char ch) {
        return List.of('N', 'S', 'O', 'W', 'n', 's', 'o', 'w').contains(ch);
    }
}
@Luro02 Luro02 added enhancement New feature or request D-easy Easy to implement. new-lint A new lint. low-priority labels Jun 30, 2024
@Luro02 Luro02 modified the milestones: v0.6.0, v0.7.0 Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-easy Easy to implement. enhancement New feature or request low-priority new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant