llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.15k stars 12.03k forks source link

clang and analyzer don't complain about conditions that are always true #18424

Open llvmbot opened 10 years ago

llvmbot commented 10 years ago
Bugzilla Link 18050
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @AnnaZaks,@belkadan,@rnk,@Weverything

Extended Description

static NSString *const bla = @"bla";

if (bla) NSLog(@"always true");

if (@"blabla") NSLog(@"always true");

both conditions don't generate a warning with clang even with -Weverything and even the static analyzer doesn't complain although the conditions are bound to be always true.

Weverything commented 10 years ago

r200356 now makes Clang catch Objective-C object literal to bool conversions. On by default, or use -Wobjc-literal-conversion. So this will warn on the example of:

if (@"blabla")

Weverything commented 10 years ago

http://llvm-reviews.chandlerc.com/D2608

This is a patch to extend -Wstring-conversion to catch "if(@"blabla")"

llvmbot commented 10 years ago

shouldn't Wstring-conversion also catch the second case?

"Due to that, -Wstring-conversion will warn when a string literal is converted to a true boolean value. "

only works for C strings and not ObjC strings?

0f73b9cf-134f-41af-a8b1-14d9f305ee95 commented 10 years ago

Wouldn't this be better suited for a compiler warning?

belkadan commented 10 years ago

The static analyzer isn't usually very good at answering questions that are true on /all/ paths; it's better at finding /any/ path. But we can probably contrive a way to handle the first case, and the second case could probably be implemented just with the constant evaluator.

llvmbot commented 10 years ago

assigned to @tkremenek