IJ-CR-145218 [java-inspections] IDEA-201932 Provide inspection to highlight redundant creation operations in java date time api

- combine into one inspection
- fix messages

GitOrigin-RevId: d4e064948f8c730c4d68c58e6c9b1277c6b66b4d
This commit is contained in:
Mikhail Pyltsin
2024-09-26 19:14:26 +02:00
committed by intellij-monorepo-bot
parent a7dfa7615e
commit f474d8047b
26 changed files with 869 additions and 934 deletions

View File

@@ -1,118 +0,0 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import java.time.*;
import java.time.chrono.*;
public class Main {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
LocalTime localTime2 = LocalTime.now();
boolean b = localTime.com<caret>pareTo(localTime2) > 0;
System.out.println(b);
b = localTime.compareTo(localTime2) >= 0;
System.out.println(b);
b = localTime.compareTo(localTime2) < 0;
System.out.println(b);
b = localTime.compareTo(localTime2) <= 0;
System.out.println(b);
b = localTime.compareTo(localTime2) == 0;
System.out.println(b);
b = localTime.compareTo(localTime2) != 0;
System.out.println(b);
b = 0 < localTime.compareTo(localTime2);
System.out.println(b);
b = 0 <= localTime.compareTo(localTime2);
System.out.println(b);
b = 0 > localTime.compareTo(localTime2);
System.out.println(b);
b = 0 >= localTime.compareTo(localTime2);
System.out.println(b);
b = 0 == localTime.compareTo(localTime2);
System.out.println(b);
b = 0 != localTime.compareTo(localTime2);
System.out.println(b);
b = ((localTime).compareTo((localTime2)) != 0);
System.out.println(b);
OffsetTime offsetTime = OffsetTime.now();
OffsetTime offsetTime2 = OffsetTime.now();
b = offsetTime.compareTo(offsetTime2) > 0;
System.out.println(b);
b = (((offsetTime.compareTo(offsetTime2)))) > 0;
System.out.println(b);
b = offsetTime.compareTo(offsetTime2) >= 0;
System.out.println(b);
b = offsetTime.compareTo(offsetTime2) < 0;
System.out.println(b);
b = offsetTime.compareTo(offsetTime2) <= 0;
System.out.println(b);
b = offsetTime.compareTo(offsetTime2) == 0;
System.out.println(b);
b = offsetTime.compareTo(offsetTime2) != 0;
System.out.println(b);
OffsetDateTime offsetDateTime = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = OffsetDateTime.now();
b = offsetDateTime.compareTo(offsetDateTime2) > 0;
System.out.println(b);
b = offsetDateTime.compareTo(offsetDateTime2) >= 0;
System.out.println(b);
b = offsetDateTime.compareTo(offsetDateTime2) < 0;
System.out.println(b);
b = offsetDateTime.compareTo(offsetDateTime2) <= 0;
System.out.println(b);
b = offsetDateTime.compareTo(offsetDateTime2) == 0;
System.out.println(b);
b = offsetDateTime.compareTo(offsetDateTime2) != 0;
System.out.println(b);
LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.now();
b = localDate.compareTo(localDate2) > 0;
System.out.println(b);
b = localDate.compareTo(localDate2) >= 0;
System.out.println(b);
b = localDate.compareTo(localDate2) < 0;
System.out.println(b);
b = localDate.compareTo(localDate2) <= 0;
System.out.println(b);
b = localDate.compareTo(localDate2) == 0;
System.out.println(b);
b = localDate.compareTo(localDate2) != 0;
System.out.println(b);
b = localDate.compareTo(new ChronoLocalDate() {}) > 0;
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now();
b = localDateTime.compareTo(localDateTime2) > 0;
System.out.println(b);
b = localDateTime.compareTo(localDateTime2) >= 0;
System.out.println(b);
b = localDateTime.compareTo(localDateTime2) < 0;
System.out.println(b);
b = localDateTime.compareTo(localDateTime2) <= 0;
System.out.println(b);
b = localDateTime.compareTo(localDateTime2) == 0;
System.out.println(b);
b = localDateTime.compareTo(localDateTime2) != 0;
System.out.println(b);
b = /*test0*/localDateTime/*test1*/./*test2*/compareTo/*test3*/(/*test4*/localDateTime2/*test5*/) > 0;
System.out.println(b);
try {
b = localDateTime.compareTo(new ChronoLocalDateTime<>() {}) > 0;
}
catch (Exception e) {
}
b = /*test0*/localDateTime/*test1*/./*test2*/compareTo/*test3*/(/*test4*/localDateTime2/*test5*/) > 0;
}
public static boolean unresolvedType(LocalTime localTime) {
return localTime.compareTo(unresolved) > 0;
}
}

View File

@@ -1,9 +0,0 @@
import java.time.*;
class Main {
LocalTime convert(LocalTime source)
{
return LocalTime.from<caret>(source);
}
}

View File

@@ -1,5 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.temporal.ChronoField;
class Main {

View File

@@ -5,6 +5,6 @@ import java.time.temporal.ChronoField;
class Main {
public static int test(OffsetDateTime offsetDateTime) {
return offsetDateTime.get<caret>(ChronoField.NANO_OF_SECOND);
return offsetDateTime.<warning descr="Calls with explicit 'ChronoField' or 'ChronoUnit' arguments call can be simplified">get<caret></warning>(ChronoField.NANO_OF_SECOND);
}
}

View File

@@ -0,0 +1,118 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import java.time.*;
import java.time.chrono.*;
public class Main {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
LocalTime localTime2 = LocalTime.now();
boolean b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo<caret></warning>(localTime2) > 0;
System.out.println(b);
b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2) >= 0;
System.out.println(b);
b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2) < 0;
System.out.println(b);
b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2) <= 0;
System.out.println(b);
b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2) == 0;
System.out.println(b);
b = localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2) != 0;
System.out.println(b);
b = 0 < localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = 0 <= localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = 0 > localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = 0 >= localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = 0 == localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = 0 != localTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localTime2);
System.out.println(b);
b = ((localTime).<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>((localTime2)) != 0);
System.out.println(b);
OffsetTime offsetTime = OffsetTime.now();
OffsetTime offsetTime2 = OffsetTime.now();
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) > 0;
System.out.println(b);
b = (((offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2)))) > 0;
System.out.println(b);
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) >= 0;
System.out.println(b);
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) < 0;
System.out.println(b);
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) <= 0;
System.out.println(b);
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) == 0;
System.out.println(b);
b = offsetTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetTime2) != 0;
System.out.println(b);
OffsetDateTime offsetDateTime = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = OffsetDateTime.now();
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) > 0;
System.out.println(b);
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) >= 0;
System.out.println(b);
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) < 0;
System.out.println(b);
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) <= 0;
System.out.println(b);
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) == 0;
System.out.println(b);
b = offsetDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(offsetDateTime2) != 0;
System.out.println(b);
LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.now();
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) > 0;
System.out.println(b);
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) >= 0;
System.out.println(b);
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) < 0;
System.out.println(b);
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) <= 0;
System.out.println(b);
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) == 0;
System.out.println(b);
b = localDate.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDate2) != 0;
System.out.println(b);
b = localDate.compareTo(new <error descr="Class 'Anonymous class derived from ChronoLocalDate' must implement abstract method 'getChronology()' in 'ChronoLocalDate'">ChronoLocalDate</error>() {}) > 0;
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now();
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) > 0;
System.out.println(b);
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) >= 0;
System.out.println(b);
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) < 0;
System.out.println(b);
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) <= 0;
System.out.println(b);
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) == 0;
System.out.println(b);
b = localDateTime.<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>(localDateTime2) != 0;
System.out.println(b);
b = /*test0*/localDateTime/*test1*/./*test2*/<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>/*test3*/(/*test4*/localDateTime2/*test5*/) > 0;
System.out.println(b);
try {
b = localDateTime.compareTo(new <error descr="Class 'Anonymous class derived from ChronoLocalDateTime' must implement abstract method 'toLocalDate()' in 'ChronoLocalDateTime'">ChronoLocalDateTime<></error>() {}) > 0;
}
catch (Exception e) {
}
b = /*test0*/localDateTime/*test1*/./*test2*/<warning descr="Expression with 'java.time' 'compareTo()' call can be simplified">compareTo</warning>/*test3*/(/*test4*/localDateTime2/*test5*/) > 0;
}
public static boolean unresolvedType(LocalTime localTime) {
return localTime.compareTo(<error descr="Cannot resolve symbol 'unresolved'">unresolved</error>) > 0;
}
}

View File

@@ -0,0 +1,9 @@
import java.time.*;
class Main {
LocalTime convert(LocalTime source)
{
return LocalTime.<warning descr="Redundant 'LocalTime.from()' call">from<caret></warning>(source);
}
}

View File

@@ -1,19 +1,20 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.temporal.ChronoField;
class Main {
public static int test(OffsetDateTime offsetDateTime) {
return offsetDateTime.get<caret>(ChronoField.NANO_OF_SECOND);
return offsetDateTime.<warning descr="Calls with explicit 'ChronoField' or 'ChronoUnit' arguments call can be simplified">get<caret></warning>(ChronoField.NANO_OF_SECOND);
}
public static int test2(OffsetDateTime offsetDateTime) {
return offsetDateTime.get(ChronoField.INSTANT_SECONDS);
}
public static int test3(OffsetDateTime offsetDateTime) {
return offsetDateTime.get(ChronoField.YEAR);
return offsetDateTime.<warning descr="Calls with explicit 'ChronoField' or 'ChronoUnit' arguments call can be simplified">get</warning>(ChronoField.YEAR);
}
public static void test4(OffsetTime time) {
time.get(1, ChronoUnit.YEARS);
time.get(1, <error descr="Cannot resolve symbol 'ChronoUnit'">ChronoUnit</error>.YEARS);
}
}

