C and C++


LANG.STRUCT.SCOPE.LOCAL : Scope Could Be Local Static

Summary

A variable that requires only local static scope has been declared with larger scope.

Note: When configuration file parameter SCOPE_CHECK_SUGGESTS_LOCAL_STATIC is set to No, Scope Could Be Local Static warnings are not issued. Instead, global static variables that could be local static are reported as Scope Could Be File Static, and file static variables that could be local static are not reported at all.

Properties

Class Name Scope Could Be Local Static
Significance style
Mnemonic LANG.STRUCT.SCOPE.LOCAL
Categories
MisraC2023 MisraC2023:8.7 Functions and objects should not be defined with external linkage if they are referenced in only one translation unit
  MisraC2023:8.8 The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage
  MisraC2023:8.9 An object should be declared at block scope if its identifier only appears in a single function
Misra2012 Misra2012:8.7 Functions and objects should not be defined with external linkage if they are referenced in only one translation unit
  Misra2012:8.8 The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage
  Misra2012:8.9 An object should be declared at block scope if its identifier only appears in a single function
Misra2004 Misra2004:8.7 Objects shall be defined at block scope if they are only accessed from within a single function
  Misra2004:8.10 All declarations and definitions of objects or functions at file scope shall have internal linkage unless external linkage is required
  Misra2004:8.11 The static storage class specifier shall be used in definitions and declarations of objects and functions that have internal linkage
AUTOSARC++14 AUTOSARC++14:M3-4-1 An identifier declared to be an object or type shall be defined in a block that minimizes its visibility.
MisraC++2008 MisraC++2008:3-4-1 An identifier declared to be an object or type shall be defined in a block that minimizes its visibility.
CWE CWE:1126 Declaration of Variable with Unnecessarily Wide Scope
CERT-C CERT-C:DCL19-C Minimize the scope of variables and functions
JSF++ JSF++:136 Declarations should be at the smallest feasible scope.
POW10 POW10:6 Declare data objects at the smallest possible level of scope.
JPL JPL:13 Declare data objects at smallest possible level of scope.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Scope Could Be Local Static"

Example

int myint = 5;  /* 'Scope Could Be Local Static' warning issued here */

/* ... */                       /* code that does not use myint */

int modify_int(int i){
    return i + myint;
}

/* ... */                       /* code that does not use myint */

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.