Skip to content

Commit 011f806

Browse files
committed
remove redundant reference check and add one more test
1 parent 9608e95 commit 011f806

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,14 @@ void handleConstMemberCall(const CallExpr *CE,
586586
if (DirectCallee == nullptr)
587587
return;
588588

589-
bool isReference =
590-
DirectCallee->getReturnType().getTypePtr()->isReferenceType();
591-
if (isReference) {
592-
StorageLocation &Loc =
593-
State.Lattice.getOrCreateConstMethodReturnStorageLocation(
594-
*RecordLoc, DirectCallee, State.Env, [&](StorageLocation &Loc) {
595-
// no-op
596-
});
589+
StorageLocation &Loc =
590+
State.Lattice.getOrCreateConstMethodReturnStorageLocation(
591+
*RecordLoc, DirectCallee, State.Env, [&](StorageLocation &Loc) {
592+
// no-op
593+
});
597594

598-
State.Env.setStorageLocation(*CE, Loc);
599-
return;
600-
}
595+
State.Env.setStorageLocation(*CE, Loc);
596+
return;
601597
}
602598

603599
// Cache if the const method returns a boolean or pointer type.

clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,7 +4010,7 @@ TEST_P(UncheckedOptionalAccessTest,
40104010

40114011
TEST_P(
40124012
UncheckedOptionalAccessTest,
4013-
ConstRefAccessorToOptionalViaNonConstRefAccessorToHoldingObjectWithModAfterCheck) {
4013+
ConstRefAccessorToOptionalViaConstRefAccessorToHoldingObjectWithModAfterCheck) {
40144014
ExpectDiagnosticsFor(R"cc(
40154015
#include "unchecked_optional_access_test.h"
40164016
@@ -4027,7 +4027,7 @@ TEST_P(
40274027
40284028
A& getA() { return a; }
40294029
4030-
void clear() { a = A{}; };
4030+
void clear() { a = A{}; }
40314031
40324032
private:
40334033
A a;
@@ -4049,6 +4049,41 @@ TEST_P(
40494049
)cc");
40504050
}
40514051

4052+
4053+
TEST_P(
4054+
UncheckedOptionalAccessTest,
4055+
ConstRefAccessorToOptionalViaConstRefAccessorToHoldingObjectWithAnotherConstCallAfterCheck) {
4056+
ExpectDiagnosticsFor(R"cc(
4057+
#include "unchecked_optional_access_test.h"
4058+
4059+
class A {
4060+
public:
4061+
const $ns::$optional<int>& get() const { return x; }
4062+
private:
4063+
$ns::$optional<int> x;
4064+
};
4065+
4066+
class B {
4067+
public:
4068+
const A& getA() const { return a; }
4069+
4070+
void callWithoutChanges() const {
4071+
// no-op
4072+
}
4073+
4074+
private:
4075+
A a;
4076+
};
4077+
4078+
void target(B& b) {
4079+
if (b.getA().get().has_value()) {
4080+
b.callWithoutChanges(); // calling const method which cannot change A
4081+
b.getA().get().value();
4082+
}
4083+
}
4084+
)cc");
4085+
}
4086+
40524087
// FIXME: Add support for:
40534088
// - constructors (copy, move)
40544089
// - assignment operators (default, copy, move)

0 commit comments

Comments
 (0)