Miscellaneous-LocalScope

Declare variables as local as possible

Required inputs: IR

Defining variables in the minimum block scope possible reduces the visibility of those variables. This makes the code more readable as less code needs to be understood to see all usage of the variable.

Possible Messages

Key

Text

Severity

Disabled

function_file_static

{} can be declared static in primary file.

None

False

locality_block

{} can be declared in a more local scope.

None

False

locality_function

Global {} can be declared inside function.

None

False

locality_function_static

Global {} can be declared as local static inside function.

None

False

locality_loop_init

{} can be declared in the for-loop’s initialization.

None

False

unused_variable

Variable is unused.

None

False

var_file_static

{} can be declared static in primary file.

None

False

Options

allow_moving_to_other_primary_file

allow_moving_to_other_primary_file : bool = False

When a variable is only used in a single file X, but currently implemented in a different file Y, this controls whether the check suggests to move it into X and make it static there.
 

check_loop_counter

check_loop_counter : bool = False

Whether to report for-loop counters being declared before the loop that could be declared in the for-init part instead from C99 on.
 

consider_constructors_as_capturing

consider_constructors_as_capturing : bool = False

Whether passing a variable into a constructor is considered capturing it. This influences the check if making a global variable local would introduce a leaking reference: If the constructed object would outlive the local variable, making the variable local will not be suggested. If the option is set to false, passing variables into constructors has no effect on the analysis.
 

exclude_c_function_locals

exclude_c_function_locals : bool = False

If True, then the violation for C function local variables is disabled
 

exclude_dllexport

exclude_dllexport : bool = True

If true, no suggestions will be produced to make functions marked as dllexport or dllimport static in a primary file.
 

exclude_function_locals

exclude_function_locals : bool = True

If true, variables that could even be function-local are not reported.
 

exclude_undefined

exclude_undefined : bool = True

Whether only-declared symbols should be reported as well.
 

ignore_types

ignore_types : set[bauhaus.analysis.config.GlobPattern] = set()

Variables of types named here are ignored in this check. Globbing patterns are supported.
 

move_global_const_into_function

move_global_const_into_function : bool = True

Controls suggestions for global constants that could be declared locally in a function.
 

only_check_unit_locals

only_check_unit_locals : bool = False

Whether only global static variables should be checked. Note: this option is automatically activated during single-file analysis.
 

report_global_constants

report_global_constants : bool = False

Whether unused global constants should be reported.
 

report_nonstatic_functions

report_nonstatic_functions : bool = False

Whether functions that can be made unit-local (static) should be reported.
 

report_undefined_variables

report_undefined_variables : bool = True

Whether only-declared variables should be reported.
 

report_unused

report_unused : bool = True

Report unused variables.
 

suppress_attribute_maybe_unused

suppress_attribute_maybe_unused

Type: FilterAction

Default: 'suppress'

Filter action to apply to violations for objects marked as [[maybe_unused]] or __attribute__((unused)). If set to "suppress", findings will be reported but marked as suppressed. If set to "exclude", findings will not be reported at all. If set to "normal", no special treatment is applied and the finding is reported as usual.
 

template_args_can_be_static

template_args_can_be_static : bool = False

Whether functions whose address is used as template argument can be made static (some compilers, like Microsoft's, do not allow it then).
 

treat_initialization_as_use

treat_initialization_as_use : bool = False

Whether an explicit initialization should be considered a use of the variable.
 

treat_side_effect_constructors_as_use

treat_side_effect_constructors_as_use : bool = False

Whether variables should be seen as used if they are of a class type and initialized through a call to a constructor having a side-effect, e.g. std::lock_guard, or destroyed through a destructor having a side-effect.
 

Option Types

These types are used by options listed above:

FilterAction

Used for the return value of add_filter() callbacks.
 

normal

The violation proceeds as usual.

exclude

The violation is excluded: The violation is discarded as if `--exclude` applied to it. The following filters won't get to see the violation, and it will not be imported into the database under any circumstances.

suppress

The violation is suppressed as if `AXIVION DISABLE` applied to it. Whether the violation gets imported into the database is controlled by the Axivion CI configuration.