Comments (4)
This is a known issue. A similar situation was discussed in dart-lang/language#2977.
Every switch expression is subject to exhaustiveness checking, and the exhaustiveness analysis does recognize this case, so the following version of the code is accepted:
int add(int? a, int? b) => switch ((a, b)) {
(_, null) => a!,
(null, _) => b!,
(final int a, final int b) => a + b
};
However, exhaustiveness analysis is different from the flow analysis which is also performed, and the exhaustiveness analysis is stronger in some instances. So the declaration is accepted because it is subject to both kinds of analysis.
A switch statement is different: It is only subject to exhaustiveness analysis in the case where the static type of the scrutinee is an 'always-exhaustive type', and that doesn't include the record type (int?, int?)
, because it doesn't include int?
.
This means that the switch statement is subject to flow analysis (that one is always enabled), but not to exhaustiveness analysis. And the flow analysis isn't smart enough to detect that this particular switch statement is exhaustive.
The issue dart-lang/language#2977 explicitly mentions this catch-22: If the default
case is included then it is flagged as unreachable, but if it is omitted then the switch statement isn't recognized as being exhaustive.
There is no beautiful solution, but you might use something like the following:
int add(int? a, int? b) {
switch ((a, b)) {
case (_, null): return a!;
case (null, _): return b!;
case (final int a, final int b): return a + b;
}
// Flow analysis cannot see that the switch is exhaustive.
throw "Unreachable";
}
from sdk.
Mm yeah. The way to comply with both of these is to add a final return
outside the switch
.
It's possible the body_might_complete_normally
logic could be updated to treat switch statements as though they considered exhaustiveness...
from sdk.
It could have something to do with how records are analyzed, since having just 1 parameter works fine:
// no linter issues!
int increment(int? a) {
switch (a) {
case null: return 0;
case final int a: return a + 1;
}
}
from sdk.
Thank you @eernstg!
I think it makes sense to close this one as a duplicate of dart-lang/language#2977.
from sdk.
Related Issues (20)
- False positive in unintended_html_in_doc_comment lint HOT 2
- [breaking change] Compute closures of type schemas in the upper bound computations in the CFE as specified HOT 2
- Assists for adding and maybe removing digit separators from number literals
- [breaking change] Mark `IOOverrides` as `base` HOT 3
- Check if current is available within Iterator without moving the iterator HOT 1
- A directory which is "excluded" via analysis options should not be searched for other analysis options. HOT 1
- Missing unnecessary `!` warning with nested extension types HOT 4
- Exception from yaml parsing during dart fix HOT 1
- http library throws `HttpException: Connection closed before full header was received` without a stack trace HOT 4
- [analyzer_plugin]: setContextRoots event never received HOT 1
- DateTime parse function is too generic HOT 1
- `augmented()` in generative constructors needs additional compile-time checks
- Macro with explicit "dart:core" import doesn't work HOT 1
- Not able to close/release RawDatagramSocket after receiving a SocketException in Windows Desktop HOT 2
- Weird error: The argument type 'Object' can't be assigned to the parameter type 'Object'. HOT 2
- ZLibDecoder regression in 3.5.0 HOT 5
- [DDC] Performance regressions related to new generic class representation
- [dart2wasm] Extremely large const data collections emit functions that exceed maximum function size in wasm HOT 1
- Add @Since annotations to JS APIs added in 3.6 HOT 1
- Incorrect dead code removal quick fix when multiple cases share a body HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.