View File

@@ -1,35 +1,35 @@
import java.time.*;
public class DateRedundant {
class DateRedundant {
class First {
LocalDateTime convert(LocalDateTime source)
{
return LocalDateTime.from<caret>(source);
return LocalDateTime.<warning descr="Redundant 'LocalDateTime.from()' call">from<caret></warning>(source);
}
LocalTime convert(LocalTime source)
{
return LocalTime.from(source);
return LocalTime.<warning descr="Redundant 'LocalTime.from()' call">from</warning>(source);
}
LocalDate convert(LocalDate source)
{
return LocalDate.from(source);
return LocalDate.<warning descr="Redundant 'LocalDate.from()' call">from</warning>(source);
}
OffsetDateTime convert(OffsetDateTime source)
{
return OffsetDateTime.from(source);
return OffsetDateTime.<warning descr="Redundant 'OffsetDateTime.from()' call">from</warning>(source);
}
OffsetTime convert(OffsetTime source)
{
return OffsetTime.from(source);
return OffsetTime.<warning descr="Redundant 'OffsetTime.from()' call">from</warning>(source);
}
ZonedDateTime convert(ZonedDateTime source)
{
return ZonedDateTime.from(source);
return ZonedDateTime.<warning descr="Redundant 'ZonedDateTime.from()' call">from</warning>(source);
}
}
@@ -37,7 +37,7 @@ public class DateRedundant {
static class Third {
private LocalDate getLocalDate(LocalDate fullTime)
{
return LocalDate.of(fullTime.getYear(),
return LocalDate.<warning descr="Redundant creation of 'LocalDate' object">of</warning>(fullTime.getYear(),
fullTime.getMonth(),
fullTime.getDayOfMonth()
);
@@ -45,7 +45,7 @@ public class DateRedundant {
private LocalDate getLocalDate(ZonedDateTime fullTime)
{
return LocalDate.of(fullTime.getYear(),
return LocalDate.<warning descr="Redundant creation of 'LocalDate' object">of</warning>(fullTime.getYear(),
fullTime.getMonth(),
fullTime.getDayOfMonth()
);
@@ -53,7 +53,7 @@ public class DateRedundant {
private LocalTime getLocalTime(ZonedDateTime fullTime)
{
return LocalTime.of(fullTime.getHour(),
return LocalTime.<warning descr="Redundant creation of 'LocalTime' object">of</warning>(fullTime.getHour(),
fullTime.getMinute(),
fullTime.getSecond(),
fullTime.getNano());
@@ -61,7 +61,7 @@ public class DateRedundant {
private LocalDate getLocalDate(LocalDateTime fullTime)
{
return LocalDate.of(fullTime.getYear(),
return LocalDate.<warning descr="Redundant creation of 'LocalDate' object">of</warning>(fullTime.getYear(),
fullTime.getMonth(),
fullTime.getDayOfMonth()
);
@@ -69,7 +69,7 @@ public class DateRedundant {
private LocalDate getLocalDate2(LocalDateTime fullTime)
{
return LocalDate.of(fullTime.getYear(),
return LocalDate.<warning descr="Redundant creation of 'LocalDate' object">of</warning>(fullTime.getYear(),
fullTime.getMonthValue(),
fullTime.getDayOfMonth()
);
@@ -77,7 +77,7 @@ public class DateRedundant {
private LocalTime getLocalTime(LocalDateTime fullTime)
{
return LocalTime.of(fullTime.getHour(),
return LocalTime.<warning descr="Redundant creation of 'LocalTime' object">of</warning>(fullTime.getHour(),
fullTime.getMinute(),
fullTime.getSecond(),
fullTime.getNano());
@@ -85,7 +85,7 @@ public class DateRedundant {
private LocalDate getLocalDate(OffsetDateTime fullTime)
{
return LocalDate.of(fullTime.getYear(),
return LocalDate.<warning descr="Redundant creation of 'LocalDate' object">of</warning>(fullTime.getYear(),
fullTime.getMonthValue(),
fullTime.getDayOfMonth()
);
@@ -94,7 +94,7 @@ public class DateRedundant {
private LocalTime getLocalTime(OffsetDateTime fullTime)
{
return LocalTime.
of(fullTime.getHour(),
<warning descr="Redundant creation of 'LocalTime' object">of</warning>(fullTime.getHour(),
fullTime.getMinute(),
fullTime.getSecond(),
fullTime.getNano());
@@ -102,7 +102,7 @@ public class DateRedundant {
private LocalTime getLocalTime(LocalTime fullTime)
{
return LocalTime.of(fullTime.getHour(),
return LocalTime.<warning descr="Redundant creation of 'LocalTime' object">of</warning>(fullTime.getHour(),
fullTime.getMinute(),
fullTime.getSecond(),
fullTime.getNano());