Analyzer should issue a "mixed mode unsoundness" warning (or hint) in certain scenarios #43452
Labels
legacy-area-analyzer
Use area-devexp instead.
NNBD
Issues related to NNBD Release
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
Due to dart-lang/language#1143, we are changing the front end so that inserts a synthetic
throw
if certain "impossible" situations arise due to the inherent unsoundness of mixed mode. For most of these scenarios, there should be a corresponding analyzer warning (or hint) to alert users to the situation and point them to documentation of what to do about it.(Note that some or all of these scenarios may already be covered by analyzer hints, but I believe that in a lot of cases the hint is too general, e.g. a "dead code" hint. We want to have one or more hints that are specific to the following scenarios so that we can link to the appropriate documentation.)
The scenarios the analyzer should warn (or hint) about are as follows:
is
test to produce a false result (or anis!
test to produce a true result). This can be identified by FlowAnalysis.isExpression_end returningfalse
.is
todynamic
.==
test to produce a true result (or a!=
test to produce a false result). This can be identified by FlowAnaylsis.equalityOp_end returningfalse
. This will only occur when one side of the comparison has typeNull
, so in practice this probably means one side of the comparison is the literalnull
.dynamic
.??
or??=
operation to be reached. This can be identified by FlowAnalysis.ifNullExpression_rightBegin returningfalse
.??
expression, that the user can eliminate the warning by replacing the expression with its left hand side, or suppress it by casting the left hand side to a nullable type.??=
expression, the user can eliminate the warning by removing the assignment, or suppress it by replacing it with its desugared equivalent and using a cast todynamic
(e.g. replacingx ??= y;
withif ((x as dynamic) == null) x = y;
).?.
or?..
to benull
. This can be identified by FlowAnalysis.nullAwareAccess_rightBegin returningfalse
.?
, or suppress it by casting the target to a nullable type.There is one other scenario in which the front end inserts a synthetic
throw
: after evaluation of an expression having typeNever
. However, I don't think this should have a special analyzer warning beyond the usual "dead code" hint, because it crops up too frequently in legitimate code (e.g. callingfail
in a test). Also I believe it's unlikely to be a problem in practice.I plan to follow up with a language test that expects an analyzer warning in each of these cases. If we decide that we want a hint instead, we'll have to work with @munificent to adapt the language test infrastructure to be able to test for hints.
The text was updated successfully, but these errors were encountered: