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

Functions which do not return should be declared as "noreturn"

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      function could be declared with attribute 'noreturn'
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Level:
      Control-flow Analysis

      Description

      The attribute noreturn indicates that a function does not return. This information clarifies the behavior of the function and it allows the compiler to do optimizations.

      It can also help the compiler (and static analyzer tools, i.e. us) provide better error messages:

      __attribute__((noreturn)) void f();
      
      int g(int b) {
        if (b == 5) {
          f();
          printf("Hello world\n"); // This is dead code, the compiler/static analyzer can now detect it
          // There is no returned value, but it is fine, the compiler/static analyzer knows not to warn about it
        } else {
          return 3;
        }
      }
      

      This rule detects when the attribute noreturn can be added to a function.

      Noncompliant Code Example

      void g() { // Noncompliant
        abort();
      }
      

      Compliant Solution

      __attribute__((noreturn)) void g() { // or [[noreturn]] for C++
        abort(); // Compliant
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              amelie.renard Amélie Renard
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: