This is an archive of the discontinued LLVM Phabricator instance.

[clang] 'unused-but-set-variable' warning should not apply to __attribute__((objc_precise_lifetime) Objective-C pointers

Authored by mwyman on Feb 22 2022, 5:07 PM.



The objc_precise_lifetime attribute is applied to Objective-C pointers to ensure the optimizer does not prematurely release an object under Automatic Reference Counting (ARC). It is a common enough pattern to assign values to these variables but not reference them otherwise, and annotating them with __unused is not really correct as they are being used to ensure an object's lifetime.

Diff Detail

Event Timeline

mwyman requested review of this revision.Feb 22 2022, 5:07 PM
mwyman created this revision.
stephanemoore accepted this revision.Feb 22 2022, 5:24 PM

Make sense to me.

I don't know offhand who might be a good second pair of eyes to take a look.


Is it worth adding some cases with nested scopes? I think that's a common scenario where objc_precise_lifetime is often used?

This revision is now accepted and ready to land.Feb 22 2022, 5:24 PM

Maybe ahatanak might be a good reviewer based on

dmaclach accepted this revision.Feb 22 2022, 5:48 PM
dmaclach added inline comments.

what happens in the case that x is never assigned to anything? Do we still get a warning?

mwyman added inline comments.Feb 22 2022, 7:04 PM

The code implementing -Wunused-variable checking already explicitly excludes objc_precise_lifetime-attributed variables:

mwyman marked an inline comment as done.Feb 22 2022, 7:04 PM
mwyman updated this revision to Diff 410863.Feb 23 2022, 10:39 AM

Added new inner scope for test file.

mwyman marked an inline comment as done.Feb 23 2022, 10:39 AM
mbenfield accepted this revision.Feb 23 2022, 11:34 AM
This revision was landed with ongoing or failed builds.Feb 24 2022, 2:26 PM
This revision was automatically updated to reflect the committed changes.