49 lines
1.3 KiB
Plaintext
49 lines
1.3 KiB
Plaintext
This rule raises an issue when unused scope-limited definitions are found.
|
|
|
|
== Why is this an issue?
|
|
|
|
When a class or function is defined in a parent function or method, it is only visible in this parent function or method's scope.
|
|
If the defined class or function is not used within this scope it is dead code (unnecessary, inoperative code) that should be removed.
|
|
|
|
Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
def parent_function():
|
|
def nested_function(): # Noncompliant: this function is never used in this scope.
|
|
print("nested_function")
|
|
|
|
class NestedClass: # Noncompliant: this class is never used in this scope.
|
|
def __init__(self):
|
|
print("NestedClass")
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
----
|
|
def parent_function():
|
|
class NestedClass:
|
|
def __init__(self):
|
|
print("NestedClass")
|
|
|
|
NestedClass()
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|