Community
Participate
Working Groups
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
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.
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
(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; } }
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.
This appears to be fixed by the patch for bug 394521. I think we can close as duplicate.
*** This bug has been marked as a duplicate of bug 394521 ***