Uploaded image for project: 'SonarPython'
  1. SonarPython
  2. SONARPY-666

Rule S3984: Exceptions should not be created without being raised

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.11
    • Component/s: Rules
    • Labels:

      Description

      Expected Issues:

      class CustomException(TypeError):
          pass
      
      def InstantiatedBuiltinExceptions():
          BaseException()  # Noncompliant
          Exception()  # Noncompliant
          ValueError()  # Noncompliant
          CustomException()  # Noncompliant
      
          # An unused reference to an exception class is also Noncompliant, even when it is not called.
          BaseException  # Noncompliant
          Exception  # Noncompliant
          ValueError  # Noncompliant
          CustomException  # Noncompliant
      
      
      def compliant(param, func):
          lambda: ValueError() if param else None
          func(ValueError())
          if param == 1:
              raise ValueError
          elif param == 2:
              raise ValueError()
          return ValueError()
      
      def variables_are_out_of_scope():
          """S3984 doesn't raise on exceptions assigned to unused variables.
          S1481 already raises on unused variables.
          """
          var = ValueError()  # No issue from S3984
      
      def gen():
          yield ValueError()
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                andrey.tyukin Andrey Tyukin
                Reporter:
                nicolas.harraudeau Nicolas Harraudeau
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: