
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In Java 10 https://openjdk.java.net/jeps/286[Local-Variable Type Inference] was introduced. It allows you to omit the expected type of a variable by declaring it with the ``++var++`` keyword.
|
|
|
|
While it is not always possible or cleaner to use this new way of declaring a variable, when the type on the left is the same as the one on the right in an assignment, using the ``++var++`` will result in a more concise code.
|
|
|
|
|
|
This rule reports an issue when the expected type of the variable is the same as the returned type of assigned expression and the type can be easily inferred by the reader, either when the type is already mentioned in the name or the initializer, or when the expression is self-explanatory.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
MyClass myClass = new MyClass();
|
|
|
|
int i = 10; // Type is self-explanatory
|
|
|
|
MyClass something = MyClass.getMyClass(); // Type is already mentioned in the initializer
|
|
|
|
MyClass myClass = get(); // Type is already mentioned in the name
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
var myClass = new MyClass();
|
|
|
|
var i = 10;
|
|
|
|
var something = MyClass.getMyClass();
|
|
|
|
var myClass = get();
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://openjdk.java.net/jeps/286[JEP 286: Local-Variable Type Inference]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Declare this local variable with "var" instead.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Variable declaration type.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|