From bc5e0924d21dbbb95889d49c7ff3c0fa53f51cfb Mon Sep 17 00:00:00 2001 From: Taus Date: Wed, 13 Oct 2021 12:27:20 +0000 Subject: [PATCH] Add tests for "implicit `this`" --- ql/test/queries/style/ImplicitThis/Bad.qll | 11 +++++++++++ ql/test/queries/style/ImplicitThis/Good.qll | 16 ++++++++++++++++ .../style/ImplicitThis/ImplicitThis.expected | 1 + .../style/ImplicitThis/ImplicitThis.qlref | 1 + ql/test/queries/style/ImplicitThis/Okay.qll | 13 +++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 ql/test/queries/style/ImplicitThis/Bad.qll create mode 100644 ql/test/queries/style/ImplicitThis/Good.qll create mode 100644 ql/test/queries/style/ImplicitThis/ImplicitThis.expected create mode 100644 ql/test/queries/style/ImplicitThis/ImplicitThis.qlref create mode 100644 ql/test/queries/style/ImplicitThis/Okay.qll diff --git a/ql/test/queries/style/ImplicitThis/Bad.qll b/ql/test/queries/style/ImplicitThis/Bad.qll new file mode 100644 index 00000000000..97b51284acc --- /dev/null +++ b/ql/test/queries/style/ImplicitThis/Bad.qll @@ -0,0 +1,11 @@ +import ql + +class Foo extends string { + Foo() { this = "hello" } + + string getBar() { result = "bar" } + + string getBarWithThis() { result = this.getBar() } + + string getBarWithoutThis() { result = getBar() } +} diff --git a/ql/test/queries/style/ImplicitThis/Good.qll b/ql/test/queries/style/ImplicitThis/Good.qll new file mode 100644 index 00000000000..95b5f17f1de --- /dev/null +++ b/ql/test/queries/style/ImplicitThis/Good.qll @@ -0,0 +1,16 @@ +import ql + +class Foo extends string { + Foo() { this = "hello" } + + string getBar() { result = "bar" } + + string getBarWithThis() { result = this.getBar() } + + /* Okay because not a member predicate. */ + string getBaz() { result = Baz::baz() } +} + +module Baz { + string baz() { result = "baz" } +} diff --git a/ql/test/queries/style/ImplicitThis/ImplicitThis.expected b/ql/test/queries/style/ImplicitThis/ImplicitThis.expected new file mode 100644 index 00000000000..fa3adbaf992 --- /dev/null +++ b/ql/test/queries/style/ImplicitThis/ImplicitThis.expected @@ -0,0 +1 @@ +| Bad.qll:10:41:10:48 | PredicateCall | Use of implicit `this`. | diff --git a/ql/test/queries/style/ImplicitThis/ImplicitThis.qlref b/ql/test/queries/style/ImplicitThis/ImplicitThis.qlref new file mode 100644 index 00000000000..0bdcd3b4b5b --- /dev/null +++ b/ql/test/queries/style/ImplicitThis/ImplicitThis.qlref @@ -0,0 +1 @@ +queries/style/ImplicitThis.ql diff --git a/ql/test/queries/style/ImplicitThis/Okay.qll b/ql/test/queries/style/ImplicitThis/Okay.qll new file mode 100644 index 00000000000..37c9dd4ab2a --- /dev/null +++ b/ql/test/queries/style/ImplicitThis/Okay.qll @@ -0,0 +1,13 @@ +import ql + +class Foo extends string { + Foo() { this = "hello" } + + string getBar() { result = "bar" } + + /* Okay, because we don't write `this.some_method` anywhere */ + string getBarWithoutThis() { result = getBar() } + + /* Okay, because this is the only way to cast `this`. */ + string useThisWithInlineCast() { result = this.(string).toUpperCase() } +}