Skip to content

Commit 68a903e

Browse files
committed
Fix crash when analyzing anonymous lambda fns
1 parent ba2f02d commit 68a903e

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

clang-tools-extra/clang-tidy/bugprone/NullCheckAfterDereferenceCheck.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ void NullCheckAfterDereferenceCheck::registerMatchers(MatchFinder *Finder) {
124124
// well supported by the check.
125125
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
126126
hasBody(containsPointerValue)),
127-
cxxConstructorDecl(hasAnyConstructorInitializer(
128-
withInitializer(containsPointerValue)))))
127+
cxxConstructorDecl(
128+
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
129+
hasAnyConstructorInitializer(
130+
withInitializer(containsPointerValue)))))
129131
.bind(FuncID),
130132
this);
131133
}

clang-tools-extra/test/clang-tidy/checkers/bugprone/null-check-after-dereference.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,46 @@ struct S {
44
int a;
55
};
66

7-
int warning_deref(int *p) {
7+
void warning_deref(int *p) {
88
*p = 42;
99

1010
if (p) {
1111
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point [bugprone-null-check-after-dereference]
1212
// CHECK-MESSAGES: :[[@LINE-4]]:3: note: one of the locations where the pointer's value cannot be null
1313
// FIXME: If there's a direct path, make the error message more precise, ie. remove `one of the locations`
1414
*p += 20;
15-
return *p;
16-
} else {
17-
return 0;
1815
}
1916
}
2017

21-
int warning_member(S *q) {
18+
void warning_member(S *q) {
2219
q->a = 42;
2320

2421
if (q) {
2522
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point
2623
// CHECK-MESSAGES: :[[@LINE-4]]:3: note: one of the locations where the pointer's value cannot be null
2724
q->a += 20;
28-
return q->a;
29-
} else {
30-
return 0;
3125
}
3226
}
3327

34-
int negative_warning(int *p) {
28+
void negative_warning(int *p) {
3529
*p = 42;
3630

3731
if (!p) {
3832
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: pointer value is checked even though it cannot be null at this point
3933
// CHECK-MESSAGES: :[[@LINE-4]]:3: note: one of the locations where the pointer's value cannot be null
40-
return 0;
41-
} else {
42-
*p += 20;
43-
return *p;
34+
return;
4435
}
36+
37+
*p += 20;
4538
}
4639

47-
int no_warning(int *p, bool b) {
40+
void no_warning(int *p, bool b) {
4841
if (b) {
4942
*p = 42;
5043
}
5144

5245
if (p) {
53-
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point
5446
*p += 20;
55-
return *p;
56-
} else {
57-
return 0;
5847
}
5948
}
6049

@@ -195,18 +184,6 @@ int chained_references(int *a, int *b, int *c, int *d, int *e) {
195184
}
196185

197186
if (c) {
198-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point
199-
// CHECK-MESSAGES: :[[@LINE-5]]:5: note: one of the locations where the pointer's value cannot be null
200-
*d = 42;
201-
}
202-
203-
if (d) {
204-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point
205-
// CHECK-MESSAGES: :[[@LINE-5]]:5: note: one of the locations where the pointer's value cannot be null
206-
*e = 42;
207-
}
208-
209-
if (e) {
210187
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: pointer value is checked even though it cannot be null at this point
211188
// CHECK-MESSAGES: :[[@LINE-5]]:5: note: one of the locations where the pointer's value cannot be null
212189
return *a;
@@ -282,6 +259,12 @@ int cxx17_crash(int *p) {
282259
return 0;
283260
}
284261

262+
// In an earlier version, the check would crash when encountering anonymous lambdas.
263+
void lambda_crash(int *p) {
264+
auto f = [p](){ *p = 42; };
265+
f();
266+
}
267+
285268
int note_tags() {
286269
// FIXME: Note tags are not appended to declarations
287270
int *ptr = nullptr;

0 commit comments

Comments
 (0)