New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uneachable code / Dead code on nested if #51869
Comments
This requires special knowledge about That's not unreasonable, we have and (will) use that information in other places, like switches over |
I think this would have been caught (or... there was aspiration that this would have been caught) by the now defunct invariant_booleans linter rule. It became really bogged down with bugs, false positives, false negatives, etc. I don't see us implementing support for such dead code any time soon. But I've thought about it: one such neat way to implement it robustly (hopefully) would be if Dart's flow analysis code had hooks and/or an attribute map on which we could hang information. Because implementing checks like this well require flow analysis, but it would be a shame to implement flow analysis n times for n lint rules + language features. |
@stereotype441 Should we create an |
@srawlins said:
and @bwilkerson said:
I have no objection to an Flow analysis is highly constrained in what it can do effectively for four major reasons:
Also, flow analysis contains extra complexity that's not present in other parts of the analyzer because it has to be able to work with both the analyzer and CFE representations of code. I have long dreamt of adding a facility to the analyzer that does basic block analysis of a function, reduces each basic block to a set of equations involving types and boolean values, and then iterates those equations to a fixed point. This analysis wouldn't have any of the drawbacks above, and could be used as the basis for lints and dead code analysis, but not for compile-time errors. Ideally, we would design it with an eye toward adding more sophisticated "symbolic execution" features in the future, such as verifying function preconditions and postconditions, detecting out-of-range array accesses, and checking that the program doesn't violate important Flutter design invariants. I've had some hallway discussions with @bwilkerson and @pq about this, and I hope to sketch out a brief design proposal for it once my work on Dart 3.0 is wrapped up. My preference would be to consider feature requests like this one as design goals for this new analysis facility, rather than candidates for inclusion in flow analysis. |
That sound reasonable to me. |
Yeah this sounds good to me. It makes sense that we shouldn't hook into the (currently) only flow analysis engine we have. It'd be great to have an additional flow analysis system that we can use when calculating diagnostics like these to answer "can X have been modified within this block at this statement?" or "has X only been assigned non-nullable values at this statement?" I imagine this will be written in a 3rd party plugin before the analyzer proper. |
Describe the issue
There are two lints which could be used for this situation. Most probably
Uneachable code
.To Reproduce
If I have a code similar to this:
Expected behavior
I expected the block inside
if (variable == false)
would be warned withUneachable code
. I've tried with non-final variables first and I think that could be a case where this is true if I can replace the value of the variable (but shouldn't dart analysis see that that was anUneachable code
in that case as well, since I've not updated the value). But with final variables that is not possible at all.The text was updated successfully, but these errors were encountered: