-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error if member access must be null safe but isn't (#626)
Closes partially #543 ### Summary of Changes * If a member access is not null safe, but the receiver is nullable, an error is shown now. * The scope provider is now more relaxed and points to the correct class members, even if the member access is missing null safety. --------- Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
e77037e
commit 077daff
Showing
7 changed files
with
101 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/language/validation/other/expressions/memberAccesses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import { SdsMemberAccess } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { UnknownType } from '../../../typing/model.js'; | ||
|
||
export const CODE_MEMBER_ACCESS_MISSING_NULL_SAFETY = 'member-access/missing-null-safety'; | ||
|
||
export const memberAccessMustBeNullSafeIfReceiverIsNullable = | ||
(services: SafeDsServices) => | ||
(node: SdsMemberAccess, accept: ValidationAcceptor): void => { | ||
if (node.isNullSafe) { | ||
return; | ||
} | ||
|
||
const receiverType = services.types.TypeComputer.computeType(node.receiver); | ||
if (receiverType === UnknownType) { | ||
return; | ||
} | ||
|
||
if (receiverType.isNullable) { | ||
accept('error', 'The receiver can be null so a safe access must be used.', { | ||
node, | ||
code: CODE_MEMBER_ACCESS_MISSING_NULL_SAFETY, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...s/resources/validation/other/expressions/member accesses/missing safe access/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package validation.other.expressions.memberAccesses.missingSafeAccess | ||
|
||
class MyClass() { | ||
attr a: Int | ||
} | ||
|
||
fun nullableMyClass() -> instance: MyClass? | ||
|
||
pipeline test { | ||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»1.a«(); | ||
|
||
// $TEST$ error "The receiver can be null so a safe access must be used." | ||
»null.a«(); | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»nullableMyClass().a«(); // This should be where the error is reported. Remove the "no" on the bug in Langium is fixed. | ||
|
||
// $TEST$ error "The receiver can be null so a safe access must be used." | ||
nullableMyClass()».a«(); // Langium currently computes the wrong range for a member access | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»unresolved.a«(); | ||
|
||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»1?.a«(); | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»null?.a«(); | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»nullableMyClass()?.a«(); | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
nullableMyClass()»?.a«(); // Langium currently computes the wrong range for a member access | ||
|
||
// $TEST$ no error "The receiver can be null so a safe access must be used." | ||
»unresolved?.a«(); | ||
} |
30 changes: 24 additions & 6 deletions
30
tests/resources/validation/style/unnecessary safe access/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
package tests.validation.style.unnecessarySafeAccess | ||
|
||
class MyClass | ||
class MyClass { | ||
attr a: Int | ||
} | ||
|
||
fun nullableMyClass() -> result: MyClass? | ||
|
||
pipeline test { | ||
// $TEST$ info "The receiver is never null, so the safe access is unnecessary." | ||
»1?.toString«(); | ||
»1?.a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»null?.a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»nullableMyClass()?.a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
nullableMyClass()»?.a«(); // Langium currently computes the wrong range for a member access | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»unresolved?.a«(); | ||
|
||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»1.a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»null?.toString«(); | ||
»null.a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»nullableMyClass()?.toString«(); | ||
»nullableMyClass().a«(); | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
nullableMyClass()»?.toString«(); // Langium currently computes the wrong range for a member access | ||
nullableMyClass()».a«(); // Langium currently computes the wrong range for a member access | ||
|
||
// $TEST$ no info "The receiver is never null, so the safe access is unnecessary." | ||
»unresolved?.toString«(); | ||
»unresolved.a«(); | ||
} |