Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 380749 - Bug in codan "no return" warning.
Summary: Bug in codan "no return" warning.
Status: CLOSED DUPLICATE of bug 394521
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: Next   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-26 06:23 EDT by Andrey Eremchenko CLA
Modified: 2016-12-22 00:20 EST (History)
4 users (show)

See Also:


Attachments
For if(constant), skip the warning for a missing return value when possible (5.21 KB, patch)
2012-07-05 20:15 EDT, Emmanuel Proulx CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Eremchenko CLA 2012-05-26 06:23:24 EDT
Build Identifier: Version: 4.2.0 Build id: I20120503-1800

Codan shows wrong warning "No return, in function returning non-void" for this code:

int func()
{
  if (true)
    return 0;
}

Actually this "if" is generated by expanding some our macro for tracing purposes. And there is "if (!(false))" in the expanded code.
Have to swtich off these very annoying false warnings from Codan.

Reproducible: Always
Comment 1 Emmanuel Proulx CLA 2012-07-05 20:15:12 EDT
Created attachment 218360 [details]
For if(constant), skip the warning for a missing return value when possible

This is a proposal for fixing the bug 380749.

This applies when there's a constant in the if(). In which case we can determine the if() will always be either true or false. When that's the case, we can skip the warning for a missing return value for the then or else that's unused.
Comment 2 Elena Laskavaia CLA 2012-07-06 09:54:47 EDT
I did not know cdt has facilities to do constant folding, that is cool

For control flow it is not that simple
a) if code is dead code it should be added to list of root nodes for dead code list
b) parts of dead code technically can have goto statements and life code would go there, so you have to process the code but do not link it to if branch

And there is bug about 
while (1) {
...
}

which is more likely to occur in code it would be nice to do that too
Comment 3 Emmanuel Proulx CLA 2012-07-06 13:37:55 EDT
(In reply to comment #2)
> I did not know cdt has facilities to do constant folding, that is cool
> 
> For control flow it is not that simple
> a) if code is dead code it should be added to list of root nodes for dead code
> list
> b) parts of dead code technically can have goto statements and life code would
> go there, so you have to process the code but do not link it to if branch
> 
> And there is bug about 
> while (1) {
> ...
> }
> 
> which is more likely to occur in code it would be nice to do that too

I'm pretty sure those problems would occur with or without the fix I made. I think we should create other bugs for those issues.

About the loops, I was thinking the same as you. For example, this should not give the warning either:

int loop() {
  while(1) {
   //do something
    return 0;
  }
}
Comment 4 Emmanuel Proulx CLA 2012-07-17 08:21:20 EDT
After our conversation I went back to the code to check that "dead code" functionality you explained to me. I agree with you that the part that's never executed is considered dead code. I had a look at it. It does sound like we could just add the code to the dead code list like this:

IBranchNode thenNode = factory.createBranchNode(IBranchNode.THEN);
if(processThen) {
	addOutgoing(ifNode, thenNode);
	IBasicBlock then = createSubGraph(thenNode, body.getThenClause());
	addJump(then, mergeNode);
}
else {
	dead.add(thenNode);
}

But this doesn't work. It is as I suspected. The dead code detection is a separate issue. The following code should complain about dead code, but it does not (it complains about missing a return statement instead):
int func() {
	if(0)
		return 0;
	else {
		return 1;
		puts("hello");
	}
}

So I think we should log a different bug about the dead code detection. This isn't simple to fix either. We can talk about it some more.
Comment 5 Nathan Ridge CLA 2014-08-13 02:02:51 EDT
This appears to be fixed by the patch for bug 394521. I think we can close as duplicate.
Comment 6 Nathan Ridge CLA 2016-12-22 00:20:23 EST

*** This bug has been marked as a duplicate of bug 394521 ***