From 99b1bf689688f0c31cfddb28930ffffc8e729ff0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:27:23 +0100 Subject: [PATCH] Create rule S6902: SequencedCollection methods should be used to get the first or last element (#3596) * Create rule S6902: SequencedCollection methods should be preferred to get the first or last element * fix from review --------- Co-authored-by: alban-auzeill Co-authored-by: Alban Auzeill --- rules/S6902/java/metadata.json | 23 +++++++++++++ rules/S6902/java/rule.adoc | 62 ++++++++++++++++++++++++++++++++++ rules/S6902/metadata.json | 2 ++ 3 files changed, 87 insertions(+) create mode 100644 rules/S6902/java/metadata.json create mode 100644 rules/S6902/java/rule.adoc create mode 100644 rules/S6902/metadata.json diff --git a/rules/S6902/java/metadata.json b/rules/S6902/java/metadata.json new file mode 100644 index 00000000000..6fedb3bd9d4 --- /dev/null +++ b/rules/S6902/java/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "SequencedCollection methods should be preferred to get the first or last element", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6902", + "sqKey": "S6902", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "targeted", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S6902/java/rule.adoc b/rules/S6902/java/rule.adoc new file mode 100644 index 00000000000..62307c55bfc --- /dev/null +++ b/rules/S6902/java/rule.adoc @@ -0,0 +1,62 @@ +== Why is this an issue? + +Java 21 introduces a new SequencedCollection interface that provides a uniform API for accessing its first and last elements. +The new `getFirst()` and `getLast()` methods offer a consistent way to access elements across `SortedSet`, `NavigableSet`, `LinkedHashSet`, `List` and `Deque` collections. +Because those methods are more concise and readable, they should be used instead of more complex workarounds that recreate the same behavior. + +For example, `list.get(list.size() - 1)` can be replaced by `list.getLast()`. + +This rule identifies code that can be simplified by using the new `getFirst()` and `getLast()` methods. + +== How to fix it + +Replace the highlighted code with the corresponding `getFirst()` or `getLast()` methods. + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +public String concatenateFirstAndLast(List list) { + return list.get(0) + // Noncompliant + list.get(list.size() - 1); // Noncompliant +} +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +public String concatenateFirstAndLast(List list) { + return list.getFirst() + // Compliant + list.getLast(); // Compliant +} +---- + +==== Noncompliant code example + +[source,java,diff-id=2,diff-type=noncompliant] +---- +public String concatenateFirstAndLast(LinkedHashSet linkedHashSet) { + return linkedHashSet.iterator().next() + // Noncompliant + new ArrayList(linkedHashSet).get(linkedHashSet.size() - 1); // Noncompliant +} +---- + +==== Compliant solution + +[source,java,diff-id=2,diff-type=compliant] +---- +public String concatenateFirstAndLast(LinkedHashSet linkedHashSet) { + return linkedHashSet.getFirst() + // Compliant + linkedHashSet.getLast(); // Compliant +} +---- + +== Resources + +=== Documentation + +* https://openjdk.org/jeps/431[JEP 431: Sequenced Collections] +* https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/SequencedCollection.html[Java SE 21 API documentation - SequencedCollection] diff --git a/rules/S6902/metadata.json b/rules/S6902/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S6902/metadata.json @@ -0,0 +1,2 @@ +{ +}