Object-returning function could return a null reference #2103
Labels
code-path-analysis
Involves simulating execution paths / interpreting the user code ..to an extent.
difficulty-03-duck
Involves more challenging problems and/or developing within and revising the internals API
enhancement
Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope.
feature-inspections
up-for-grabs
Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky
Milestone
A common rookie mistake is to assume an object-returning function will return a valid object reference.
Here the built-in
Find
method returns an object reference that can beNothing
- running the.Activate
member call on a null reference will throw a runtime error 91 "object or with block variable not set".The fix would be to introduce a local variable to capture the reference, verify whether it's valid, and then proceed to use it:
There are other cases where runtime error 91 would be raised, e.g.
...but these are harder to properly verify whether the null-check is made or not; let's concentrate the efforts on direct member calls - that way we know no null-check is made, and we don't even need to look for the function's return value or track down its usage throughout the procedure.
In other words, the former snippet would trip the inspection, and the latter wouldn't - at least for a first iteration.
Name:
PossibleNullReferenceInspection
Type:
CodeInspectionType.CodeQualityIssues
Default Severity:
CodeInspectionSeverity.Error
Result text:
Info:
An
@Ignore PossibleNullReference
attribute atop the function usage would prevent that usage from firing up an inspection result.For user code (i.e. not built-in), we should offer the option of marking the function itself with an annotation saying "bugger off, this function will never return a null reference" - perhaps a
@NotNull
annotation could be useful here, and could be reused eventually for other purposes. If anything it makes useful and documenting metadata anyway.The text was updated successfully, but these errors were encountered: