Skip to content

BSL035 - Duplicate string literal

Summary

Duplicate string literal

Identifiers

Field Value
Rule code BSL035
Compatible alias DuplicateStringLiteral
Severity INFORMATION
Without configuration Yes
BSLLS JSON, ON mode without overrides Yes
Registry flag implemented Yes
Tags convention, readability

The implemented flag describes the registry; it does not certify verified BSLLS parity.

Behavior

  • The public identifier BSL035 and alias DuplicateStringLiteral 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 = ["BSL035"]
ignore = ["DuplicateStringLiteral"]

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: BSL035
  • bsl-disable:
Value = "example";  // bsl-disable: BSL035
  • compatible BSLLS form:
Value = "example";  // BSLLS:DuplicateStringLiteral-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: BSL035
// code without this diagnostic
// noqa-enable: BSL035

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

// BSLLS:DuplicateStringLiteral-off
// code without this diagnostic
// BSLLS:DuplicateStringLiteral-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

It is bad form to use the same string literals multiple times in the same module or method: - it can lead to problems with further maintenance, if necessary, change the value - there is a high probability of missing one of the repetitions - it can be a consequence of "copy-paste" - the developer may have forgotten to change the code after copying a similar block of code.

Features of the implementation of diagnostic

  • Diagnostics with default settings does not respect the case of literal characters - the strings AAAA and AaaA are considered the same.
  • You cannot specify a minimum parsed literal value less than the default. Short service literals are often used, which will generate unnecessary comments. For example: empty string "", selector numbers "1", "0", etc.
  • You cannot reduce the allowed number of repetitions to less than 1, because it makes no practical sense.

Examples

Bad code

Procedure Test(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One("Value");
    Else
        Result = Result + Two("Value");
    EndIf;
EndProcedure

Сorrected:

Procedure Test(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One(Result);
    Else
        Result = Result + Two(Result);
    EndIf;
EndProcedure

Bad code

Procedure Test2(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One("Value");
    Else
        Result = Result + Two("Value");
    EndIf;
EndProcedure

Procedure Test3(Param)
    If Param = "Five" Then
        Result = Result + Five("Value");
    EndIf;
EndProcedure

Сorrected

Procedure Test2(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One(StringValue());
    Else
        Result = Result + Two(StringValue());
    EndIf;
EndProcedure

Procedure Test3(Param)
    If Param = "Five" Then
        Result = Result + Five(StringValue());
    EndIf;
EndProcedure

Function StringValue()
   Return "Value";
EndFunction

Sources