可能在比较中缺少“self”¶
ID: py/comparison-missing-self
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- reliability
- maintainability
- external/cwe/cwe-570
- external/cwe/cwe-571
Query suites:
- python-security-and-quality.qls
当两个相同的表达式进行比较时,通常表明存在错误,因为比较的布尔值将始终相同。通常,它可能表明self
被遗漏了。
建议¶
将值与自身比较从不属于最佳实践。如果self
被遗漏,则插入它。如果确实需要常量行为,请使用布尔文字True
或False
,而不是以x == x
或类似方式对其进行模糊编码。
示例¶
class Customer:
def __init__(self, data):
self.data = data
def check_data(self, data):
if data != data: # Forgotten 'self'
raise Exception("Invalid data!")
#Fixed version
class Customer:
def __init__(self, data):
self.data = data
def check_data(self, data):
if self.data != data:
raise Exception("Invalid data!")