2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Even if it is technically possible, https://docs.oracle.com/javase/specs/jls/se16/html/jls-3.html#jls-3.8[Restricted Identifiers] should not be used as identifiers. This is only possible for compatibility reasons, using it in Java code is confusing and should be avoided.
Note that this applies to any version of Java, including the one where these identifiers are not yet restricted, to avoid future confusion.
This rule reports an issue when restricted identifiers:
* var
* yield
* record
are used as identifiers.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
var var = "var"; // Noncompliant: compiles but this code is confusing
var = "what is this?";
int yield(int i) { // Noncompliant
return switch (i) {
case 1: yield(0); // This is a yield from switch expression, not a recursive call.
default: yield(i-1);
};
}
String record = "record"; // Noncompliant
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
var myVariable = "var";
int minusOne(int i) {
return switch (i) {
case 1: yield(0);
default: yield(i-1);
};
}
String myRecord = "record";
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.oracle.com/javase/specs/jls/se16/html/jls-3.html#jls-3.8[JLS16, 3.8: Identifiers]
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Rename this [variable|method name] to not match a restricted identifier.
=== Highlighting
Identifier
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]