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

Adding a char array through dedicated for loop instead of a string #621

Open
Luro02 opened this issue Oct 11, 2024 · 0 comments
Open

Adding a char array through dedicated for loop instead of a string #621

Luro02 opened this issue Oct 11, 2024 · 0 comments
Labels
enhancement New feature or request low-priority new-lint A new lint.

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Oct 11, 2024

What it does

This lint seems more like an edge-case, so the value of it, is questionable.
The main idea is that some code can be simplified by invoking the String constructor for char arrays and then using the string value instead.

Lint Name

USE_STRING_CONSTRUCTOR

Category

api

Example

String buildString(StringBuilder output, char[] array) {
    for (char element : array) {
        output.append(element);
    }
    return output.toString();
}

Could be written as:

String buildString(StringBuilder output, char[] array) {
    output.append(new String(array));
    return output.toString();
}

Similarly this could detect redundant conversions to string like:

String buildString(char[] array) {
    String result = "";
    for (char element : array) {
        result += element;
    }
    return result;
}

Could be written as:

String buildString(char[] array) {
    return new String(array);
}
@Luro02 Luro02 added enhancement New feature or request new-lint A new lint. low-priority labels Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low-priority new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant