blob: 2257e8fb0af7c48e0424db3d1dcd96d6b0cf05e1 [file] [log] [blame]
Martin Brænne2a63b442023-04-03 08:01:17 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5// Tests for the treatment of the `this` pointer (which is always nonnull).
6#include <optional>
7#include <set>
8#include <string>
9
Googler7f19b2b2023-05-01 09:44:57 -070010#include "nullability/test/check_diagnostics.h"
Martin Brænne2a63b442023-04-03 08:01:17 -070011#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
12
Sam McCall4f6be422023-06-27 02:51:22 -070013namespace clang::tidy::nullability {
Martin Brænne2a63b442023-04-03 08:01:17 -070014namespace {
15
Martin Brænne834d4b82023-05-15 07:07:05 -070016TEST(PointerNullabilityTest, ImplicitThis) {
Martin Brænne2a63b442023-04-03 08:01:17 -070017 EXPECT_TRUE(checkDiagnostics(R"cc(
18 struct Foo {
19 void foo();
Martin Brænne834d4b82023-05-15 07:07:05 -070020 void target() {
21 __assert_nullability<NK_nonnull>(this);
22 foo();
23 }
Martin Brænne2a63b442023-04-03 08:01:17 -070024 };
25 )cc"));
Martin Brænne834d4b82023-05-15 07:07:05 -070026}
Martin Brænne2a63b442023-04-03 08:01:17 -070027
Martin Brænne834d4b82023-05-15 07:07:05 -070028TEST(PointerNullabilityTest, ExplicitThis) {
Martin Brænne2a63b442023-04-03 08:01:17 -070029 // (->) explicit `this`
30 EXPECT_TRUE(checkDiagnostics(R"cc(
31 struct Foo {
32 void foo();
33 void target() { this->foo(); }
34 };
35 )cc"));
36}
37
Martin Brænne834d4b82023-05-15 07:07:05 -070038TEST(PointerNullabilityTest, ClassWithPointerTemplateArg) {
39 EXPECT_TRUE(checkDiagnostics(R"cc(
40 template <class T>
41 struct S;
42 template <>
Sam McCall7d9afee2023-06-27 01:43:24 -070043 struct S<int *_Nullable> {
Martin Brænne834d4b82023-05-15 07:07:05 -070044 void target() {
45 // `_Nullable` in the specialization is bogus: we can't specialize on
46 // nullability as it's just sugar. Therefore the correct inner
47 // nullability here is "unspecified".
48 __assert_nullability<NK_nonnull, NK_unspecified>(this);
49 }
50 };
51 )cc"));
52}
53
Martin Brænne2a63b442023-04-03 08:01:17 -070054} // namespace
Sam McCall4f6be422023-06-27 02:51:22 -070055} // namespace clang::tidy::nullability