On 23/09/2025 16:40, Yair Lenga via Gcc wrote:
It’s a little bit of both (error and style). Gcc does warn about
‘set-and-not-used’, and similar, which are technically valid, but in
many cases indication of wasted work. Same here - positioning the
strlen inside the condition, can lead to lot of unnecessary work.


It is a little different, I think. Set and not used variables are not extra work at run-time - the compiler will optimise away the calculations. But they are very likely to be the result of a mistake in the code. So they are not really a style issue - the warning is primarily helping the developer spot their bugs.

In the case of strlen inside a for loop (or its controlling expressions), it is likely that the programmer wrote the code as they intended, and it is not a bug - it is more of a style issue or a lack of understanding on the programmer's part.

Given pattern is easy to detect (I think) - suggesting putting it
into the compiler. Static analysis code can go deeper - identify
parts of loop body (and condition) that can be extracted from the
loop and executed once.


I agree with this. A compiler is not a complete static analysis solution - that is beyond its scope. But it can do quite a lot of static analysis, and it is a tool to aid developers as well as compile code. So if a warning here can be added to gcc without too much work, then it will help developers - after all, C developers don't all use separate static analysis tools, but they do all use compilers! (Unfortunately, not all of them use the warnings that are available easily and freely from the compiler. Maybe gcc should enable "-Wall -Wextra" by default, unless the option "-i-laugh-in-the-face-of-bugs" is active :-)


Reply via email to