apex-dev-tools / apex-ls

Apex language server library
Other
2 stars 1 forks source link

Final assignment: Apex vs apex-ls differences #124

Open bmathewcertinia opened 1 year ago

bmathewcertinia commented 1 year ago

This contains a collection of Final assignment issues that differ from what Apex allows and the way we have implemented it. Apex is not consistent with the final assignment rules, we are trying to be slightly better.

Local Unassigned final value

Currently the below snippet produces the error Variable 'actualMessage' can not be assigned to, it is final

final String actualMessage;
// When - Then
try {
    ...
} catch (AuraHandledException ex) {
    actualMessage = ex.getMessage();
}

This is covered by the unit test Non-Initialised local in FinalTest.scala

Reassigning a null final variable

If the first assignment of a final variable is null wither explicitly or from a function then reassigning it after a null check is valid in Apex but apex-ls do not allow any modification of final assignment after it is assigned.

public static void fn() { 
   final String finalString = null;
    if (finalString == null) {
        finalString = 'abcdefg';
        System.assert(false, ' RESULT: '+ finalString);   
    }
}

fn(); // System.AssertException: Assertion Failed: RESULT: abcdefg
kjonescertinia commented 1 year ago

I am OK with these differences but I think we should change the warning message a little, from can not be assigned to, it is finaltoshould not be assigned to, it is final` to indicate we don't think you should do this, but you can if you really want.