Skip to content

Commit 3c6fc86

Browse files
author
Brian King
committed
Check for getter by member reference
1 parent 532c006 commit 3c6fc86

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,7 +2302,7 @@ class VarDeclUsageChecker : public ASTWalker {
23022302
const VarDecl *AssociatedGetter = nullptr;
23032303

23042304
/// The first reference to the associated getter.
2305-
const DeclRefExpr *AssociatedGetterDeclRef = nullptr;
2305+
const Expr *AssociatedGetterRefExpr = nullptr;
23062306

23072307
/// This is a mapping from VarDecls to the if/while/guard statement that they
23082308
/// occur in, when they are in a pattern in a StmtCondition.
@@ -2569,7 +2569,7 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
25692569
if (FD && FD->getAccessorKind() == AccessorKind::Set) {
25702570
auto getter = dyn_cast<VarDecl>(FD->getStorage());
25712571
if ((access & RK_Read) == 0 && AssociatedGetter == getter) {
2572-
if (auto DRE = AssociatedGetterDeclRef) {
2572+
if (auto DRE = AssociatedGetterRefExpr) {
25732573
Diags.diagnose(DRE->getLoc(), diag::unused_setter_parameter,
25742574
var->getName());
25752575
Diags.diagnose(DRE->getLoc(), diag::fixit_for_unused_setter_parameter,
@@ -2864,10 +2864,18 @@ std::pair<bool, Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
28642864
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
28652865
addMark(DRE->getDecl(), RK_Read);
28662866

2867-
// If the Decl is a read of a getter, track the first DRE for diagnostics
2867+
// If the Expression is a read of a getter, track for diagnostics
28682868
if (auto VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2869-
if (AssociatedGetter == VD && AssociatedGetterDeclRef == nullptr)
2870-
AssociatedGetterDeclRef = DRE;
2869+
if (AssociatedGetter == VD && AssociatedGetterRefExpr == nullptr)
2870+
AssociatedGetterRefExpr = DRE;
2871+
}
2872+
}
2873+
// If the Expression is a member reference, see if it is a read of the getter
2874+
// to track for diagnostics.
2875+
if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
2876+
if (auto VD = dyn_cast<VarDecl>(MRE->getMember().getDecl())) {
2877+
if (AssociatedGetter == VD && AssociatedGetterRefExpr == nullptr)
2878+
AssociatedGetterRefExpr = MRE;
28712879
}
28722880
}
28732881
// If this is an AssignExpr, see if we're mutating something that we know

test/decl/var/usage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ func sr964() {
287287
print(suspiciousSetter) // expected-warning {{setter argument 'newValue' was never used, but the property was accessed}} expected-note {{did you mean to use 'newValue' instead of accessing the property's current value?}} {{13-29=newValue}}
288288
}
289289
}
290+
struct MemberGetter {
291+
var suspiciousSetter: String {
292+
get { return "" }
293+
set {
294+
print(suspiciousSetter) // expected-warning {{setter argument 'newValue' was never used, but the property was accessed}} expected-note {{did you mean to use 'newValue' instead of accessing the property's current value?}} {{15-31=newValue}}
295+
}
296+
}
297+
}
290298
var namedSuspiciousSetter: String {
291299
get { return "" }
292300
set(parameter) {

0 commit comments

Comments
 (0)