Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-824

Functions should not be declared at block scope

    Details

    • Message:
      Move this function declaration outside of this block scope.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • MISRA C 2004:
      8.6
    • MISRA C++ 2008:
      3-1-2

      Description

      A function declared at block scope will refer to a member of the enclosing namespace, and so the declaration should be explicitly placed at the namespace level.

      Additionally, where a declaration statement could either declare a function or an object, the compiler will choose to declare the function. To avoid potential developer confusion over the meaning of a declaration, functions should not be declared at block scope.

      Noncompliant Code Example

      class A {
      };
      
      void fun() {
        void nestedFun();  // Noncompliant; declares a function in block scope
      
        A a();      // Noncompliant; declares a function at block scope, not an object
      }
      

      See

      • MISRA C:2004, 8.6 - Functions shall be declared at file scope
      • MISRA C++:2008, 3-1-2 - Functions shall not be declared at block scope

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: