llvm / llvm/llvm-project

[objc] No warning for Obj-C 'fast enumeration' `for` loops that never actually loop (a la `Wunreachable-code-loop-increment`)

Open
#158,729 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics objective-c
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

For the following C:

```c
int x = 5;
for (int i = 0; i < x; i++)
{
break;
}
```

Clang warns: `warning: loop will run at most once (loop increment never executed) [-Wunreachable-code-loop-increment]`

Nice! See also: https://godbolt.org/z/7cq8errEa

--------

However the following Objective-C alas gives no warnings (confirmed with -Weverything):

```objc
#import

int main (void)
{
NSArray* a = @[@1, @2, @3];
for (NSNumber* n in a)
{
NSLog(@"%@", n);
break;
}

return 0;
}
```

This is of course a reduced triviality, but such a warning would have found a real bug in a real codebase.

(The static analyzer also gives no warnings.)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Objective-C fast-enumeration example with Clang and compare it with the C loop that emits -Wunreachable-code-loop-increment. Trace the warning behavior for loops containing break, including under -Weverything, and add coverage for the missing diagnostic. Done means the Objective-C case warns without introducing a warning for valid looping code.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, objective-c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.