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

Detect code that can be replaced by Objects.requireNonNullElse/Objects.requireNonNullElseGet #612

Open
Luro02 opened this issue Oct 8, 2024 · 0 comments
Labels
api lints that check for code that reimplements parts of the api enhancement New feature or request low-priority new-lint A new lint.

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Oct 8, 2024

What it does

These two methods could be used to simplify some code:

where one checks if a value is null, then returns/breaks/assigns a default value or calls some code or otherwise uses the non-null value.

The amount of code that triggers this lint, is likely minimal, especially, when Objects.requireNonNullElseGet cannot be suggested, which makes this a low-priority issue.

Lint Name

USE_OBJECTS_REQUIRE_NON_NULL

Category

api

Example

Character defaultRepr = super.getDefaultRepresentation(forPlayer);
if (defaultRepr == null) {
    return Character.toLowerCase(this.callsign);
} else {
    return defaultRepr;
}

Could be written as:

Character defaultRepr = super.getDefaultRepresentation(forPlayer);
return Objects.requireNonNullElseGet(defaultRepr, () -> Character.toLowerCase(this.callsign));
@Luro02 Luro02 added enhancement New feature or request new-lint A new lint. api lints that check for code that reimplements parts of the api low-priority labels Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api lints that check for code that reimplements parts of the api enhancement New feature or request low-priority new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant