Python: autoformat (4 spaces) NonCls.ql NonSelf.ql

This commit is contained in:
Rasmus Wriedt Larsen
2019-09-26 13:20:58 +02:00
parent 457794e030
commit 4a5aae0db8
2 changed files with 42 additions and 39 deletions

View File

@@ -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

View File

@@ -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