Maybe missing ‘self’ in comparison¶
ID: py/comparison-missing-selfKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - reliability - correctness - external/cwe/cwe-570 - external/cwe/cwe-571Query suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
When two identical expressions are compared it is typically an indication of a mistake, since the Boolean value of the comparison will always be the same. Often, it can indicate thatself has been omitted.
Recommendation¶
It is never good practice to compare a value with itself. Ifself has been omitted, then insert it. If the constant behavior is indeed required, use the Boolean literalsTrue orFalse, rather than encoding them obscurely asx==x or similar.
Example¶
classCustomer:def__init__(self,data):self.data=datadefcheck_data(self,data):ifdata!=data:# Forgotten 'self'raiseException("Invalid data!")#Fixed versionclassCustomer:def__init__(self,data):self.data=datadefcheck_data(self,data):ifself.data!=data:raiseException("Invalid data!")
References¶
Python Language Reference:Comparisons.
Common Weakness Enumeration:CWE-570.
Common Weakness Enumeration:CWE-571.