mirror of
https://github.com/github/codeql.git
synced 2026-04-29 18:55:14 +02:00
Python: autoformat (4 spaces) NonCls.ql NonSelf.ql
This commit is contained in:
@@ -16,7 +16,8 @@ import python
|
||||
|
||||
predicate first_arg_cls(Function f) {
|
||||
exists(string argname | argname = f.getArgName(0) |
|
||||
argname = "cls" or
|
||||
argname = "cls"
|
||||
or
|
||||
/* Not PEP8, but relatively common */
|
||||
argname = "mcls"
|
||||
)
|
||||
@@ -27,21 +28,21 @@ predicate is_type_method(Function f) {
|
||||
}
|
||||
|
||||
predicate classmethod_decorators_only(Function f) {
|
||||
forall(Expr decorator |
|
||||
decorator = f.getADecorator() |
|
||||
decorator.(Name).getId() = "classmethod")
|
||||
forall(Expr decorator | decorator = f.getADecorator() | decorator.(Name).getId() = "classmethod")
|
||||
}
|
||||
|
||||
from Function f, string message
|
||||
where (f.getADecorator().(Name).getId() = "classmethod" or is_type_method(f)) and
|
||||
not first_arg_cls(f) and classmethod_decorators_only(f) and
|
||||
not f.getName() = "__new__" and
|
||||
(
|
||||
if exists(f.getArgName(0)) then
|
||||
message = "Class methods or methods of a type deriving from type should have 'cls', rather than '" +
|
||||
f.getArgName(0) + "', as their first parameter."
|
||||
else
|
||||
message = "Class methods or methods of a type deriving from type should have 'cls' as their first parameter."
|
||||
)
|
||||
|
||||
where
|
||||
(f.getADecorator().(Name).getId() = "classmethod" or is_type_method(f)) and
|
||||
not first_arg_cls(f) and
|
||||
classmethod_decorators_only(f) and
|
||||
not f.getName() = "__new__" and
|
||||
(
|
||||
if exists(f.getArgName(0))
|
||||
then
|
||||
message = "Class methods or methods of a type deriving from type should have 'cls', rather than '"
|
||||
+ f.getArgName(0) + "', as their first parameter."
|
||||
else
|
||||
message = "Class methods or methods of a type deriving from type should have 'cls' as their first parameter."
|
||||
)
|
||||
select f, message
|
||||
|
||||
@@ -21,32 +21,34 @@ predicate is_type_method(FunctionValue fv) {
|
||||
}
|
||||
|
||||
predicate used_in_defining_scope(FunctionValue fv) {
|
||||
exists(Call c |
|
||||
c.getScope() = fv.getScope().getScope() and c.getFunc().pointsTo(fv)
|
||||
)
|
||||
exists(Call c | c.getScope() = fv.getScope().getScope() and c.getFunc().pointsTo(fv))
|
||||
}
|
||||
|
||||
from Function f, FunctionValue fv, string message
|
||||
where
|
||||
exists(ClassValue cls, string name |
|
||||
cls.declaredAttribute(name) = fv and cls.isNewStyle() and
|
||||
not name = "__new__" and
|
||||
not name = "__metaclass__" and
|
||||
/* declared in scope */
|
||||
f.getScope() = cls.getScope()
|
||||
) and
|
||||
not f.getArgName(0) = "self" and
|
||||
not is_type_method(fv) and
|
||||
fv.getScope() = f and
|
||||
not f.getName() = "lambda" and
|
||||
not used_in_defining_scope(fv) and
|
||||
(
|
||||
if exists(f.getArgName(0)) then
|
||||
message = "Normal methods should have 'self', rather than '" + f.getArgName(0) + "', as their first parameter."
|
||||
else
|
||||
message = "Normal methods should have at least one parameter (the first of which should be 'self')." and
|
||||
not f.hasVarArg()
|
||||
) and
|
||||
not fv instanceof ZopeInterfaceMethodValue
|
||||
|
||||
exists(ClassValue cls, string name |
|
||||
cls.declaredAttribute(name) = fv and
|
||||
cls.isNewStyle() and
|
||||
not name = "__new__" and
|
||||
not name = "__metaclass__" and
|
||||
/* declared in scope */
|
||||
f.getScope() = cls.getScope()
|
||||
) and
|
||||
not f.getArgName(0) = "self" and
|
||||
not is_type_method(fv) and
|
||||
fv.getScope() = f and
|
||||
not f.getName() = "lambda" and
|
||||
not used_in_defining_scope(fv) and
|
||||
(
|
||||
(
|
||||
if exists(f.getArgName(0))
|
||||
then
|
||||
message = "Normal methods should have 'self', rather than '" + f.getArgName(0) +
|
||||
"', as their first parameter."
|
||||
else
|
||||
message = "Normal methods should have at least one parameter (the first of which should be 'self')."
|
||||
) and
|
||||
not f.hasVarArg()
|
||||
) and
|
||||
not fv instanceof ZopeInterfaceMethodValue
|
||||
select f, message
|
||||
|
||||
Reference in New Issue
Block a user