-
Notifications
You must be signed in to change notification settings - Fork 170
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
extend cases of unnecessary_null_checks/null_check_on_nullable_type_parameter #3258
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5081ed4
extend cases of unnecessary_null_checks/null_check_on_nullable_type_p…
a14n 401eac1
fix bad await handling
a14n 1e8b797
add handling of async
a14n 1227225
address review comments
a14n 2695e5a
add test for typedef
a14n 545ba5c
add some negative tests
a14n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,14 +41,36 @@ DartType? getExpectedType(PostfixExpression node) { | |
node.thisOrAncestorMatching((e) => e.parent is! ParenthesizedExpression); | ||
var parent = realNode?.parent; | ||
|
||
DartType? Function(DartType? type) mayHandleAwait = (type) => type; | ||
if (parent is AwaitExpression) { | ||
mayHandleAwait = | ||
(type) => (type as ParameterizedType?)?.typeArguments.first; | ||
parent = parent.parent; | ||
} | ||
|
||
// in return value | ||
if (parent is ReturnStatement || parent is ExpressionFunctionBody) { | ||
var parentExpression = parent?.thisOrAncestorOfType<FunctionExpression>(); | ||
if (parentExpression == null) { | ||
return null; | ||
} | ||
var staticType = parentExpression.staticType; | ||
return staticType is FunctionType ? staticType.returnType : null; | ||
return staticType is FunctionType | ||
? mayHandleAwait(staticType.returnType) | ||
: null; | ||
} | ||
// in yield value | ||
if (parent is YieldStatement) { | ||
var parentExpression = parent.thisOrAncestorOfType<FunctionExpression>(); | ||
if (parentExpression == null) { | ||
return null; | ||
} | ||
var staticType = parentExpression.staticType; | ||
if (staticType is! FunctionType) { | ||
return null; | ||
} | ||
return mayHandleAwait( | ||
(staticType.returnType as ParameterizedType).typeArguments.first); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and a few places below have the same problem. Consider moving the logic into a single |
||
} | ||
// assignment | ||
if (parent is AssignmentExpression && | ||
|
@@ -57,27 +79,44 @@ DartType? getExpectedType(PostfixExpression node) { | |
node.operand is! Identifier || | ||
(parent.leftHandSide as Identifier).name != | ||
(node.operand as Identifier).name)) { | ||
return parent.writeType; | ||
return mayHandleAwait(parent.writeType); | ||
} | ||
// in variable declaration | ||
if (parent is VariableDeclaration) { | ||
return parent.declaredElement?.type; | ||
return mayHandleAwait(parent.declaredElement?.type); | ||
} | ||
// as right member of binary operator | ||
if (parent is BinaryExpression && parent.rightOperand == realNode) { | ||
var parentElement = parent.staticElement; | ||
if (parentElement == null) { | ||
return null; | ||
} | ||
return parentElement.parameters.first.type; | ||
return mayHandleAwait(parentElement.parameters.first.type); | ||
} | ||
// as member of list | ||
if (parent is ListLiteral) { | ||
return mayHandleAwait( | ||
(parent.staticType as ParameterizedType?)?.typeArguments.first); | ||
} | ||
// as member of set | ||
if (parent is SetOrMapLiteral && parent.isSet) { | ||
return mayHandleAwait( | ||
(parent.staticType as ParameterizedType?)?.typeArguments.first); | ||
} | ||
// as member of map | ||
if (parent is MapLiteralEntry) { | ||
var typeParameters = | ||
(parent.parent! as SetOrMapLiteral).staticType as ParameterizedType?; | ||
return mayHandleAwait( | ||
typeParameters?.typeArguments[parent.key == node ? 0 : 1]); | ||
} | ||
// as parameter of function | ||
if (parent is NamedExpression) { | ||
realNode = parent; | ||
parent = parent.parent; | ||
} | ||
if (parent is ArgumentList && realNode is Expression) { | ||
return realNode.staticParameterElement?.type; | ||
return mayHandleAwait(realNode.staticParameterElement?.type); | ||
} | ||
return null; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be misunderstanding the code, but if the goal of this is to unwrap
Future
andFutureOr
, it will actually attempt unwrap any potentially parameterized type. (The classParameterizedType
only means "a kind of type that is allowed to have type parameters". It doesn't mean "a type that actually has type parameters".)I think that this will happily unwrap
List<int>
, and will crash whentype
is something without type arguments, such asString
.Consider using
isDartAsyncFuture
andisDartAsyncFutureOr
explicitly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
humm, there was indeed issue with await handling. The
return await
case is the only one that need a special processing because the target type is really a Future (unlike other case where the unwrap type is provided)