Skip to content

Warn if the getter is used, but the setter argument is not used#11465

Merged
jrose-apple merged 6 commits intoswiftlang:masterfrom
KingOfBrian:bugfix/SR-964
Sep 27, 2017
Merged

Warn if the getter is used, but the setter argument is not used#11465
jrose-apple merged 6 commits intoswiftlang:masterfrom
KingOfBrian:bugfix/SR-964

Conversation

@KingOfBrian
Copy link
Copy Markdown
Contributor

This PR generates a warning if a setters getter is used, but the setter argument is not used. This is a common bug that occurs when a custom setter is implemented, but the getter is accidentally used instead of the setter argument.

This PR also removes some code that added method parameters to the checker. It was only used to warn about var parameters that were not mutated. Since var parameter support was dropped in 3.0, this was just busy work.

This was a rebase off of this PR that wasn't cooperating with CI.

Resolves SR-964.

@milseman
Copy link
Copy Markdown
Member

@swift-ci please test

@milseman
Copy link
Copy Markdown
Member

@swift-ci please test

@milseman
Copy link
Copy Markdown
Member

@swift-ci please smoke test

@milseman
Copy link
Copy Markdown
Member

@shahmishal is @swift-ci doing ok? It seems to bear a grudge against @KingOfBrian

@shahmishal
Copy link
Copy Markdown
Member

@milseman It should be fixed now.

@swift-ci please test

@swift-ci
Copy link
Copy Markdown
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 990f03d

@swift-ci
Copy link
Copy Markdown
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 990f03d

@KingOfBrian
Copy link
Copy Markdown
Contributor Author

Looks like one of the checks isn't reporting, and I don't think the two failures are related to this change. Really excited for this warning and would love to get it it 4.1 !

@milseman
Copy link
Copy Markdown
Member

CC @gottesmm @shahmishal that failure is the futimens one. In case you're still trying to track it down: https://ci.swift.org/job/swift-PR-osx/86/console

/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm/lib/Support/Unix/Path.inc:597:7: error: 'futimens' is only available on macOS 10.13 or newer [-Werror,-Wunguarded-availability-new]

@milseman
Copy link
Copy Markdown
Member

@swift-ci please test OS X Platform

@marcelofabri
Copy link
Copy Markdown
Contributor

Is something blocking this from being merged? I'd love to see it on 4.1 as well 🎉

@milseman
Copy link
Copy Markdown
Member

milseman commented Sep 4, 2017

Did this ever get reviewed?

@milseman milseman requested a review from jrose-apple September 5, 2017 18:11
@milseman
Copy link
Copy Markdown
Member

milseman commented Sep 5, 2017

CC @jrose-apple. If you have time, could you review this change?

Copy link
Copy Markdown
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation feels a bit funny to me, since that map is normally for stored properties, but I can see how it works nicely with the existing tracking. So my only remaining concern is with the phrasing of the diagnostic.

Comment thread include/swift/AST/DiagnosticsSema.def Outdated
(Identifier))
WARNING(unused_setter_newvalue, none,
"setter argument %0 was never used, but the getter was accessed; "
"consider using %0 or removing %1",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "removing" phrasing is confusing. How about something like "setter argument %0 was never used; did you mean to use it instead of accessing the property's current value"?

Ideally, we'd put the warning on one of the uses of the getter, and maybe even have a fix-it if it's just one use, but that's probably more work than you want to do right now.

@CodaFi
Copy link
Copy Markdown
Contributor

CodaFi commented Sep 23, 2017

Well, that’s not... quite a rebase.

@KingOfBrian
Copy link
Copy Markdown
Contributor Author

Hah! was hoping to fix this before anyone noticed. Me and git didn't get along last night. I think my initial rebase was off... and things escalated. I'll clean it up shortly!

@KingOfBrian
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback @jrose-apple -- I added the fixit, and moved where the error was reported. I track the first DeclRefExpr to use the getter for the diagnostic, which let me drop the RK_DoNotReport.

Comment thread lib/Sema/MiscDiagnostics.cpp Outdated

/// This is a mapping from a VarDecl property getter to the expression that
/// used it.
llvm::SmallDenseMap<VarDecl*, DeclRefExpr*> GetterDeclUsageMap;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This map will only ever have one entry, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea -- sort of funny, I woke up this morning and that was literally my first thought. I converted it to a pair, but I'm not sure if that's a normal C++ construct to use. Would that make sense to you?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would just use two instance variables, and not worry about keeping them tied together. You also set each half at a different point in execution anyway.

(Also, may as well make them const.)

Comment thread test/decl/var/usage.swift Outdated
var suspiciousSetter: String {
get { return "" }
set {
print(suspiciousSetter) // expected-warning {{setter argument 'newValue' was never used; did you mean to use it instead of accessing the property's current value?}} expected-note {{do you want to replace 'suspiciousSetter' with 'newValue'?}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to have the note, I think the "did you mean" part belongs in the note, rather than describing the fix-it. Also, you can test fix-its with the expected-* syntax! Use {{1-2=foo}} syntax after the diagnostic text to mean "replace columns 1 to 2 with 'foo'".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that makes sense. I thought a note was more appropriate since the fixit isn't always the correct course of action. I can just do the warning if you think that makes more sense.

Otherwise, does this language look good?

warning: setter argument 'newValue' was never used, but the property was accessed
note: did you mean to use 'newValue' instead of accessing the property's current value?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that seems fine, and I agree that the separate note is a good idea.

@jrose-apple
Copy link
Copy Markdown
Contributor

@swift-ci Please test

@jrose-apple jrose-apple self-assigned this Sep 27, 2017
@swift-ci
Copy link
Copy Markdown
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 93d1462ccdff17e7bcdc5c51174bb25346266a44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants