This is an archive of the discontinued LLVM Phabricator instance.

Objective-C literals to bool conversion warning
ClosedPublic

Authored by rtrieu on Jan 23 2014, 2:43 PM.

Details

Reviewers
rtrieu
Summary

Warn when a Objective-C literal is converted to a boolean. This warns on NSNumber*, NSString*, NSArray* and NSDictionary* literals. Excludes objc_yes and objc_no.

I don't normally work with Objective-C so let me know if I am missing any test cases.

Diff Detail

Event Timeline

Huh, I thought we already had a check for this. This seems fine, but we might as well test every Objective-C literal type. None of them can evaluate to nil...and then I'd rather group the @"" conversion with those, rather than -Wstring-conversion.

Is there a list of Objective C literal types that this new warning should cover?

Besides strings, anything on this page: http://clang.llvm.org/docs/ObjectiveCLiterals.html

That corresponds to ObjCStringLiteral, ObjCBoxedExpr, ObjCArrayLiteral, and ObjCDictionaryLiteral...but not ObjCBoolLiteral (which is just __objc_yes and __objc_no). -Wobjc-literal-conversion, perhaps?

(I suppose you could throw ObjCSelectorExpr in there as well, though that one probably deserves custom text; the others are all "Objective-C object literals". ObjCEncodeExpr just expands to a C string, so that could go under -Wstring-conversion if you cared.)

rtrieu updated this revision to Unknown Object (????).Jan 27 2014, 5:11 PM

Remember, "Objective-C" is always hyphenated. :-)

include/clang/Basic/DiagnosticSemaKinds.td
2299–2301 ↗(On Diff #6701)

Ideally I'd like something more like "implicit boolean conversion of %select{array literal|dictionary literal|numeric literal|boxed expression}0 always evaluates to true". Failing that, let's go with "Objective-C object literal".

lib/Sema/SemaChecking.cpp
5338–5339

There are a lot of "Objective-C types". How about "This covers the literal expressions that evaluate to Objective-C objects"?

rtrieu updated this revision to Unknown Object (????).Jan 28 2014, 2:37 PM

Hyphenate Objective-C, fix warning text, fix comment text.

Justification for dropping the "boolean" in "implicit boolean conversion"? I'm worried that a non-compiler-writer looking at the code won't see where the conversion is. (I didn't phrase it as "conversion...to bool" because Objective-C programmers don't usually think about "bool", even though they're probably compiling in gnu99 mode.)

Justification for dropping the "boolean" in "implicit boolean conversion"? I'm worried that a non-compiler-writer looking at the code won't see where the conversion is. (I didn't phrase it as "conversion...to bool" because Objective-C programmers don't usually think about "bool", even though they're probably compiling in gnu99 mode.)

Oops, dropped it while copying the text. I'll go fix the text.

rtrieu updated this revision to Unknown Object (????).Jan 28 2014, 3:04 PM

"implicit conversion" -> "implicit boolean conversion"

Looks good to me. I think we can even turn it on by default. Thanks, Richard!

rtrieu accepted this revision.Jan 28 2014, 3:48 PM
rtrieu closed this revision.

Committed at r200356. Switched to on by default instead of DefaultIgnore. One analysis test required disabling this warning.