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

hashCode and toString should not be called on array instances. RSPEC-2116 #44

Closed
yeikel opened this issue Apr 16, 2022 · 4 comments · Fixed by #128 or #126
Closed

hashCode and toString should not be called on array instances. RSPEC-2116 #44

yeikel opened this issue Apr 16, 2022 · 4 comments · Fixed by #128 or #126
Assignees
Labels
good first issue Good for newcomers recipe

Comments

@yeikel
Copy link
Contributor

yeikel commented Apr 16, 2022

While hashCode and toString are available on arrays, they are largely useless. hashCode returns the array’s "identity hash code", and toString returns nearly the same value. Neither method’s output actually reflects the array’s contents. Instead, you should pass the array to the relevant static Arrays method.

    String argStr = args.toString(); // Noncompliant
    int argHash = args.hashCode(); // Noncompliant
    String argStr = Arrays.toString(args);
    int argHash = Arrays.hashCode(args);

More examples with Strings

int[] s = new int[]{1,2,3};
System.out.println(s);
Sytem.out.println(String.format("s=%s",s));

// .. any other usage where a String is expected
int[] s = new int[]{1,2,3};
System.out.println(Arrays.toString(s));
Sytem.out.println(String.format("s=%s",Arrays.toString(s)));

// .. any other usage where a String is expected

https://rules.sonarsource.com/java/RSPEC-2116

@yeikel yeikel changed the title hashCode and toString should not be called on array instances. RSPEC-2116 hashCode and toString should not be called on array instances. RSPEC-2116 Apr 16, 2022
@tkvangorder tkvangorder added enhancement New feature or request good first issue Good for newcomers labels Apr 18, 2022
@tkvangorder
Copy link
Contributor

This could be generalized with a recipe called "ChangeMethodInvocation" that matches a given method pattern and changes the invocation to an alternate method invocation that has a possibly different "select" and accepts the same arguments.

@mccartney
Copy link
Contributor

ChangeMethodInvocation [...] accepts the same arguments.

Except in this case the arguments are different (as opposed to #43)

@timtebeek timtebeek transferred this issue from openrewrite/rewrite May 1, 2023
@timtebeek
Copy link
Contributor

Some implementation notes:

  • both methods are available on all objects, so any implementation is performance sensitive: limit & return early where possible
  • probably best to implement hashCode separate from toString, as that's slightly easier
  • toString() can also be implied for arguments headed into String.format; that's probably best handled in a separate recipe or visitor to check the arguments to String.format() and similar methods such as printf.

@timtebeek
Copy link
Contributor

Reopened until we also merge in the separate recipes for toString #126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe
Projects
Archived in project
5 participants