Skip to content

BSL148 — All execution paths of a function must have a Return statement

Summary

All execution paths of a function must have a Return statement

Identifiers

Field Value
Rule code BSL148
Compatible alias AllFunctionPathMustHaveReturn
Severity ERROR
Enabled by default Yes
Implemented Yes
Tags error-handling, correctness

Behavior

  • The public identifier BSL148 and alias AllFunctionPathMustHaveReturn are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL148"]
ignore = ["AllFunctionPathMustHaveReturn"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL148
  • bsl-disable:
Value = "example";  // bsl-disable: BSL148
  • compatible BSLLS form:
Value = "example";  // BSLLS:AllFunctionPathMustHaveReturn-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL148
// code without this diagnostic
// noqa-enable: BSL148

// bsl-disable: BSL148
// code without this diagnostic
// bsl-enable: BSL148

// BSLLS:AllFunctionPathMustHaveReturn-off
// code without this diagnostic
// BSLLS:AllFunctionPathMustHaveReturn-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

Functions should not have an implicit return. If control reaches the EndFunction line function returns an Undefined value.

As a rule, this is not a normal operation; the programmer must explicitly describe all return values of the function. However, it is quite easy to overlook a situation in which control reaches the EndFunction line and returns an unexpected Undefined value.

This diagnostics checks that all possible paths of the function execution have an explicit Return statement and the function does not return unexpected values.

Examples

Incorrect

// if the rate is full, but not Tax10 not Tax10 - returns Undefined
// this could be error or planned behavior.
Function DefineTaxRate(Val Rate)
    If Rate = Enums.TaxRates.Tax20 Then
        Return 20;
    ElsIf Rate = Enums.TaxRates.Tax10 Then
        Return 10;
    ElsIf Not ValueIsFilled(Rate) Then
        Return Constants.DefaultTaxRate.Get();
    EndIf;

    // implicit return Undefined
EndFunction

Correct

// explicitly specify the intention to return the result in the end of the function.
Function DefineTaxRate(Val Rate)
    If Rate = Enums.TaxRates.Tax20 Then
        Return 20;
    ElsIf Rate = Enums.TaxRates.Tax10 Then
        Return 10;
    ElsIf Not ValueIsFilled(Rate) Then
        Return Constants.DefaultTaxRate.Get();
    EndIf;

    // explicit return
    Return Undefined;
EndFunction

Another example of incorrect code:

Function DiscountAmount(Val OrderBasket)
    If OrderBasket.Rows.Count() > 10 Then
        Return Discounts.DiscountOnBigBasket(OrderBasket);
    ElsIf OrderBasket.IsCustomerCard Then
        // function returns an unintended value is Undefined
        Discounts.DiscountByCustomerCard(OrderBasket);
    Else
        Return 0;
    EndIf;
EndFunction