Simple unreachable-code detection
Needs ReviewPublic

Authored by flherne on Feb 12 2017, 5:37 PM.

Details

Reviewers
brauch
Summary

[unfinished, rfc]

Code in the same block after break, continue, return or raise can never be executed. Add a visitor that tracks when this is true and adds warnings to affected expressions.

Test Plan

It...seems to work.

TODO: unit tests, stress-testing, more looking at results.

Diff Detail

Repository
R53 KDevelop: Python Support
Lint
Lint Skipped
Unit
Unit Tests Skipped
flherne updated this revision to Diff 11252.Feb 12 2017, 5:37 PM
flherne retitled this revision from to Simple unreachable-code detection.
flherne updated this object.
flherne edited the test plan for this revision. (Show Details)
flherne added a reviewer: brauch.
flherne set the repository for this revision to R53 KDevelop: Python Support.
Restricted Application added a subscriber: kdevelop-devel. ยท View Herald TranscriptFeb 12 2017, 5:37 PM

Even though we in principle have this kind of stuff in the control flow graph, this looks sane to me, tbh the control flow graph is a bit overdesigned for what one usually wants to do.

Hm, I think this actually needs to be merged into the declaration builder.

At the moment,

def foo(): 
    if False: return 10

is given return type int when in fact it's equivalent to:

def foo():
     if False: return 10
     return None

And always returns NoneType.

i.e. the declaration builder must know whether the end of the function can be reached without hitting a return statement.

Not sure; if the nonsense code is deteced and marked with a warning, is it really a problem when the type inferred for it by static analysis is inaccurate?

In the example above, the code isn't nonsensical or warned about, but we get it wrong anyway. ;-)

The problem is that we ignore the implicit return None at the end of functions if there's /any/ explicit return statement, even if control-flow can still reach the end without hitting one.

(well, if False is daft, but we can't warn on that without tracking constant values)

Ah, I see what you mean -- you are right, if you want that feature. I'm fine with both variants, I think it's useful even without being in the declaration builder.