Fix formatting and rewrite redundant exists

This commit is contained in:
Joe Farebrother
2025-01-27 16:58:19 +00:00
parent 526e235fc1
commit fa76bf3c9f
2 changed files with 16 additions and 17 deletions

View File

@@ -14,6 +14,8 @@ private predicate isMetaclass(Class c) {
/** Holds if `f` is a class method. */
private predicate isClassMethod(Function f) {
f.getADecorator() = API::builtin("classmethod").asSource().asExpr()
or
f.getName() in ["__new__", "__init_subclass__", "__metaclass__", "__class_getitem__"]
}
/** Holds if `f` is a static method. */
@@ -37,8 +39,7 @@ predicate shouldBeSelf(Function f, Class c) {
methodOfClass(f, c) and
not isStaticMethod(f) and
not isClassMethod(f) and
not f.getName() in ["__new__", "__init_subclass__", "__metaclass__", "__class_getitem__"] and
isMetaclass(c) and
not isMetaclass(c) and
not isZopeInterface(c)
}
@@ -47,9 +48,9 @@ predicate shouldBeCls(Function f, Class c) {
methodOfClass(f, c) and
not isStaticMethod(f) and
(
isClassMethod(f)
isClassMethod(f) and not isMetaclass(c)
or
f.getName() in ["__new__", "__init_subclass__", "__metaclass__", "__class_getitem__"]
isMetaclass(c) and not isClassMethod(f)
)
}
@@ -68,12 +69,12 @@ predicate firstArgNamedCls(Function f) {
/** Holds if the first parameter of `f` should be named `self`, but isn't. */
predicate firstArgShouldBeNamedSelfAndIsnt(Function f) {
exists(Class c | shouldBeSelf(f, c)) and
shouldBeSelf(f, _) and
not firstArgNamedSelf(f)
}
/** Holds if the first parameter of `f` should be named `cls`, but isn't. */
predicate firstArgShouldBeNamedClsAndIsnt(Function f) {
exists(Class c | shouldBeCls(f, c)) and
shouldBeCls(f, _) and
not firstArgNamedCls(f)
}

View File

@@ -20,15 +20,13 @@ from Function f, string message
where
firstArgShouldBeNamedSelfAndIsnt(f) 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
)
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')."
)
select f, message