mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-109376 Groovy: "Final variable access" inspection doesn't report assignments to final parameters
This commit is contained in:
@@ -361,6 +361,7 @@ cannot.find.operator.overload.method=Cannot resolve index access with arguments
|
||||
named.arguments.are.not.allowed.inside.index.operations=Named arguments are not allowed inside index operations
|
||||
expected.0.to.be.inline.constant=Expected ''{0}'' to be an inline constant
|
||||
cannot.assign.a.value.to.final.field.0=Cannot assign a value to final field ''{0}''
|
||||
cannot.assign.a.value.to.final.parameter.0=Cannot assign a value to final parameter ''{0}''
|
||||
variable.0.might.not.have.been.initialized=Variable ''{0}'' might not have been initialized
|
||||
unexpected.symbol=Unexpected symbol
|
||||
statement.expected=Statement expected
|
||||
|
||||
@@ -284,6 +284,7 @@ public class GroovyAnnotator extends GroovyElementVisitor {
|
||||
checkStringNameIdentifier(referenceExpression);
|
||||
checkThisOrSuperReferenceExpression(referenceExpression, myHolder);
|
||||
checkFinalFieldAccess(referenceExpression);
|
||||
checkFinalParameterAccess(referenceExpression);
|
||||
if (ResolveUtil.isKeyOfMap(referenceExpression)) {
|
||||
PsiElement nameElement = referenceExpression.getReferenceNameElement();
|
||||
LOG.assertTrue(nameElement != null);
|
||||
@@ -296,6 +297,19 @@ public class GroovyAnnotator extends GroovyElementVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkFinalParameterAccess(GrReferenceExpression ref) {
|
||||
final PsiElement resolved = ref.resolve();
|
||||
|
||||
if (resolved instanceof GrParameter) {
|
||||
final GrParameter parameter = (GrParameter)resolved;
|
||||
if (parameter.isPhysical() && parameter.hasModifierProperty(FINAL) && PsiUtil.isLValue(ref)) {
|
||||
if (parameter.getDeclarationScope() instanceof PsiMethod) {
|
||||
myHolder.createErrorAnnotation(ref, GroovyBundle.message("cannot.assign.a.value.to.final.parameter.0", parameter.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkFinalFieldAccess(@NotNull GrReferenceExpression ref) {
|
||||
final PsiElement resolved = ref.resolve();
|
||||
|
||||
|
||||
+24
@@ -1426,4 +1426,28 @@ A.foo = 3 //no error
|
||||
}
|
||||
''')
|
||||
}
|
||||
|
||||
void testFinalParameter() {
|
||||
testHighlighting('''\
|
||||
def foo0(final i) {
|
||||
<error descr="Cannot assign a value to final parameter 'i'">i</error> = 5
|
||||
print i
|
||||
}
|
||||
|
||||
def foo1(i) {
|
||||
i = 5
|
||||
print i
|
||||
}
|
||||
|
||||
def foo2(final i = 4) {
|
||||
<error descr="Cannot assign a value to final parameter 'i'">i</error> = 5
|
||||
print i
|
||||
}
|
||||
|
||||
def foo3(final i) {
|
||||
print i
|
||||
}
|
||||
''')
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user