linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Unreachable code diagnostic
@ 2017-02-24 18:07 Matthew Wilcox
  2017-02-24 18:33 ` Linus Torvalds
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Matthew Wilcox @ 2017-02-24 18:07 UTC (permalink / raw)
  To: linux-sparse

I was recently sent some code that looked like this:

int foo()
{
	lock();
	return bar();
	unlock();
}

When you're restructuring code that contains locks, this is a
*really* easy mistake to make.  I've done it myself.  But there's no
compiler warning for it!  gcc doesn't have it, sparse doesn't have it.
I've mentioned it to the gcc developers and they don't seem terribly
enthusiastic (they had a -Wunreachable-code at one point, but it
got disabled, probably due to too many false positives like their
-Wmaybe-uninitialized).

Maybe sparse could warn about code after an unconditional return
statement?  I wouldn't like to see it warn about code after a conditional
return statement where the condition is always true; I think that would
have a lot of false positives due to macros.  For example, something
like this:

int foo()
{
	int i = 0;
	for (;;) {
		if (i == FOO_MAX)
			return i;
		bar(i++);
	}
}

if FOO_MAX happens to be 0 should silently optimise to 'return 0' and
not emit a warning.

I would like to see it warn in this case:

int foo()
{
	return bar();
	do { } while (0);
}

As that can be generated by the preprocessor in the case of optimising
away locks for the !SMP case, for example.

Any takers for this idea?  :-)

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-02-24 21:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-24 18:07 Unreachable code diagnostic Matthew Wilcox
2017-02-24 18:33 ` Linus Torvalds
2017-02-24 20:11   ` Luc Van Oostenryck
2017-02-24 20:43     ` Josh Triplett
2017-02-24 20:47       ` Luc Van Oostenryck
2017-02-24 18:57 ` Josh Triplett
2017-02-24 19:56   ` Luc Van Oostenryck
2017-02-24 21:05   ` Matthew Wilcox
2017-02-24 19:26 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).